'How to to send mail using gmail in Laravel?
I try again and again to test sending an email from localhost but I still cannot. I don't know anymore how to do it. I try search to find solution but I cannot find one. I edited config/mail.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "ses", "log"
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => ['address' => '[email protected]', 'name' => 'Do not Reply'],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME'),
/*
|--------------------------------------------------------------------------
| SMTP Server Password
|--------------------------------------------------------------------------
|
| Here you may set the password required by your SMTP server to send out
| messages from your application. This will be given to the server on
| connection so that the application will be able to send messages.
|
*/
'password' => env('MAIL_PASSWORD'),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Mail "Pretend"
|--------------------------------------------------------------------------
|
| When this option is enabled, e-mail will not actually be sent over the
| web and will instead be written to your application's logs files so
| you may inspect the message. This is great for local development.
|
*/
'pretend' => false,
];
`
and I edited .env
file like this already:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
Solution 1:[1]
first login to your gmail account and under My account > Sign In And Security > Sign In to google
, enable two step verification
, then you can generate app password
, and you can use that app password in .env
file.
Your .env
file will then look something like this
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls
Don't forget to run php artisan config:cache
after you make changes in your .env
file.
Solution 2:[2]
Try using sendmail instead of smtp driver (according to these recommendations: http://code.tutsplus.com/tutorials/sending-emails-with-laravel-4-gmail--net-36105)
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls
Solution 3:[3]
All you have to do is just edit in you.env file, that's it.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=<your_email_address>
MAIL_PASSWORD=<your_gmail_app_password_>
MAIL_ENCRYPTION=ssl
for app password goto https://support.google.com/accounts/answer/185833?hl=en
and genearate your app pasword and save for future use. because once you generate app password you cannot re-edit password or change same app password.(you can create multiple app password)
Solution 4:[4]
This is working sample that i have tried :
Open your mail.php
under config
folder then fill with this option :
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' =>'[email protected]', 'name' => 'Email_Subject'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME','[email protected]'),
'password' => env('MAIL_PASSWORD','youremailpassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
Open your .env
file under root
project. Also edit this file following above
option such
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremailusername
MAIL_PASSWORD=youremailpassword
MAIL_ENCRYPTION=tls
After that clear your config by running this command
php artisan config:cache
Restart your local server
Try visit your route with controller contains mail function at first
time it still error Authentication Required
. You need to login via
your gmail account to authorize untrusted connection. Visit this link
to authorize
Solution 5:[5]
if you still could be able to send mail after setting all configs right and get forbidden or timeout errors you could set the allow less secure apps to access your account
in gmail. you can follow how to here
Solution 6:[6]
Note: Laravel 7 replaced MAIL_DRIVER by MAIL_MAILER
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourgmailaddress
MAIL_PASSWORD=yourgmailpassword
MAIL_ENCRYPTION=tls
Allow less secure apps from "Google Account" - https://myaccount.google.com/ - Settings - Less secure app access (Turn On)
Flush cache config:
php artisan config:cache
For Apache:
sudo service apache2 restart
Solution 7:[7]
in bluehost i could not reset password; with this driver worked:
MAIL_DRIVER=sendmail
Solution 8:[8]
you need to enable first the App password of your google account -> security section link
then the app password that will be generated copy it and paste it in to .env file
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=app_password
MAIL_ENCRYPTION=ssl
Solution 9:[9]
your MAIL_PASSWORD=must a APPpasword
after change the .env stop the server then clear configuratios cahce php artisan config:cahce and start the server again
reference Cannot send message without a sender address in laravel 5.2 I have set .env and mail.php both
Solution 10:[10]
If you're developing on an XAMPP, then you'll need an SMTP service to send the email. Try using a MailGun account. It's free and easy to use.
Solution 11:[11]
The problem for me is that for some reason the username/password came NULL from mail config. To check that before sending a email check with the follow code:
dd(Config::get('mail'));
If your username/password came nulled just set with:
Config::set('mail.username', 'yourusername');
Config::set('mail.password', 'yourpassword');
Solution 12:[12]
If you are using email password then you should replace it with app password.for setting APP password you need to enable the 2 step authentication before setting password which can be disabled later.
Also make sure that you have allowed less secure app in setting section.For additional info you can follow how to here
Solution 13:[13]
Working for me after trying various combinations.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=passowrd
MAIL_ENCRYPTION=ssl
It is necessary to generate application password https://myaccount.google.com/security and us it as MAIL_PASSWORD
environment variable.
I found about about this by checking error code from google server, which was use-full and lead me to this webpage.
Solution 14:[14]
You can also try adding to laravel these 2 lines:
[email protected]
MAIL_FROM_NAME="[email protected]"
After this run this comand to flush mail cache config:
php artisan config:cache
Solution 15:[15]
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME='Name'
allow less secure apps to access your account in the Gmail security setting.
Solution 16:[16]
These configuration works for me you use port 25,465,587
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls
Solution 17:[17]
Just change MAIL_ENCRYPTION=null to MAIL_ENCRYPTION=tls
And run this command "php artisan config:cache"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow