[php] Trying to get Laravel 5 email to work

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' =>"[email protected]" , 'name' => "example"],
        'encryption' => 'tls',
        'username' => env('[email protected]'),
        '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('[email protected]', '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
[email protected]
MAIL_PASSWORD=MyPassword

This question is related to php laravel laravel-5 email-integration

The answer is


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


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


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!


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


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.


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

[email protected]

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' => '[email protected]' , 'name' => 'YourName' ],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

the problem will be solved by clearing cache

php artisan config:cache

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.


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.


  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.

My .env file configuration is like this for laravel 5.1

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
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.


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

[email protected]

  1. Run the downloaded file of MailHog
  2. Send Email
  3. Check sent email by going to localhost:8025

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.


I had the same problem as you, I just got it to work.

Firstly, you need to double check that the .env settings are set up correctly. Here are my settings:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourusername
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls

Please make sure that your password is not between quotes :).

And in config/mail.php it has the following, without the comments.

<?php

return [

'driver' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', '587'),
'from' => ['address' => 'yourusername', 'name' => 'yourname'],
'encryption' => env('MAIL_ENCRYPTION','tls'),
'username' => env('MAIL_USERNAME', '[email protected]'),
'password' => env('MAIL_PASSWORD', 'password'),
'sendmail' => '/usr/sbin/sendmail -bs',

'pretend' => false,

];

Hope it works for you :)


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.


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

Change this:

'username' => env('[email protected]'),
'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'));
});

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

php artisan config:clear

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-5

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory Can't install laravel installer via composer Including a css file in a blade template? No Application Encryption Key Has Been Specified How to Install Font Awesome in Laravel Mix Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes Laravel 5.4 redirection to custom url after login How to set the default value of an attribute on a Laravel model laravel 5.3 new Auth::routes()

Examples related to email-integration

Trying to get Laravel 5 email to work How to configure SMTP settings in web.config Can there be an apostrophe in an email address? Undefined symbols for architecture i386: _OBJC_CLASS_$_SKPSMTPMessage", referenced from: error Sending emails in Node.js? How to send 100,000 emails weekly? Is it possible to add an HTML link in the body of a MAILTO link