06011991
06011991

Reputation: 807

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!

Upvotes: 4

Views: 10864

Answers (6)

Hamza Qureshi
Hamza Qureshi

Reputation: 202

Gmail automatically rewrites the "from" header of any messages sent through its SMTP server to the default "Send mail as" email address assigned in the Gmail or Google Apps account used to authenticate (in this case, your personal account).

This SMTP service is intended for personal use only, so its not very flexible. We can change this address by modifying the default account in the Gmail settings, but this still won't enable us to configure the "from" address through the application.

In development, this behavior may not matter, but for production environments, we'd need to use a mail provider that respects the headers sent from the application. Gmail also limits the rate of emails sent through its SMTP server in this way, which prevents us from using the service for most applications in production that send any significant amount of email.

Upvotes: 0

Luca C.
Luca C.

Reputation: 12584

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

Upvotes: 4

Gary Green
Gary Green

Reputation: 22395

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'));

Upvotes: 5

Otto
Otto

Reputation: 929

You'll need to set up some DNS records, so your mail provider can verify that mailgun is allowed to send email on behalf of your domain.

http://mailgun-documentation.readthedocs.io/en/latest/faqs.html#email-clients-say-sent-via-mailgun-us-with-messages-i-send-how-do-i-get-rid-of-this

Upvotes: -1

Kevin Patel
Kevin Patel

Reputation: 150

Are you using sandbox? Because I am also using the mailgun with sandbox and I am not facing this issue

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbox3940xxxxxxxxxxxxxxxxxx.mailgun.org
MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxx
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN=sandboxxxxxxxxxxxxxxxxxxxfaf6.mailgun.org
MAILGUN_SECRET=key-xxxxxxxxxxxxxxxxxxxxxxxxx

Upvotes: -1

Masud Miah
Masud Miah

Reputation: 256

In config/mail.php

set from property as:

'from' => ['address' => '[email protected]', 'name' => 'Anything you Want']

Upvotes: -1

Related Questions