Send Email via SMTP Server in Laravel using PHPMailer

Send Email SMTP Server in Laravel using PHPMailer – Vkprogramming

Generally, we used the default mail() function to send emails in the PHP-based web application. But, the PHP mail() function sometimes fails to deliver email or deliver mail to the spam folder. Due to this, the SMTP server is always recommended to send emails from the script in the web application. The PHPMailer library can be used to send an email with an SMTP server using PHP. You can integrate the PHPMailer library to send emails from any PHP frameworks (CodeIgniter, Laravel, Lumen, etc.).

The PHPMailer library provides an easy way to send email via an SMTP server using PHP. If your application is built with Laravel, the PHPMailer library can be used to send an email to the SMTP server. In this tutorial, we will show you how to send email via an SMTP server in Laravel using PHPMailer.


Send email via SMTP server in Laravel

Run the following command to install PHPMailer via Composer in your Laravel application.

composer require phpmailer/phpmailer

Load the PHPMailer and Exception classes.

use PHPMailerPHPMailerPHPMailer;

use PHPMailerPHPMailerException;


Initialize the PHPMailer class, configure SMTP and email.

// Create an instance of PHPMailer class 

$mail = new PHPMailer;

// SMTP configurations


$mail->Host        = ‘’;

$mail->SMTPAuth = true;

$mail->Username = ‘’;

$mail->Password = ‘******’;

$mail->SMTPSecure = ‘tls’;

$mail->Port        = 587;


// Sender info 

$mail->setFrom(‘’, ‘SenderName’);

// Add a recipient 


// Add cc or bcc  



// Email subject 

$mail->Subject = ‘Send Email via SMTP in Laravel’; 

// Set email format to HTML 


// Email body content 

$mailContent = ‘ 

    <h2>Send HTML Email using SMTP Server in Laravel</h2> 

    <p>It is a test email by Vkprogramming, sent via SMTP server with PHPMailer in Laravel.</p> 

    <p>Read the tutorial and download this script from <a href=””>Vkprogramming</a>.</p>’; 

$mail->Body = $mailContent; 

// Send email 


    echo ‘Message could not be sent. Mailer Error: ‘.$mail->ErrorInfo; 


    echo ‘Message has been sent.’; 


Send email via Gmail SMTP server in Laravel

To use Gmail SMTP for sending emails in Laravel, some settings need to be changed in the Google account. Follow the below steps to use Gmail SMTP with the PHPMailer library in Laravel.

  1. Login to your Google account. Go to the My Account page.
  2. Navigate to the Security page and scroll down to the Signing into Google section » Turn Off the 2-Step Verification
  3. Scroll down the Less secure app access section and turn On Less secure app access.


You are done! Now you can use Gmail SMTP to send emails from the Laravel application.

Specify your Gmail account credentials (email address and password), SMTP host, and port to send email using Gmail SMTP in Laravel.


// SMTP configuration


$mail->Host     = ‘’;

$mail->SMTPAuth = true;

$mail->Username = ‘’;

$mail->Password = ‘******’;

$mail->SMTPSecure = ‘tls’;

$mail->Port     = 587;



This example code helps you to send text or HTML emails from the Laravel application using the Laravel Mail service with the SMTP server. Not only Laravel but this code can be used to send emails from Lumen also. There are various configurations are available in PHPMailer to customize the email functionality as per your needs.



Related searches

send email in background laravel how to send an automatic email in laravel laravel send mail without SMTP laravel phpmailer laravel 8 send an email example send mail in laravel 8 using SMTP send email to laravel 7 send mail using SMTP in PHP example download.

Scroll to Top