Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 329
Captain Hypertext Avatar asked Mar 07 '16 15:03

Captain Hypertext


People also ask

Does mailgun send SMS?

TextMagic, also a Mailgun customer, allows you to send notifications, alerts, reminders, confirmations and SMS marketing campaign messages to your customers, staff members and suppliers. On average, TextMagic customers are sending over 2.5M text messages around the world each month.


1 Answers

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.

like image 119
Captain Hypertext Avatar answered Sep 19 '22 15:09

Captain Hypertext