How to use PHPMailer to send emails in PHP

Featured image of PHPMailer to send emails in PHP

Learn how to use PHPMailer to send emails in PHP

PHPMailer is a code library to send emails safely and easily via PHP code from a web server or local server. Today in this tutorial You will learn how to use PHPMailer to send emails in PHP, but not only that we learn how to send email to multiple users at once and also learn how to send Email With File Attachments. To sending emails through PHPMailer in this tutorial i will use Gmail SMTP.

#1 First you Download PHPMailer library from here –

Download PHPMailer

#2 After downloading PHPMailer library

Copy the PHPMailer ZIP file and go to your Xampp htdocs Folder or wampp www directory. Here you make a new folder and named it whatever you want, here i named this folder phpmail. After that go inside the newly created folder and paste and extract the PHPMailer ZIP file. After that here you create a new file called index.php

My phpmail folder structure

Note – Here i renamed the PHPMailer-master folder into PHPMailer
folder structure of phpmaill

#3 After completing above all the steps


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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions                             
try {
    //Server settings
    $mail->SMTPDebug = 0; // Enable verbose debug output                                 
    $mail->isSMTP(); //Set mailer to use SMTP                                      
    $mail->Host = ''; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = 'Your_email_Address'; // SMTP username
    $mail->Password = 'Your_email_Password'; // SMTP password
    $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587; // TCP port to connect to

    $mail->setFrom('[email protected]', 'Sender Name'); // Sender email and name
    $mail->addAddress('[email protected]'); // Reciver email

    // if you want to send email to multiple users, then add the email addresses you which you want to send.
    //$mail->addAddress('[email protected]');
    //$mail->addAddress('[email protected]');

    //For Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');  // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // You can specify the file name

    $mail->isHTML(true);// Set email format to HTML                                  
    $mail->Subject = 'Here is the subject'; // Subject of the email
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';

    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;

Certainly in this index.php file you need to change some informations. So let’s see those changes –

Need to change
$mail->Host ‘SMTP host name’(Gmail smtp host name is
$mail->Username ‘Type your email address’
$mail->Password ‘Type your email password’
$mail->SMTPSecure Choose encryption type (tsl or ssl)
$mail->Port Write TCP port (Gmail SMTP port (TLS) is – 587)
$mail->setFrom ‘write your email address’, ‘Enter your name’
$mail->addAddress ‘Enter reciver email address’
$mail->Subject ‘Write subject of the email’
$mail->Body ‘Write what you want to say’

Now start your Apache server, after that open your browser and run your page (http://localhost/phpmail/index.php)

If you see an error that the Message could not be sent. Go to your google account ( and login, after that click on Sign-in & security and then scroll down and check the Allow less secure apps. For the reason that message could not be sent.

google sign and security
allow less secure apps

Now try again


Please enter your comment!
Please enter your name here