Is there a way to add default headers to all emails in Laravel 5.1? I want all emails to be sent with the following header:
x-mailgun-native-send: true
Laravel uses SwiftMailer for mail sending.
When you use Mail facade to send an email, you call send() method and define a callback:
\Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
$m->to($user->email, $user->name)->subject('Your Reminder!');
});
Callback receives $m
variable that is an \Illuminate\Mail\Message
object, that has getSwiftMessage()
method that returns \Swift_Message
object which you can use to set headers:
$swiftMessage = $m->getSwiftMessage();
$headers = $swiftMessage->getHeaders();
$headers->addTextHeader('x-mailgun-native-send', 'true');
syntax error, unexpected '$swiftMessage' (T_VARIABLE)
. Thoughts?
Commented
Dec 4, 2015 at 23:09
I know this is an old post, but I'm passing by the same problem right now and I think this could be useful to others.
If you're using a structure exactly as shown in Laravel (6.x and 7.x), like this:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('[email protected]')
->view('emails.orders.shipped');
}
you could add headers to the E-mail in the format below:
public function build()
{
return $this->from('[email protected]')
->view('emails.orders.shipped')
->withSwiftMessage(function ($message) {
$message->getHeaders()
->addTextHeader('Custom-Header', 'HeaderValue');
});
}
I hope this could be useful.
link to the current documentation:https://laravel.com/docs/7.x/mail#customizing-the-swiftmailer-message
Slight modification to @maxim-lanin's answer. You can use it like this, fluently.
\Mail::send('email.view', ['user' => $user], function ($message) use ($user) {
$message->to($user->email, $user->name)
->subject('your message')
->getSwiftMessage()
->getHeaders()
->addTextHeader('x-mailgun-native-send', 'true');
});
For those who need to add this configuration to all emails (like me), there is a practical way using listeners.
php artisan make:listener MessageSendingListener
MessageSendingListener
:<?php
namespace App\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Queue\InteractsWithQueue;
class MessageSendingListener
{
/**
* Handle the event.
*
* @param \Illuminate\Mail\Events\MessageSending $event
* @return void
*/
public function handle(MessageSending $event)
{
$event->message
->getHeaders()
->addTextHeader('x-mailgun-native-send', 'true');
}
}
EventServiceProvider
:<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
// ...
\Illuminate\Mail\Events\MessageSending::class => [
\App\Listeners\MessageSendingListener::class,
],
];
}
Obs.: Tested with Laravel 7.
x-mailgun-native-send
? i can't seems to find any documentation on mailgun website. ta