Add default mail headers in Laravel 5.2

2020-05-02 10:27发布

问题:

I need to send a mail header "x-mailgun-native-send: true" to all my mails so they wont get rejected by receivers.

Is it possible to set default mail headers in Laravel? I know I can add headers with every mail by adding this:

$m->getHeaders()->addTextHeader('x-mailgun-native-send', true);

Thanks

回答1:

I found at Laravel will fire an event right before sending an email: https://laravel.com/docs/5.3/mail#events You can listen for this event and add your headers in the event listeners handle method.

public function handle(MessageSending $event)
{
    $headers = $event->message->getHeaders();
    $headers->addTextHeader('x-mailgun-native-send', true);
}