5

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);
Tommy Nicholas
  • 1,133
  • 5
  • 20
  • 31

2 Answers2

8

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.

Derek MacDonald
  • 231
  • 2
  • 10
Simon Hampel
  • 2,686
  • 1
  • 18
  • 18
  • @Mattias confirmed. You need to create the 'emails.test' view, but the email that fires is the template specified via the headers. – Russ Matney Jun 08 '15 at 17:37
  • HOWEVER – smtp headers are limited to 1000 chars, so you won't be able to hand unlimited data to the template that way. See https://mandrill.zendesk.com/hc/en-us/articles/205582487-How-do-I-use-merge-tags-to-add-dynamic-content-#provide-merge-data-using-smtp-headers – Russ Matney Jun 08 '15 at 17:49
  • Thanks for this. Two things: 1. `X-MC-Temmplate` has two `m`s in it. 2. You can add more than 1000 characters, you just need to add multiple headers. – hotmeteor Jul 03 '15 at 16:39
  • This should be marked as the correct answers, as it's more practical and doesn't require gambs. +1 – Mateus Felipe Nov 30 '17 at 17:05
3

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.

clarkf
  • 3,032
  • 20
  • 14
  • That does indeed seem to be the case. Upon further thought, there doesn't seem to be a compelling reason not to just roll my own template in Laravel - there's a couple little things that I'd prefer Mandrill to handle but nothing major. – Tommy Nicholas Aug 20 '14 at 23:59
  • 1
    There are a lot of reasons to use a template hosted on Mandrill. Primarily speed and bandwidth. If you're sending off a million templated e-mails, that's a million you have to render within PHP and a million you have to send over the network as the body to mandrill. – datashaman Feb 16 '15 at 08:41
  • Just to add to @datashaman's comment, depending on what you're doing, there's also a huge advantage to non-devs being able to add and edit the templates via mandrill vs. requiring dev time/experience/a dev environment to edit the code itself – Russ Matney Jun 08 '15 at 17:02