dengdeng
1 year ago

Laravel 5.4 mail global from address not work

Posted 1 year ago by dengdeng

Hello, I am facing a problem about laravel 5.4 mail global from address config. I have a mailable class ResetAdminPassword, and the build method:

  /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail.reset_admin_pass');
    }

In my .env file, I defined the from address:

MAIL_USERNAME=w*****@163.com
MAIL_FROM_ADDRESS=w*****@163.com

The username and from_address are the same. According to the doc:

Using A Global from Address

However, if your application uses the same "from" address for all of its emails, it can become cumbersome to call the from method in each mailable class you generate. Instead, you may specify a global "from" address in your config/mail.php configuration file. This address will be used if no other "from" address is specified within the mailable class:

'from' => ['address' => '[email protected]', 'name' => 'App Name'],

I didn't specify from method in my build method.But when I test send mail, I got the wrong message 553 Mail from must equal authorized user. Then I update my build method:

  /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('w*****@163.com')->view('mail.reset_admin_pass');
    }

And the mail send succeed. I tested another way, I changed my config/mail.php file from array like this:

'from' => [
        'address' => 'w*****@163.com',
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

And in build method, I didn't use from method:

  /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('mail.reset_admin_pass');
    }

It also worked. So I guess that the application maybe didn't get MAIL_FROM_ADDRESS env variable. So I defined a route:

Route::get('test',function(){
    return env('MAIL_FROM_ADDRESS');
})

I can get the value:w*****@163.com. So I am confused now. Someone can help me? Thanks.

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