Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the sender address in mail in laravel with mail gun

I am sending the email using below code

Mail::send('emails.sample',[],function($message) use($attachment,$filename)
        {
            $message->subject('PDF is generated !');
            $message->from('[email protected]', 'Example');
            $message->to('[email protected]');
            $message->attachData($attachment,$filename,array('mime'=>'application/pdf','Content-Disposition'=>'attachment'));
        });

but after receiving the email, sender email address is coming like this

[email protected] on behalf of Example [email protected]

but it should be like either email address or name

[email protected]

How do I fix this? any suggestions, please!

like image 577
06011991 Avatar asked Oct 22 '25 04:10

06011991


2 Answers

For anyone still reading this - it's because you need to set the "sender" address. Gmail doesn't seem to care, but Outlook does.

$message->sender(config('mail.from.address'));
like image 80
Gary Green Avatar answered Oct 24 '25 03:10

Gary Green


you can add these in .env:

[email protected]
MAIL_FROM_NAME=From Name Surname

in alternative, if you let it blank, you can still change them in config/mail.php, here replace with yours:

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'IDesk'),
],

as you can see config/mail.php at first try to read it from .env sto .env should be used

like image 32
Luca C. Avatar answered Oct 24 '25 01:10

Luca C.