Using Laravel's Mailgun driver, how do you (gracefully) send custom data and tags with your email?
Asked Answered
S

4

17

So I'm using Laravel 5.1 and trying to integrate with Mailgun. Well, that's easy, but now I'm trying to send custom variables from my application along with my emails.

I'm actually switching our application over from Mandrill because of their "new direction" and such. With them, I could supply the variables and tags via the email headers, but with Mailgun, that only works when you send via SMTP. In Laravel, Mail::send() uses an API call, so in theory I'd add the metadata there with "v:my-custom-data" => "{"this_id": 123}", but I'd like to avoid altering core classes like that.

I also considered using Bogardo/Mailgun, but then I'd have to replace all the Mail::send()s with Mailgun::send(), and then I couldn't send emails locally (environment based email driver), and then the application would be "married" to Mailgun.

Anyone done this before? Please let me know if I'm not clear here.

Schock answered 7/3, 2016 at 15:58 Comment(0)
S
30

I fixed my own problem. I was wrong, YOU CAN add custom variables via the SMTP method:

// Send email with custom variables and tags in Laravel
Mail::send('emails.blank',
    ['html' => 'This is a test of Mailgun. <strong>How did it work out?</strong>'],
    function($message) {
        $message->to('[email protected]');
        $message->subject('Mailgun API Test');

        $headers = $message->getHeaders();
        $headers->addTextHeader('X-Mailgun-Variables', '{"msg_id": "666", "my_campaign_id": 1313}');
        $headers->addTextHeader('X-Mailgun-Tag', 'test-tag');
    });

My testing was just inadequate. Very good to know, however I think this is an undocumented feature, so I suggest using with caution.

Schock answered 7/3, 2016 at 16:53 Comment(1)
As a side note, SparkPost's API does not behave this way, despite having similar metadata handling. I tried it recently.Schock
B
8

Update for Laravel 5.4+

As you can read in the offical docs:

The withSwiftMessage method of the Mailable base class allows you to register a callback which will be invoked with the raw SwiftMailer message instance before sending the message. This gives you an opportunity to customize the message before it is delivered:

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    $this->view('emails.orders.shipped');

    $this->withSwiftMessage(function ($message) {
        $message->getHeaders()
                ->addTextHeader('Custom-Header', 'HeaderValue');
    });
}
Brinn answered 26/9, 2018 at 13:1 Comment(0)
B
6

You can just do like this in Laravel 5 :

Mail::send(['text' => 'my_template'], $data, function ($message) {
  ..
  $message->getSwiftMessage()->getHeaders()->addTextHeader('X-Mailgun-Tag', 'my-tag');
});
Bullroarer answered 13/10, 2016 at 7:55 Comment(0)
B
1

Laravel implementation of the Mailgun Driver doesn't support options/parameters.

Here's a Gist which adds support to the native driver : https://gist.github.com/j0um/27df29e165a1c1e0634a8dee2122db99

Beautician answered 21/2, 2019 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.