Send Email via SMTP Server in Laravel using PHPMailer

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 email from the script in the web application. The PHPMailer library can be used to send email with an SMTP server using PHP. You can integrate the PHPMailer library to send email 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 built with Laravel, the PHPMailer library can be used to send an email with SMTP server. In this tutorial, we will show you how to send email via 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 PHPMailer\PHPMailer\PHPMailer
use 
PHPMailer\PHPMailer\Exception;

Initialize the PHPMailer class, configure SMTP and email.

// Create an instance of PHPMailer class  
$mail = new PHPMailer;

// SMTP configurations
$mail->isSMTP();
$mail->Host        'smtp.example.com';
$mail->SMTPAuth true;
$mail->Username 'user@example.com';
$mail->Password '******';
$mail->SMTPSecure 'tls';
$mail->Port        587;

// Sender info 
$mail->setFrom('sender@example.com''SenderName');

// Add a recipient 
$mail->addAddress('recipient@example.com'); 

// Add cc or bcc  
$mail->addCC('cc@example.com'); 
$mail->addBCC('bcc@example.com'); 
 
// Email subject 
$mail->Subject 'Send Email via SMTP in Laravel'
 
// Set email format to HTML 
$mail->isHTML(true); 
 
// Email body content 
$mailContent 
    <h2>Send HTML Email using SMTP Server in Laravel</h2> 
    <p>It is a test email by CodexWorld, sent via SMTP server with PHPMailer in Laravel.</p> 
    <p>Read the tutorial and download this script from <a href="https://www.codexworld.com/">CodexWorld</a>.</p>'

$mail->Body $mailContent
 
// Send email 
if(!$mail->send()){ 
    echo 
'Message could not be sent. Mailer Error: '.$mail->ErrorInfo
}else{ 
    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.

  • Login to your Google account. Go to the My Account page.
  • Navigate to the Security page and scroll down to the Signing in to Google section » Turn Off the 2-Step Verification
  • .

    google-account-security-2-step-verification-gmail-smtp-codexworld
  • Scroll down the Less secure app access section and turn On Less secure app access.
    google-account-security-less-secure-app-access-gmail-smtp-codexworld

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->isSMTP();
$mail->Host     'smtp.gmail.com';
$mail->SMTPAuth true;
$mail->Username 'codexworld@gmail.com';
$mail->Password '******';
$mail->SMTPSecure 'tls';
$mail->Port     587;

Conclusion

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

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up