Sending Email in Laravel using Gmail SMTP Server

Sohail · · 462 Views

In this post, we talk about how to send email using the Gmail SMTP server in Laravel. Well, there's no uncertainty the need to send emails in your web application will emerge, and you need to know how to get this done, properly? Not to worry, in this article, you will learn how easy it is to set up email in your Laravel application using Gmail SMTP Server! One more advantage of utilizing an SMTP server is, you can send emails from your local server also.

Setup Of Gmail SMTP Server In Laravel

Laravel uses config/mail.php file for storing configuration to sending emails. This file contains configuration options like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, and so forth. We need to provide this config data for sending emails.

To add these configurations, we don't have to edit config/mail.php. We should store these values in the .env file.

Open your .env file, and update the mail configuration details as follows.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=ENTER_YOUR_GMAIL_USERNAME
MAIL_PASSWORD=ENTER_YOUR_GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl

Here, we set the driver as smtp, host for Gmail as smtp.googlemail.com, SMTP port for Gmail as 465, and encryption method to ssl. Ensure you have replaced your Gmail username and password.

Configure your Google Account

To utilize the Gmail SMTP server, you need to change a few settings on your Google account. So, log in to your Google account and click on 'Account.' When you are on the 'Account' page, click on 'Security.' Scroll down to the bottom, and you will discover 'Less secure app access' settings. Set this to ON.

Sending Email in Laravel using Gmail SMTP Server

Send Emails from your Laravel Application

Now, all the basic setup has been finished. We would now be able to write some codes in Laravel to send an email.

For this tutorial, I am going to use Laravel's 'Mail' class to send the email. 

Here is an example code:

$to_name = 'TO_NAME';
$to_email = 'TO_EMAIL_ADDRESS';
$data = array('name'=>"Sam Jose", "body" => "Test mail");
    
Mail::send('emails.mail', $data, function($message) use ($to_name, $to_email) {
    $message->to($to_email, $to_name)
            ->subject('Artisans Web Testing Mail');
    $message->from('FROM_EMAIL_ADDRESS','Artisans Web');
});

In the above code, we are using our mail template as 'emails.mail' file. Hence we need to create an 'emails' folder and the mail.blade.php file at resources\views\emails\mail.blade.php

That is it! Now, in the background Laravel will automatically utilize the Gmail SMTP server and send your emails.

0

Please login or create new account to add your comment.

0 comments
You may also like:

What are Laravel Macros and How to Extending Laravel’s Core Classes using Macros with example?

Laravel Macros are a great way of expanding Laravel's core macroable classes and add additional functionality needed for your application. In simple word, Laravel Macro is an (...)
Harish Kumar

Install Laravel Valet Linux+ development environment on Ubuntu System

The official Laravel Valet development environment is great if you are an Apple user. But there is no official Valet for Linux or Window system.
Harish Kumar

Laravel Sanctum API Token Authentication Tutorial with example

Laravel Sanctum is a popular package for API Token Authentication. There are many other packages available to authenticate the APIs request in Laravel. For example, We are already (...)
Harish Kumar

Create SPA authentication Using Laravel Sanctum and Vue.js

In this guide, we will focus on SPA authentication in a simple Vue.js app using Laravel Sanctum. Laravel Sanctum provides a featherweight authentication system for SPAs (single (...)
Harish Kumar

Create API Authentication with Laravel Passport

In this article, we'll see how to implement restful API authentication using Laravel Passport. You should have experience working with Laravel as this is not an introductory tutorial. (...)
Sumit Talwar

Laravel Themer: multi-theme support for Laravel application

This Laravel Themer package adds multi-theme support to your application. This theme package improves any application while allowing the freedom to organize and maintain your app's (...)
Harish Kumar