I'm trying to send a Mandrill email in the default Laravel 4 Mailer, and I want to set the template and send the data to Mandrill. I'd rather not use a local file in my Laravel, I have this all set up to work inside of Mandrill. This is how I have it set 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 of "emails" like it should. In vanilla PHP using the Mandrill library this would 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);
As an alternative, you could set the X-MC-MergeVars and X-MC-Template headers using Laravel's 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!');
});
Note that I haven't tested this - just going by what I read here: Using SMTP Headers to customize your messages.
You might need to use something other than addTextHeader - check with the Swiftmailer documentation to work out how to add the correct header type for the data you are setting.
It doesn't appear that this is possible using Illuminate\Mail\Mailer
. It looks like the Mandrill transport specifically uses the send-raw
Mandrill API endpoint.
This leaves you with two options: Implement your own transport and work within the Mailer
, or work outside the Mailer
system. If you're going to go with the latter, you'd probably be best off just using the official library.
If you want to implement your own transport, take a look at the Swift_Transport
interface. It's not going to be a perfect match, since you're attempting to do something that the Mailer
was never intended to do, but with enough hacking, you could get something up and running. You'd probably need to use undefined members on the Message
class, like:
$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 less than ideal, but a workable solution. I'd vote that you just use the Mandrill library outside of the Mailer
system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With