156

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
Advaith
  • 2,490
  • 3
  • 21
  • 36
Vantheman6
  • 1,719
  • 3
  • 13
  • 13

18 Answers18

248

I know it's working for you now @Vantheman6 but this is what worked for me in case it's the same for someone else.

I added to my .env file the details of the mail service I am using. So make sure the following details

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

in the .env file are accurate.

NOTE: Don't forget to restart your server after editing the .env file so it will pick the new data that you put in there.

Clear config cache with below command:

php artisan config:cache

If you don't restart your server, the .env file will still continue to present the old mail data to the app even though you have made changes that can cause this error.

Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
Osei-Bonsu Christian
  • 2,935
  • 1
  • 15
  • 8
  • 59
    Wow, that 'Note' saved me. I didn't know ENV vars were cached at startup! – Gerben Jacobs Mar 29 '15 at 14:24
  • Could you update the answer - and highlight the restart server, instead of the configuration file? – mrwaim May 21 '15 at 14:42
  • 2
    @mrwaim, I thought the restarting of the server is exactly what I specified in the note, unless I'm not getting what you are implying. – Osei-Bonsu Christian May 21 '15 at 18:32
  • 1
    I would suggest removing everything before "Note:" - what helped was the note, and not the things before. I would suggest, if you want to keep the other parts, put them after the note. 3 People upvoted the 'Note' comment so the Note probably helped them too. – mrwaim May 22 '15 at 00:06
  • Hi @Osei-BonsuChristian, I'm using your exact .env aside from username and password, but I get an error that is similar but without Authentication required I have "Must issue a STARTTLS command first": Swift_TransportException in AbstractSmtpTransport.php line 383: Expected response code 250 but got code "530", with message "530 5.7.0 Must issue a STARTTLS command first. cz1sm20994322pbc.84 - gsmtp. Any idea what this error is referring too? I've halted and restarted homestead to make sure my .env changes are picked up. – mtpultz Jun 23 '15 at 00:02
  • Also @Osei-BonsuChristian, I set encryption in mail.php to 'tls' and the error changed to: ErrorException in StreamBuffer.php line 95: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed – mtpultz Jun 23 '15 at 00:04
  • @mtpultz: Got any solution for this problem? – user1012181 Aug 07 '15 at 15:55
  • 22
    If you have previously cached your configuration with `php artisan config:cache` then restarting the server will not pick up the updated configuration. Try running `php artisan config:cache` again – darronz Oct 06 '15 at 12:53
  • Hello, I have followed the same procedure as you said, But I am getting the following message after 30 sec of loading that page: `FatalErrorException in HandleExceptions.php line 56: Maximum execution time of 30 seconds exceeded` – Hola Apr 02 '16 at 10:29
  • Are mail servers free? – Black Mar 01 '17 at 18:57
  • @Osei-BonsuChristian After lots of headache I saw the 'note' here about restarting the server, which worked. Thanks! It might be worth highlighting in bold. – Bryan Mar 05 '17 at 00:45
  • You also want to set MAIL_FROM_ADDRESS , many smtp servers do not allow to send email from a different address than the one you've logged in with. (If you don't set it, it will default to something like hello@example.com as sender) – jave.web Feb 17 '19 at 17:07
  • And what would be the best configuration for multiple clients with different databases and information? – Mike Otharan Jul 09 '19 at 16:05
54

My .env file configuration is like this for laravel 5.1

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example@gmail.com
MAIL_PASSWORD=****************
MAIL_ENCRYPTION=tls

here most important thing is that I created gmail application specific password(16 digit).

I need to reveal one more thing since no luck for any of configuration. That is, whenever I changed .env file need to run this command

php artisan config:cache

And this is most most most important because without this command laravel executes previous settings from it's cache. It's required me more than 10 hours to figure out.

Amir
  • 8,821
  • 7
  • 44
  • 48
36

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'));
});
jszobody
  • 28,495
  • 6
  • 61
  • 72
  • So I changed my mail.php file to reflect your suggestions` 'username' => env('MAIL_USERNAME'),` and `'password' => env('MAIL_PASSWORD'),` Which I find interesting because everywhere tells me to input your email address and password for that. I'm still getting an error `Swift_TransportException in AbstractSmtpTransport.php line 383: Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required. I'm sorry but where do i actually place the "` `dd(Config::get('mail'));`. the Config directory contains a lot of files – Vantheman6 Mar 17 '15 at 14:28
  • See my updated answer, with an example of how to examine your config. – jszobody Mar 17 '15 at 14:31
  • that's awesome i'm still getting an error, but i can actually see what it is doing. For some reason it's trying to pass ` "host" => "mailtrap.io"` to the env even though in mail php i'm saying `'host' => env('MAIL_HOST', 'smtp.gmail.com'),` – Vantheman6 Mar 17 '15 at 14:56
  • Double check the `MAIL_HOST` in your .env file – jszobody Mar 17 '15 at 15:07
  • It's accurate. `MAIL_HOST=smtp.gmail.com` So what I ended up doing was removing all the env from each part of the mail.php file. And now the correct information is passing through. OMG Thank you it worked. You are the man – Vantheman6 Mar 17 '15 at 15:15
  • Perhaps you had it twice in the .env file? The .env.example file that ships with laravel uses `mailtrap.io`. So _somewhere_ in there it was still hanging around. Avoiding env and putting all values directly in config is certainly one way to resolve it, glad to help. =) – jszobody Mar 17 '15 at 15:16
15

the problem will be solved by clearing cache

php artisan config:cache
zainudin noori
  • 191
  • 4
  • 12
13

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,
yaksowmin
  • 141
  • 1
  • 4
11

If you ever have the same trouble and try everything online and it doesn't work, it is probably the config cache file that is sending the wrong information. You can find it in bootstrap/cache/config.php. Make sure the credentials are right in there. It took me a week to figure that out. I hope this will help someone someday.

Nabil
  • 186
  • 1
  • 8
  • You Sir, deserved a nice Bordeaux, thanks a lot! I noticed that username and password were empty in that file even tough I filled them in the two appropriate files.From there, I knew what to do! Merci ! – Gregordy Apr 19 '16 at 11:09
11

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

Bhupesh Shrestha
  • 248
  • 3
  • 17
8

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

Advaith
  • 2,490
  • 3
  • 21
  • 36
5

For future reference to people who come here. after you run the command that was given in the third answer here(I am using now Laravel 5.3).

php artisan config:cache

You may encounter this problem:

[ReflectionException]
Class view does not exist

In that case, you need to add it back manually to your provider list under the app.php file. GO-TO: app->config->app.php->'providers[]' and add it, like so:

Illuminate\View\ViewServiceProvider::class,

Hope That helps someone.

GabMic
  • 1,422
  • 1
  • 20
  • 37
4

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!

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
3

That simply means that your server does not have access to the SMTP Server.

You can test this by doing:

telnet <smtpServer> <smtpPort>

You should get the Access denied error.

The solution is to just use another SMTP server that can be accessed by your server.

Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32
stidiovip
  • 110
  • 5
3

None of the above solutions worked for me on localhost. I even allowed access from less secure apps, allowed access through display unlock captcha and set the verify peer and verify peer name to false for SSL.

Eventually, I used the open source SMTP testing solution of MailHog. The steps are as follows:

  1. Download the latest version of MailHog for your OS
  2. Specify the following settings in your .env file

MAIL_DRIVER=smtp

MAIL_HOST=127.0.0.1

MAIL_PORT=1025

MAIL_USERNAME=testuser

MAIL_PASSWORD=testpwd

MAIL_ENCRYPTION=null

MAIL_FROM_ADDRESS=theaddress@youwant.com

  1. Run the downloaded file of MailHog
  2. Send Email
  3. Check sent email by going to localhost:8025
ZerosAndOnes
  • 1,083
  • 1
  • 13
  • 25
3

You should restart the server and run this commands:

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan config:clear
php artisan config:cache

That should work.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
0

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

php artisan config:clear
Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32
0

My opinion after making changes on your .env files restart your server and serve the app again. Just to be sure of the actual error. The php artisan clear and cache afterwards works pretty fine.

0

For my case the issue was that the email that I was using from('mymail@gmail.com') differed from the one I had configured in the .env file as the MAIL_USERNAME.

Below is a snippet of the mail configuration in the .env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=anothermail@gmail.com
MAIL_PASSWORD=strongpassword
MAIL_ENCRYPTION=tls

in the mail sending method, I had set

->from('mymail@gmail.com')->to('myfriend@gmail.com');

Always ensure that the mail used in the

->from('mymail@gmail')

is the same as the one configured in the .env MAIL_USERNAME.

After modifying your .env ensure to restart your server in the event you are using laravel 7 and below as they do not have automatic restart when the .env file is modified.

If you have done all of the above and have not yet solved try to run these below commands to clear any cached configuration from your system :

php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan config:clear
stanley mbote
  • 956
  • 1
  • 7
  • 17
-1

In my case: Restart the server and run php artisan config:clear command.

Duy Nguyen
  • 334
  • 4
  • 10
-3
  1. You go to the Mailgun
  2. Click Authorized Recipients
  3. Add the email address you wish send mail to.
  4. Verify the message sent to the mail.
  5. Bravo!...You're good to go.
  • 2
    Please forgive my ignorance. How does the answer above solve the Laravel5 email problems in the question? – jww Nov 12 '16 at 06:17