Simplest way to use Mail::queue with Laravel 5, IronMQ? - email

I've read other posts on StackOverflow and elsewhere on using Laravel mail and IronMQ, e.g. Using Mail::queue with iron.io - they either advocate using Queue::push and Mail::send together, or else say you can use Mail::queue but don't provide specific examples of code.
Is it possible to queue emails with just Mail::queue? (i.e. without using Queue::push?)
I've tried with the code below, but Mail::queue here doesn't actually send a message to Iron (my other settings should be OK as I can see Queue::push works, and Mail::send works too just without the queue getting involved)
public function submit_contact_form()
{
ContactForm::create(['email'=>$_POST['email'],'query'=>$_POST['query'],'name'=>$_POST['name']]);
$name_fixed = ucwords(strtolower($_POST['name']));
$data = array('name'=>$name_fixed, 'query'=>$_POST['query']);
Mail::queue('emails.contact_form', $data, function($message) {
$name_fixed = ucwords(strtolower($_POST['name']));
$message->to($_POST['email'], $name_fixed)->subject('Contact received');
});
}
Route::post('/queue', function() {
return Queue::marshal();
});
So to sum up, what's the simplest / most efficient way to use Iron queues with Laravel 5 mail?
Thanks

Related

What is the correct way to process REST requests in Contoller

I have created a controller for an "applications" table. The web and REST interfaces are working but I think the add and edit functions should be better.
When I tested add and edit I found the data needed to be posted in web FORM format (not JSON).
I found I needed to use "$this->request->input('json_decode')" in the save to decode the JSON data. I thought this happened automagically.
This function now works for add (edit is similar) and displays my json/add.ctp so I can return the successful record to the user.
public function add() {
if ($this->request->is('post')) {
$this->Application->create();
//Is the request REST passing a JSON object?
if (preg_match('/\.json/', $this->request->here)){
//This is a REST call
$this->set('status', $this->Application->save($this->request->input('json_decode')));
} else {
//This is an interactive session
if ($this->Application->save($this->request->data)) {
$this->Session->setFlash(__('The application has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The application could not be saved. Please, try again.'));
}
}
}
}
I used the "$this->request->here" to see if it ends in ".json". Is this the "correct" way to process the REST call?
There is an entire section in the CakePHP Book for this. I think it will answer your question(s):
http://book.cakephp.org/2.0/en/development/rest.html
The question is, does your action accept JSON data & Form Data? or just JSON data?
The .json is purely for the output of your data, you are able to send JSON data with the .xml extension, the difference being once the data is sterilised, it will output in XML.
if($this->request->is('post')) {
if(empty($this->request->data)){
$data = $this->request->input('json_decode', TRUE);
} else {
$data = $this->request->data;
}
} else {
$data = $this->params['url'];
}
Above is kind of what you should be doing, check if the data comes from a form, if not, decode JSON, and if it is NOT a POST, save parameters that have been included into the URL.
I am not saying the above is the "right" way to do it, but thats probably what you are looking for.

setting up script to include google docs form data in email notification

I've setup a form using googledocs. I just want to have the actual data entered into the form emailed to me, as opposed to the generic response advising that the form has been completed.
I have no skill or experience with code etc, but was sure i could get this sorted. I've spent hours+hours and haven't had any luck.
My form is really basic.it has 5 fields. 4 of which are just text responses, and one multiple choice.
I found this tute online (http://www.labnol.org/internet/google-docs-email-form/20884/) which i think sums up what i'm trying to do, but have not been able to get it to work.
from this site i entered the following code:
function sendFormByEmail(e)
{
var email = "reports.mckeir#gmail.com";
var subject = "Google Docs Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
for(var i in headers)
message += headers[i] + ' = '+ e.namedValues[headers[i]].toString() + "\n\n";
MailApp.sendEmail(email, subject, message);
}
To this, i get the following response: ->
Your script, Contact Us Form Mailer, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.
The script is used by the document 100% Club.
Details:
Start Function Error Message Trigger End
12/3/12 11:06 PM sendFormByEmail TypeError: Cannot call method "toString" of undefined. (line 12) formSubmit 12/3/12 11:06 PM
Is anyone able to help shed some light on this for me? I'm guessing i'm not including some data neeeded, but i honestly have no clue.
Workaround http://www.labnol.org/internet/google-docs-email-form/20884/
You have to setup app script to forward the data as email.
I'll point to the comment above that solved it for me: https://stackoverflow.com/a/14576983/134335
I took that post a step further:
I removed the normal notification. The app script makes that generic text redundant and useless now
I modified the script to actually parse the results and build the response accordingly.
function sendFormByEmail(e)
{
var toEmail = "changeme";
var name = "";
var email = "";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
var subject = "Google Docs Form Submitted";
var message = "";
// The variable e holds all the form values in an array.
// Loop through the array and append values to the body.
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
// Credit to Henrique Abreu for fixing the sort order
for(var i in headers) {
if (headers[i] = "Name") {
name = e.namedValues[headers[i]].toString();
}
if (headers[i] = "Email") {
email = e.namedValues[headers[i]].toString();
}
if (headers[i] = "Subject") {
subject = e.namedValues[headers[i]].toString();
}
if (headers[i] = "Message") {
message = e.namedValues[headers[i]].toString();
}
}
// See https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)
var mailOptions = {
name: name,
replyTo: email,
};
// This is the MailApp service of Google Apps Script
// that sends the email. You can also use GmailApp here.
MailApp.sendEmail(toEmail, subject, message, mailOptions);
// Watch the following video for details
// http://youtu.be/z6klwUxRwQI
// By Amit Agarwal - www.labnol.org
}
The script utilized in the example is extremely generic but very resilient to change because the message is built as a key/value pair of the form fields submitted.
If you use my script you'll have to tweak the for loop if statements to match your fields verbatim. You'll also want to edit the toEmail variable.
Thanks again for the question and answers. I was about to ditch Google Forms as the generic response was never enough for what I was trying to do.
Lastly, in response to the actual problem above "toString of undefined" specifically means one of the form fields was submitted as blank. If I had to guess, I would say the author only used this for forms where all the fields were required or a quick undefined check would've been put in place.
Something like the following would work:
for(var i in headers) {
var formValue = e.namedValues[headers[i]];
var formValueText = "";
if (typeof(formValue) != "undefined") {
formValueText = formValue.toString();
}
message += headers[i] + ' = '+ formvalueText + "\n\n";
}
I haven't tested this precisely but it's a pretty standard way of making sure the object is defined before trying methods like toString() that clearly won't work.
This would also explain Jon Fila's answer. The script blindly assumes all of the header rows in the response are sent by the form. If any of the fields aren't required or the spreadsheet has fields that are no longer in the form, you'll get a lot of undefined objects.
The script could've been coded better but I won't fault the author as it was clearly meant to be a proof of concept only. The fact that they mention the replyTo correction but don't give any examples on implementing it made it perfectly clear.
If this is a Google Form, do you have any extra columns in your spreadsheet that are not on the form? If you delete those extra columns then it started working for me.
You don't need to use a script. Simply go to Tools >> Notification Rules on your Google Spreadsheet. There you can change the settings to receive an email with your desired information every time the document is changed.

codeigniter2, nusoap, soap server and examples

A morning of googling I've learnt a few things.
Many people would like to use their CI2 installs to make a soap server for other apps to talk to their app.
However, each forum post I find ends in 'This doesn't work in CI2' or similar.
I've found this article:
http://phpmaster.com/web-services-with-php-and-soap-1/
Which is GREAT... but I can't get it working in CI2.
I've put my library in place, renamed where needed and this code is bringing up many errors.
I've not started on the client side yet.
`class soap extends CI_Controller {
function __construct ()
{
parent:: __construct ();
$this->load->library('nusoap_base');
}
function index()
{
$this->nusoap = new soap_server();
$this->nusoap->register("getProd");
$this->nusoap->service($HTTP_RAW_POST_DATA);
}
function getProd($category) {
if ($category == "books") {
return join(",", array(
"The WordPress Anthology",
"PHP Master: Write Cutting Edge Code",
"Build Your Own Website the Right Way"));
}
else {
return "No products listed under that category";
}
}
}`
Got fed up, using CI REST library instead (cheers Phil!)
:)

Passing hash from perl CGI::Application::Plugin::JSON to jquery form plugin

I need to pass a hash from server side to client side. I am using jquery and perl CGI::Application respectively on the front-end and back-end. I am a starter when it comes to using jquery thus I modified the jquery form plug-in example which shows how to handle JSON data returned from the server http://jquery.malsup.com/form/#json. I tried to use the given code with my favourite perl web framework CGI::Application. The CGI::Application::Plugin::JSON works well when passing scalar values but due to lack of documentation I can't figure out how to pass arrays or hashes or for that matter complex data structures. When passing a hash I am using the following code snippet :-
foreach my $k (sort keys %hash)
{
return $self->add_json_header ( { message => $hash{$k}} );
}
This is the error I am getting in the apache error log:
ajaxtest.pl: Odd number of elements in hash assignment at /usr/local/share/perl/5.10.0/CGI/Application/Plugin/JSON.pm line 98., referer: http://localhost/echo.html
While passing scalar I am using CGI::Application::Plugin::JSON json_body function.
Kindly let me know where I am going wrong. Following is the Jquery code in the html file that is also given on the form plugin site (link given above):
// prepare the form when the DOM is ready
$(document).ready(function() {
// bind form using ajaxForm
$('#jsonForm').ajaxForm({
// dataType identifies the expected content type of the server response
dataType: 'json',
// success identifies the function to invoke when the server response
// has been received
success: processJson
});
});
function processJson(data) {
// 'data' is the json object returned from the server
alert(data.message);
}
Any advice on using CGI::Application::Plugin::JSON with complex data structures likes hashes of hashes and arrays of arrays is most welcome as I would be needing it it in future.
Here's a possible solution.
you will only need the JSON library and in your code you can do the following:
my %data_struct = { a => 1, b => 2 };
my $json = to_json( \%data_struct, {utf8 => 1} );
$json =~ s/"(\d+?)"/$1/g; # to_json puts quotes around numbers, we take them off
# here $self is the CGI::App object, it's probably called like that
$self->header_add( -type => 'application/json' );
return $json;
(As Raoul pointed out, you cannot return more than once in a CGI::App block.)
NOTE: I don't use CGI::Application::Plugin::JSON because I just didn't need it. This way I achieved the same result. Of course, TMTOWTDI. :)
I don't think you understand the CGI::APP return method. You can only return once per runmode.

XEP-0077: In-Band Registration

i am a newbie for xmpp. i plan to start a 'chat' web application.at client,i prepare use 'Strophe',but i found strophe cannot support registeration module.
someone said can use 'XEP-0077: In-Band Registration'.can u tell me what i can do it?
thanks
XEP-0077 is the way to go here. Make sure you've read it thoroughly. Next, look at the strophejs-plugins project to get some examples of how to write a strophe plugin. Then, you'll want to create protocol that imlements XEP-0077, starting with something like:
Strophe.addConnectionPlugin('register', {
_connection: null,
init: function(conn) {
this._connection = conn;
Strophe.addNamespace('REGISTER', 'jabber:iq:register');
},
get: function(callback) {
var stanza = $iq({type: "get"}).c("query",
{xmlns: Strophe.NS.REGISTER});
return this._connection.sendIQ(stanza.tree(), callback, function(){});
}
});
Make sure to contribute your patch to strophejs-plugins back on github.