How to use PHPMailer to send emails in PHP

PHP send emails using PHPMailer

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

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 and name it index.php

My phpmail folder structure

Note – Here i renamed the PHPMailer-master folder into PHPMailer

phpmail folder structure

3 – Creating files


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 information. So let’s see those changes –

Need to changeDescription
‘SMTP host name’(Gmail smtp host name is
‘Type your email address’
‘Type your email password’
Choose encryption type (tsl or ssl)
Write TCP port (Gmail SMTP port (TLS) is – 587)
‘write your email address’, ‘Enter your name’
‘Enter reciver email address’
‘Write subject of the email’
‘Write your message’

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 log in.

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

You May Also Like

Leave a Comment

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