Trying to get Laravel 5 email to work

2019-01-06 09:17发布

I'm trying to send an email to a specified user by typing in the URL, but I'm getting the following error:

Swift_TransportException in AbstractSmtpTransport.php line 383: Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required

So far I'm just trying to get it to work with Gmail. How can I get this to work?

This is what I have so far: mail.php

<?php
    return [
        'driver' => env('MAIL_DRIVER',' smtp'),
        'host' => env('MAIL_HOST', 'smtp.gmail.com'),
        'port' => env('MAIL_PORT', 587),
        'from' => ['address' =>"MyUsername@gmail.com" , 'name' => "example"],
        'encryption' => 'tls',
        'username' => env('MyUsername@gmail.com'),
        'password' => env('MyPassword'),
        'sendmail' => '/usr/sbin/sendmail -bs',
        'pretend' => false,
    ];

This is what I have in the routes:

Route::get('test', function() {
    Mail::send('Email.test', [], function ($message) {
        $message->to('example@gmail.com', 'HisName')->subject('Welcome!');
    });
});

This is what I have in my controller:

class MailController extends Controller
{
    public function Sending_Email()
    {
        $this->call('GET','Email.test');
        return View('Email.test');
    }
}

And this is what is in my .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=MyUsername@gmail.com
MAIL_PASSWORD=MyPassword

18条回答
太酷不给撩
2楼-- · 2019-01-06 09:31

You are getting an authentication error because the username and password in your config file is setup wrong.

Change this:

'username' => env('MyUsername@gmail.com'),
'password' => env('MyPassword'),

To this:

'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),

The env method checks your .env file. In your .env file you call these MAIL_USERNAME, so that's what you need to pass to the env method.

One troubleshooting tip: add dd(Config::get('mail')); so that you can see the actual generated config. This will help you spot issues like this, and know exactly what information Laravel is going to try and use. So you may want to stop that in your test route temporarily to examine what you have:

Route::get('test', function()
{
    dd(Config::get('mail'));
});
查看更多
相关推荐>>
3楼-- · 2019-01-06 09:32

It happens to me also.

It is because when we edit the .env file we need to restart the server. Just stop the current php artisan serve and start it once again. This will work for sure.

If still not worked try doing php artisan config:cache

查看更多
欢心
4楼-- · 2019-01-06 09:35

For development purpose https://mailtrap.io/ provides you with all the settings that needs to be added in .env file. Eg:

Host:   mailtrap.io
Port:   25 or 465 or 2525
Username:   cb1d1475bc6cce
Password:   7a330479c15f99
Auth:   PLAIN, LOGIN and CRAM-MD5
TLS:    Optional

Otherwise for implementation purpose you can get the smtp credentials to be added in .env file from the mail (like gmail n all)

After addition make sure to restart the server

查看更多
在下西门庆
5楼-- · 2019-01-06 09:35

If your results from dd(Config::get('mail')); are null simply use the command

php artisan config:clear
查看更多
家丑人穷心不美
6楼-- · 2019-01-06 09:36

Finally I got! Just to share, there are more config than just .env

This is .env

MAIL_DRIVER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=587

MAIL_USERNAME=UserName@gmail.com

MAIL_PASSWORD=********

MAIL_ENCRYPTION=tls

config/mail.php also need to input address & name for it to work.

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'Username@gmail.com' , 'name' => 'YourName' ],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
查看更多
神经病院院长
7楼-- · 2019-01-06 09:36

i've had the same problem; my 'MAIL_ENCRYPTION' was 'tls' on mail.php but it was 'null' on .env file so i changed 'null' to 'tls' and it worked!

查看更多
登录 后发表回答