Gmail SMTP configuration for sending any email is pretty easy. You can send email to email recipients using Gmail. Laravel provides an API named Swift Mailer library. This API provides the driver for SMTP, Mailgun, Postmark, Amazon SES, etc. You can send an email by configuring any of these drivers. API based drivers are simpler and faster than the SMTP servers. It will require the Guzzle HTTP library that can be installed using the composer. You can configure the mail services in the Laravel configuration file. Today, in this post, I will gonna show you how you can send emails in Laravel 8 using Gmail SMTP. We will use the Gmail configuration.
Prerequisites
You will require the following tools before creating this web application. We will create a new project in Laravel 8. So, these tools must to have in your system.
- PHP >= 7.3
- MySQL (version > 5)
- Apache/Nginx Server
- VS Code Editor
- Composer
When your system is ready, then let’s create the Laravel 8 project for sending emails.
How to Send Email Using Mailgun in Laravel 8
Create Project to Send Email Using Gmail SMTP
We will create a new project using the composer. Hence, open the terminal or command prompt and start creating the project.
composer create-project --prefer-dist laravel/laravel send-email
It will take a couple of minutes to finish the project setup.
After the project setup, we will configure the Gmail account.
Configure Gmail SMTP Settings To Send Email
In this step, we will have to configure our Gmail account. Here, we will allow sending email through the configuration of Gmail SMTP settings. Also, to send email in Laravel 8 using the Gmail, we will have to create a password. Actually, we cannot use the actual password of the Google account to send email in Laravel 8. Hence, it will require an app password.
You will have to Login your Gmail account. Then go to the option Manage your Google Account inside the profile tab. Now, under the Security tab.
Firstly, under the security tab, you will have to turn ON to allow the less secure app access.
Secondly, you will have to enable the two step verification. This will be required for the security reasons. Actually, we have allowed the less secure app for our Gmail account.
At last, you will have to create an app password. For creating an app password, you will have to choose the app and the device.
After clicking on the Generate button, you will have a dialog box with the password. So, just copy it.
Now, come back to the Laravel 8 project.
Create Auth Using Jetstream and Intertia js in Laravel 8
Configure Gmail SMTP in Laravel 8
For the Gmail SMTP configuration in Laravel, navigate to the .env file. Replace the mailer settings from the below snippet. Here, we are using Gmail SMTP, so we will have to put the following settings for the Gmail –
- Mailer: smtp
- Host: smtp.gmail.com
- Post: 465/587
- Encryption: tls/ssl
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME={{YOUR_GMAIL_ADDRESS}}
MAIL_PASSWORD={{GENERATED_PASSWORD}}
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
For the security reasons, Gmail doesn’t allow you to put the regular password as open characters. Hence, that’s the reason, we have created an app password. So, that we will not require to use the regular password.
After this configuration our app has been linked with the Gmail account. Now, we are ready to send email in Laravel 8.
For sending any email in Laravel, we will have to create a Mail class. Laravel provides the Mail class to send emails. So, let’s create it.
Create Mail Class to Send Email in Laravel 8 Using Gmail SMTP
For creating a mail class, we will use the artisan command. So, inside the project directory just hit the below command in the terminal.
php artisan make:mail FirstEmail
Now, we have the mail class inside the app/Mail folder.
So, just open it, and just add a single line of code.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class FirstEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from("admin@programmingfields.com")->view('email-template');
}
}
Here, I have attached a template for the email. That means, I am passing a email template in which I will write the message. But, it is just for a demonstration purpose. You can pass this message dynamically from the database. So, in that case, the data will be passed from the controller to mail class. And then mail class to the View.
How to Create Auth with Jetstream and Livewire in Laravel 8
Create a View for Email Template
We will create a view with the name email-template.blade.php
. Now, add the below snippet. I have entered a custom message and this will be attached with the email.
<!doctype html>
<html lang="en">
<head>
<title>Send Email in Laravel 8 Using Gmail SMTP | Programming Fields</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xl-6 col-lg-6 col-sm-12 m-auto">
<h3> Send Email in Laravel 8 Using Gmail </h3>
<p> Hey, </p>
<p> Welcome to Programming Fields </p>
<p> This is a basic demo for sending email in Laravel 8 using Gmail SMTP </p>
<br/>
<br/>
<p> Best Regards</p>
<p> Team, Programming Fields </p>
</div>
</div>
</div>
</body>
</html>
Now, in the next step, we will have to create a controller. So, let’s create it.
Laravel 8 Ajax CRUD Application Tutorial For Beginners
Create a Controller
For calling the Mail class, we will need to have a controller. So, I will create a controller.
php artisan make:controller EmailController
The controller has been created. Let’s add the functionality.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\FirstEmail;
class EmailController extends Controller
{
public function sendEmail() {
$to_email = "mailaddress@mail.com";
Mail::to($to_email)->send(new FirstEmail);
return "<p> Success! Your E-mail has been sent.</p>";
}
}
At last, we will need to create a route for the above function.
Create Route For Sending Email
We will have to create a web route for sending the email. So, let’s add the below route in the web.php file.
<?php
use App\Http\Controllers\EmailController;
use Illuminate\Support\Facades\Route;
Route::get("send-email", [EmailController::class, "sendEmail"]);
Now, our application is ready to send email in Laravel 8. So, let’s test it.
Laravel 8 Multiple Images Upload with Validation
Test Application By Sending Email
Run the application on the port. Then hit the above URL that is specified in the route.
In the result, we got the success message after the email sent. Now, let’s check the Gmail for the confirmation whether you received or not.
Yeah, I have received the email so, it worked absolutely fine.
Conclusion
In this post, we configured Gmail SMTP for sending emails using the Laravel 8. For the Gmail SMTP Settings, you will have to allow a less secure app. Also, you will require to enable the two step verification. Then you can create the app password for the mail application. This will allow you to send an email from your Gmail account to any email address. The app password is must for sending the email. Google won’t allow you to put the regular password for the security reasons. So, that’s it for this post. You can implement it in your real applications for sending emails. For any kind of issue or help let me know in the comment section. Thank you.
Boss Pogs says
Hello I just follow your tutorial. I always stucked with this error “stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed”. I follow from step 1, gmail set up but always get that error. Please help.
Boss Pogs says
Hello. I followed the steps from step 1 setting the gmail until the last. I’m getting always this error and stuck with it “stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed”. Please help me. Thanks!
Umesh Rana says
You will have to set the keys
verify_peer
,verify_peer_name
, andallow_self_signed
to solve the error. So, add the below snippets in theconfig/mail.php
file.'stream' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
],
I hope this will work.
Boss Pogs says
It still didn’t work sir
Saul says
Hello, i have a question. What if i want to send the email from an external web page. I already have my web page up and running but i get a 550 5.7.1 relaying denied gmail error. Is there a way to do that?
Umesh Rana says
On which webpage you configured the SMTP settings? If you have the SMTP configured in the same project then you can send email from any webpage.
Tahsin says
Bless you