Skip to Content
how to send email smtp server in laravel 7
php code / September 3, 2021

In the PHP-based web application, we utilised the mail() method to send emails. There are nevertheless times when it fails to deliver email or delivers it into the spam folder when using PHP’s mail() function Sending emails from web scripts should always be done via the SMTP server because of this. To send email using PHP and an SMTP server, you can use the PHPMailer library. To send emails from any PHP framework, you can use the PHPMailer package (CodeIgniter, Laravel, Lumen, etc.).

Email can be sent using PHP and the PHPMailer module, which provides a simple way to send email via SMTP server. You can use PHPMailer to send an email using an SMTP server if your application is developed in Laravel.

Using PHPMailer, we’ll teach you how to send email from Laravel using an SMTP server.

You may install PHPMailer via Composer by running the following command in your Laravel application:

composer require phpmailer/phpmailer

It is necessary to load the PHPMailer class and the Exception class in order to use these functions.

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception;

Initialize the PHPMailer class and set up SMTP and email settings.

// 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 CodexWorld, sent via SMTP server with PHPMailer in Laravel.</p> 
<p>Read the tutorial and download this script from <a href="">CodexWorld</a>.</p>'; 
$mail->Body = $mailContent; 

// Send email 
echo 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo; 
echo 'Message has been sent.'; 

In Laravel, send email over Gmail’s SMTP service
For Laravel to use Gmail SMTP to deliver email messages, various Google account settings must be adjusted. Gmail SMTP may be used using the PHPMailer library in Laravel by following the procedures listed below:

Access your Google account by logging in with your Google account credentials (see above). Then, click on the My Account link.
To sign in, go to the Security page and scroll down. Toggle off the two-step authentication

In the section titled “Less secure app access,” select “On.”

Finished! You may now send emails from your Laravel application using Gmail SMTP.

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

// SMTP configuration 
$mail->Host = ''; 
$mail->SMTPAuth = true; 
$mail->Username = ''; 
$mail->Password = '******'; 
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587;


To send text or HTML email from a Laravel application, use the Laravel Mail service with an SMTP server as shown in the example code. With this code, you can use it to send email from Lumen as well. PHPMailer allows you to tailor the email functionality to meet your specific needs through a variety of configuration options.

PHPCODE © 2024