Use Gmail as default SMTP in Lumen
Hello friends! In this article I am going to show how to use Gmail as your default SMTP mail in Lumen project. And surprisingly Gmail not only can use in production, but can be use to test your email sending and receiving too.
Generally, most people are using common email testing services such as Mailtrap to the Lumen project. So when you are developing a project for a client, Most people have faced the issue on email testing process. Sometime client cannot check whether emails are communicating correctly. Developer have to create an account on those and have to change credentials and provide to client. Not only that sometimes it need to give a session about how to use those test mail servers too.
As this is a common issue for most developers. I will show you how to use Gmail as your default testing or product mail communication method in Lumen project. So lets get started.
Follow these steps
First of all you have to edit the .env file in your Lumen project. Open the .env file and modify Mail settings as given below
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=yourgmail
MAIL_PASSWORD=yourapp_password
MAIL_FROM_ADDRESS=support@yourwebsite.lk
MAIL_FROM_NAME=yourwebsitename
MAIL_ENCRYPTION=ssl
Note – You cannot use your default Gmail password to mail password. You have to generate new app password
So you have t create a app password in order to use the Google SMTP mail now. Follow these steps.
- Go to your Google Account.
- On the left navigation panel, choose Security.
- On the ‘Signing in to Google’ panel Turn on 2 step verification
- After enabled go to ‘Signing in to Google’ panel again, and you will see App password section have been appeared
- Then choose App passwords.
- At the bottom, choose Select app and choose the app that you’re using.
- Choose Select device and choose the device that you’re using.
- Choose Generate.
- Paste the app password at .env MAIL_PASSWORD field
Example code for send mail in Lumen
When you use the ‘Mail’ class your code will be something like as written below.
In the Controller
$to_mail_name = 'TO_MAIL_NAME';
$to_mail_address = 'TO_MAIL_ADDRESS';
$maildata = array('name'=>"sample name", "body" => "nsrtech sample mail");
Mail::send('emails.sendmail', $maildata, function($message) use ($to_mail_name, $to_mail_address ) {
$message->to($to_mail_address , $to_mail_name)
->subject('Test Mail From Nsrtech');
$message->from('FROM_MAIL_ADDRESS','NSR Tech');
});
In the above code, I am using the view ’emails.sendmail’. It means you have to create a folder and file as resources->views->emails->sendmail.blade.php.
Your sendmail.blade.php will contain code as follows.
Hi <strong>{{ $name }}</strong>,
<p>{{ $body }}</p>
If you need more about Lumen framework go to following link
That’s it! Now, in the background Lumen will automatically use the Gmail SMTP server and send your emails. f you have any questions please comment below. Or if you need to check the latest post, Please go to following link. Have a nice day!!