Send Email using the PHPMailer Library and Gmail SMTP Server

In this article, I will let you know how to send email using PHP, how to configure SMTP and how to send email using the gmail SMTP server. Let’s start.

First, we will talk about the PHP mail() function. PHP provides a function mail() for sending email. But this function has a limitation, we can not send email using a local server and mostly we face email spamming issues with this function. Also sometimes mail() function does not deliver email.

For fixing these issues, we use the PHPMailer library for sending emails from PHP.

How To Enable Gmail Settings?

We need to enable the Gmail setting for sending emails from the Gmail SMTP server.

First, we need to do some security and permission settings. Click on the below URL to do the settings:

Google Account Security Setting https://myaccount.google.com/security

Turn ON the Less Secure App access
Link: https://myaccount.google.com/lesssecureapps

The Gmail setting is done.

How to Install PHPMailer Library?

For sending an email using SMTP, we need to install the PHPMailer library in the project. Composer is the recommended way to install the PHPMailer library. Also, you can download the library from the GitHub https://github.com/PHPMailer/PHPMailer/

Open the command prompt in the project’s root directory and run the below command.

composer require phpmailer/phpmailer

We need to include the environment of the PHPMailer library in the PHP file.

<?php

//Include PHPMailer classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once 'vendor/autoload.php';
 
$mail = new PHPMailer(true);
?>

Now we need to setup SMTP parameters for making connection. Here we need to put the SMTP credentials. Here we are sending email using Gmail SMTP server so we will pass the Gmail SMTP server details.

$mail->isSMTP();
$mail->SMTPDebug = 1;  
$mail->SMTPAuth = TRUE;
//Security type
$mail->SMTPSecure = "tls";
//SMTP port
$mail->Port = 587;
//gmail SMTP servername
$mail->Host = 'smtp.googlemail.com'; 
$mail->SMTPAuth = true;
//gmail username/email
$mail->Username = "example@mail.com"
//gmail password
$mail->Password = 'example12345'; 

For SSL security type, you need to use 465 into the port.

The SMTP configuration is done. Now we need to set up the header and body parameters.

$mail->setFrom('from-email@gmail.com', 'from-email');
$mail->addAddress('recipient-email@gmail.com', 'recipient-email');

$mail->isHTML(true);
 
$mail->Subject = 'This is test email subject';
$mail->Body   = '<b>This is a test email.</b>';
 
if(!$mail->Send()) {
  echo "Not sent";
} else {
  echo "Email sent successfully";
}

Now we can send the email using the PHPMailer library.

How to send the attachment file in the email?

We need to set the correct directory path. PHPMailer library can send multiple attachments in the email.

$mail->addAttachment('/attachment1.jpg');
$mail->addAttachment('/attachment2.jpg');
$mail->addAttachment('/attachment3.jpg');

This is a full PHP script to send email using the PHPMailer library.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
 
require_once "vendor/autoload.php";
require_once "constants.php";
 
$mail = new PHPMailer(true);
 
try {
$mail->isSMTP();
//SMTP debugger is off for enabling it use 2 instead of 1;
$mail->SMTPDebug  = 1;  
$mail->SMTPAuth   = TRUE;
$mail->SMTPSecure = "tls";
//SMTP port
$mail->Port       = 587;
//gmail SMTP servername
$mail->Host = 'smtp.googlemail.com'; 
$mail->SMTPAuth = true;
//gmail username/email
$mail->Username = "example@mail.com"
//gmail password
$mail->Password = 'example12345'; 

$mail->setFrom('from-email@gmail.com', 'from-email');
$mail->addAddress('recipient-email@gmail.com', 'recipient-email');
  

 //This is for sending attachment files 
$mail->addAttachment('/attachment1.jpg');
$mail->addAttachment('/attachment2.jpg');
$mail->addAttachment('/attachment3.jpg');
 
$mail->isHTML(true);
 
$mail->Subject = 'This is test email subject';
$mail->Body    = '<b>This is a test email.</b>';
 
if(!$mail->Send()) {
  echo "Error while sending Email.";
} else {
  echo "Email sent successfully";
}
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: '. $mail->ErrorInfo;
}
?>

Thanks for reading, feel free to reach out to me for any comments and suggestions. I hope you found the article helpful and you were able to configure the PHPMailer library and able to send email successfully using the Gmail SMTP server in PHP.

Leave a comment

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