Mail from not displaying the correct email

Published 6 months ago by jonjie

Hi Guys. I've setup my .env file with a different email from using Mail::send(), so basically, What I have is:

.env

[email protected]

EvaluationController

Mail::send(...., function($m){
    $m->from('[email protected]', 'jonjie');
});

But the email displaying is still the gmail one. Why is that? How can I solve it?

bobbybouwmann

Your code looks fine to me. Are you sure you're not setting the email somewhere else as well?

jonjie

Hi @bobbybouwmann .. Thanks for your reply. But, Yes, I'm really sure.

jonjie

I've also tried to edit the mail.php in config folder but still the same

jonjie

I noticed that the from when you received the email is the username in .env file. What if I have a different form and I want it to have a different from?

bobbybouwmann

Well you need to set the from in the mail.php file under the key MAIL_FROM_ADDRESS that can be set in your .env file. It's not MAIL_USERNAME!

jonjie

Hi @bobbybouwmann . MAIL_FROM_ADDRESS is not available in .env file. BTW I'm using laravel v5.1. And what if I have a different form and I want the from to be different, is that possible?

Cronix
Cronix
6 months ago (790,130 XP)

@jonjie Yeah, there isn't a MAIL_FROM_ADDRESSin laravel 5.1 (which is 3 years old now!) but it's there in more recent versions. Thanks for clarifying the version you're using as the answers could change. You could always add it in and reference the value in /config/mail.php like the other values from .env are referenced in config files.

Check the /config/mail.php for this:

    'from' => ['address' => null, 'name' => null],

and fill them out.

Also the question about changing from dynamically is covered in the docs!

Mail::send('emails.reminder', ['user' => $user], function ($m) use ($user) {
    $m->from('[email protected]', 'Your Application');

    $m->to($user->email, $user->name)->subject('Your Reminder!');
});

https://laravel.com/docs/5.1/mail#sending-mail

Also run artisan config:clear in case your config files are cached or it won't know about changes if they are.

jonjie

Hi @Cronix . Thanks for your answer. However, if I use [email protected] in .env file and [email protected] in my controller, will this work? Also, is that artisan config:clear or php artisan config:clear? I've read some of the questions about this command, and some say that after they run the command, their system brokes or getting errors.

Cronix
Cronix
6 months ago (790,130 XP)

If you override the ->from(), then it doesn't matter whether it's set in .env or not. It should just use what you tell it when using ->from(), and if you don't explicitly set ->from(), it should use the value from /config/mail.php

Technically php artisan config:clear but I have an alias for it so I don't have to keep typing php when I want to run it.

It shouldn't break anything by running it. If you are caching the config, then it will no longer read anything from .env, so making changes to .env get ignored. That's why I suggested clearing it. Really caching the config should only be done on production, but I don't know if you're using it or not so I suggested to clear it. It shouldn't hurt anything at all.

jonjie

Hi @Cronix . Thanks. One more thing, is it possible to use [email protected] even if I set the .env file to [email protected]? I mean with different domains? Thanks again.

Cronix
Cronix
6 months ago (790,130 XP)

Don't use an email address in From that isn't using the actual domain it's being sent from. It will most likely go to spam or not arrive at all, depending on the ISP that is receiving it.

If your domain is abc.com, email must be sent using [email protected] in the From field. It doesn't matter what "someone" is, but it absolutely matters that it's abc.com

But...it doesn't matter what's in .env if you set it using ->from(). It won't even look at the .env value because you explicitly set a ->from(). If you don't use a ->from(), then it will default to the From value that's in .env.

jonjie

Great! I'll try it now. Please wait for my response about the result. Thanks a lot!

Please sign in or create an account to participate in this conversation.