7

I can't seem to locate the method within the Laravel 4 docs/Email API where I can add custom headers to an email.

For example:

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('foo@example.com', 'John Smith')->subject('Welcome!');
    $message->headers('X-Tags', 'tag1 tag2 tag3');
});

Does anyone know how this can be done?

Timmetje
  • 7,641
  • 18
  • 36
eski009
  • 371
  • 1
  • 6
  • 14

1 Answers1

15

As far as I know there's no way to add custom headers without reaching in to Swift Mailer.

Try something like this.

$message->getSwiftMessage()->getHeaders()->addTextHeader('X-Tags', 'tag1 tag2 tag3');

It doesn't look pretty but from looking through the source that should do the trick.


As pointed out in the comments below the getSwiftMessage method isn't required as there is an __call magic method on the Illuminate\Mail\Message class that I overlooked.

$message->getHeaders()->addTextHeader('X-Tags', 'tag1 tag2 tag3');
Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
  • 1
    The `getSwiftMessage()` method, is that one needed? I got a good result with just `$Message->getHeaders()->addTextHeader()` – Rob Gordijn Jul 04 '13 at 10:46
  • Ah yeah it's not needed. Didn't notice the magic method there that fires the methods on the `Swift_Message` class. I have updated my answer. – Jason Lewis Jul 04 '13 at 12:07
  • +2 This is great!!!!! I am using this with Mandrill seamlessly and can pull of stats on emails and can really customise emails. http://help.mandrill.com/entries/21688056-Using-SMTP-Headers-to-customize-your-messages – Gravy Sep 14 '13 at 11:40