wolfrevokcats
wolfrevokcats

Reputation: 1

"async" Parameter using Mandrill API with laravel

This is my code:

 $email = "[email protected]";
 $data = array(
     'email' => $email,
     'async' => false,
 );

 Mail::queue('user.mails.welcome', $data, function($message) use ($email) {
     $message
         ->to($email, $email)
         ->subject('Welcome to the Laravel 4 Auth App!');
 });

And this is what shows on my Mandrill account API log:

 Full Request {
     "key": "x",
     "raw_message": "y",
     "async": "1" 
 }
 Full Response {
     "email": "xy",
     "status": "queued",
     "_id": "z" 
 }

As you can see, emails are being queued by default. Why? And how can I change that?

Upvotes: 0

Views: 2907

Answers (4)

Christopher Pecoraro
Christopher Pecoraro

Reputation: 136

The 'async' parameter of the mandrill api isn't exposed to the Laravel mail closure. I just extended the MandrillTransport class and passed a Swift_Mime_Message into it so that I could capture the response, it will be 'sent' or 'rejected' (and reject_reason will be populated with something like "hard/soft bounce") which I can store in my application:

class Mandrill extends MandrillTransport
 {
  public function send(\Swift_Mime_Message $message, &$failedRecipients = null)
  {
     $client = $this->getHttpClient();

     $response = $client->post('https://mandrillapp.com/api/1.0/messages/send-raw.json', [
         'body' => [
             'key' => $this->key,
             'raw_message' => (string)$message,
             'async' => false,
         ],
     ]);
     return $response->json();
  }
}

Upvotes: 0

wolfrevokcats
wolfrevokcats

Reputation: 1

I had to set a "from" email to have this work.

Upvotes: 0

saverio
saverio

Reputation: 113

Per the Mandrill docs:

Async: enable a background sending mode that is optimized for bulk sending. In async mode, messages/send will immediately return a status of "queued" for every recipient. To handle rejections when sending in async mode, set up a webhook for the 'reject' event. Defaults to false for messages with no more than 10 recipients; messages with more than 10 recipients are always sent asynchronously, regardless of the value of async.

Basically, it's something that Mandrill is doing on their end is is unrelated to whether you use Mail::send or Mail::queue in Laravel.

All emails sending through Mandrill are queued and sent out in accordance with the parameters defined for your account. In other words: They decide when your emails eventually get sent.

Edit: Laravel's Mandrill Mail Transport is always going to send with async mode enabled. There's no way to configure that without editing the class: MandrillTransport

Upvotes: 1

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

I am not sure I understand. Your emails are being queued because you are using the queue() method:

Mail::queue('user.mails.welcome', $data, function($message) use ($email) {

If you want to not queue, then use the send() method:

Mail::send('user.mails.welcome', $data, function($message) use ($email) {

Upvotes: 0

Related Questions