How to send an Email in Laravel using Gmail SMTP Server


Having problems sending an email in Laravel using the default SMTP settings? The PHP mail(0) send function failed you as a result of some server configurations and settings?

The purpose of this tutorial is to explain how to use Laravel to send email messages using your Gmail account as your SMTP server with the default SMTP settings for Laravel.

It should be noted that using an SMTP server has many benefits. With an SMTP server you can send email from your own local server, allowing you to test email functionality on your own server. Furthermore, emails sent via SMTP are not flagged as SPAM by Gmail.

To get started, we follow the following simple steps:

Step 1: Configure Gmail SMTP Server in Laravel Application

In Laravel, the mail.php file is used for storing details related to mail sending. The file contains settings such as MAIL_HOST, MAIL_DRIVER, etc. We need to provide this information in order to send an email successfully.
This information does not need to be added to this config/mail.php file, rather, it can be added to the .env file accordingly.
To check these settings, open your .env file, which is located in your root directory of our application:


Now edit the details above as follows.


As you can see, MAIL_DRIVER is set up to be SMTPS, MAIL_HOST to be, MAIL_PORT to be 465, and MAIL_ENCRYPTION method is SSL.
MAIL_USERNAME and PASSWORD should be replaced with your Gmail email address and password respectively.
Our Google account needs to be adjusted to give access to less protected applications given that we are using Gmail SMTP.

Step 2: Configure your Google Account

Click on the Google Account button after you log into your Google Account. As shown, this button is displayed when you click on your profile picture in your Gmail Dashboard.

 You can access this setting once you are on the My Account page under Security, by scrolling down to the bottom of the page. By clicking on the radio button, you will turn it on.

Step 3: Send Emails from your Laravel Application

We have completed the basic setup at this point. In order to send an email, we need to use some Laravel PHP code.
Get started by creating any controller where mail sending logic is handled, and then using the code snippet below as a guide, write your codes.

$to_name = ‘RECEIVER_NAME’;
$data = array(‘name’=>”Ogbonna Vitalis(sender_name)”, “body” => “A test mail”);Mail::send(‘emails.mail’, $data, function($message) use ($to_name, $to_email) {$message->to($to_email, $to_name)->subject(Laravel Test Mail’);$message->from(‘SENDER_EMAIL_ADDRESS’,’Test Mail’);});

The above code uses the ’emails.mail‘ file to store our email template. Therefore, we need to create a folder named ’emails’ and a file named mail.blade.php located at resourcesviewsemailsmail.blade.php
We’re done! Simply create any route you want and start sending emails from your Laravel application.

Hello <strong>{{ $name }}</strong>,

Hurray, we are done, simply create any route of your choice to and start sending mails from your Laravel application.



Post a Comment (0)
Previous Post Next Post