Laravel: Got "on behalf of" mess in email header? say No more!
If you use email service like mail gun, they default set 'sender' header part (not 'from' header) as "example.com@mailsubdomain.example.com". and we mostly using only "from" function to set who sent the email (eg: contact@example.com) .. but some email client take this seriously . if original "example.com@mailsubdomain.example.com" is not equal to "from" that email client inform user to by showing like -
"<info=example.com@mailsubdomain.example.com on behalf of contact@example.com"
this email client do for prevent misleading users. mostly this is harmless but users who don't know about this can be panic when they saw email like this.
so how we fix this??
in Laravel there are two major functions in emails "from" and "sender" to set who sent the email... "sender" is more accurate that "from" for some reasons Laravel mosly documented only "from" function. but i found "sender" function in old version of laravel document..
Solution #1
laravel use "SwiftMailer" package and it has built in function named "setSender" we can call it by accessing that function in 'Mailable' build() method.
checkout Customizing The SwiftMailer Message
here how it do:
Solution #2
If you need more global scope change to sender, you can create and use a mail event. Laravel already exist mail event class in code folder. we just need to use it. here step by step to create it.
open Providers\EventServiceProvider and add this code to it(im using now Laravel 5.7. newer versions will work this one. if not please comment)
import laravel built it mail event class
add add MessageSending::class =>[ \App\Listeners\EmailEvent::class] to inside of protected $listen array.
now run : php artisan event:generate
it will create new event listener file in inside "app\Listeners" folder
here example event file I have created.....
from this you can modify sender also swift mail built in function. now using sender method you can get rid of behalf of('currently') from mails that sent to your users.
Comments
Post a Comment