Install Mandrill Laravel Template

I am trying to send Mandrill email to Laravel 4 Mailer by default and I want to install the template and send data to Mandrill. I would prefer not to use the local file in my Laravel, I have everything I need to work inside Mandrill. Here's how I installed it in Laravel:

Mail::send('emails.test',[],function($message) { $message->to(Input::get('email'), 'John Smith')->subject('Welcome!'); }); return 

This sends an email from my "test" view inside the "emails" as it should. In vanilla PHP using the Mandrill library, this will work to send to the template I want.

 $message = array( 'subject' => 'Subject redacted', 'from_email' => $main_email, 'to' => array(array('email' => $to_email)), 'merge_vars' => array(array( 'rcpt' => $to_email, 'vars' => array( array( 'name' => 'DETAIL', 'content' => $detail), array( 'name' => 'ERROR_AMOUNT', 'content' => '$'.$trans_amount) , array( 'name' => 'CO_NAME', 'content' => $business_name), array( 'name' => 'SMS', 'content' => $sms) )))); $template_name = 'Test Email'; $template_content = array( array( 'name' => 'main', ) ); $response = $mandrill->messages->sendTemplate($template_name, $template_content, $message); 
+5
source share
2 answers

It seems like this is possible using Illuminate\Mail\Mailer . Mandrill transport seems to be using the send-raw Mandrill API Endpoint.

This gives you two options: execute your own transport and work in Mailer or work outside the Mailer system. If you are going to go with the latter, you are probably best off using the official library.

If you want to implement your own transport, check out the Swift_Transport interface. This will not be the perfect combination, since you are trying to do something that Mailer never intended to do, but with enough hack you could start something and start it. You probably need to use undefined members of the Message class, for example:

 $message->template = "template_name"; $message->content = array(/* content here */); $message->message = array(/* message here */); // MandrilTemplateTransport.php public function send(Swift_Mime_Message $message, &$failedRecipients = null) { $this->messages->send($message->template, $message->content, $message->message); } 

This is absolutely not ideal, but a workable solution. I would vote for you just using the Mandrill library outside of the Mailer system.

+3
source

Alternatively, you can set the X-MC-MergeVars and X-MC-Template headers using the Laravel Mandrill functions.

 Mail::send('emails.test', [], function($message) use ($mergevars) { $headers = $message->getHeaders(); $headers->addTextHeader('X-MC-MergeVars', json_encode($mergevars)); $headers->addTextHeader('X-MC-Template', 'my-template'); $message->to(Input::get('email'), 'John Smith')->subject('Welcome!'); }); 

Please note that I have not tested this - I just went over to what I read here: Using SMTP headers to configure your messages .

You may need to use something other than addTextHeader - check the Swiftmailer documentation to find out how to add the correct header type for the data you are setting.

+7
source

Source: https://habr.com/ru/post/1200787/


All Articles