Ocdang85
Ocdang85

Reputation: 13

"From Email" doesn't work in laravel 5.3

This is my code:

Mail::send('view',$dataView, function($message) use ($user)
{
    $message->from('[email protected]', 'Myname');
    $message->subject('This is title');
    $message->to([email protected], $user->user_username);
});

It works! But When I check [email protected] then I see "from email" which I config in .env ( MAIL_USERNAME ), it isn't "from email" in code ([email protected]), How to I can change it to [email protected]? Thanks and sorry about my english.

Upvotes: 1

Views: 311

Answers (3)

shrekuu
shrekuu

Reputation: 2282

In config/mail.php, around line 58, try changing:

'from' => [
    'address' => '[email protected]',
    'name' => 'Example',
],

to:

'from' => [
    'address' => env('MAIL_USERNAME'),
    'name'    => env('MAIL_USERNAME')
],

Upvotes: 1

imrealashu
imrealashu

Reputation: 5099

I guess you're misunderstanding the MAIL_USERNAME which is in your .env file.
So basically, Laravel provides simple solution to send emails via dynamic senders. Lets say you've signed up for MailGun or SendGrid to send mails and MAIL_USERNAME is the username for your mail provider not your sender mail address. (Not Gmail, as Gmail doesn't support dynamic senders. Its good if you're testing your mails.). so, you're .env will look like this.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgridUsername
MAIL_PASSWORD=sendgridPassword
MAIL_ENCRYPTION=tls

Now, you're eligible to send mails using sendgrid. Now Lets say your domain is example.com so you can use [email protected] support@example or any email address with your domain to send emails.

To Receive messages either you use webmail or Gmail.

Hope this helps.

Upvotes: 0

Nour
Nour

Reputation: 1497

You are trying to send emails via dynamic senders which is not allowed by Gmail for preventing spamming mails, so automatically Gmail will change your sender address to your default address.

Upvotes: 0

Related Questions