Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add headers to email in Laravel 5.1

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
like image 777
geoffs3310 Avatar asked Sep 01 '15 18:09

geoffs3310


People also ask

How do I add an email header?

Click "Settings" > "Appearance" on the left menu. Scroll down to "Email Headers and Footers." Click the "HTML Header" dropdown and select either HTML Header or Text header. Enter the header and footer information into the appropriate box.

How do I change my email header in Laravel?

php artisan vendor:publish --tag=laravel-mail and go to the resources/views/vendor/mail/html/message. blade. php and modify the header and footer slot. For changing Hello to Hello {user_name}, there is markdown called greeting() method that holds Hello! , you can change it whatever you want.

Where can I set headers in Laravel?

In Laravel headers should be set on the response object.

How do you make a custom header in Laravel?

getting a custom header in Laravel 5.8.If using a header like X-Requested-With: XMLHttpRequest you may notice that it converts this to HTTP_X_REQUESTED_WITH . This, in turn, is converted to lower case version for the header() method.


1 Answers

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');
like image 104
Maxim Lanin Avatar answered Oct 13 '22 21:10

Maxim Lanin