Send Email via SMTP Server in Laravel Easy Way

Send Email via SMTP Server in Laravel Easy Way

Send Email 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 email send via SMTP server in Laravel using PHPMailer.

 

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->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.’; 

}

 

email  Send 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

  1. 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->isSMTP();

$mail->Host     = ‘smtp.gmail.com’;

$mail->SMTPAuth = true;

$mail->Username = ‘vkprogramming@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.

 

People also ask

How do I send an email in laravel?

How do I automatically send mail in laravel?

What is mail in laravel?

How do you send an attachment in laravel?

send mail in laravel 8

Leave a Comment

Your email address will not be published. Required fields are marked *