Send email using PHPMailer

By Author | May 03, 2019 | | 612 Views | Category : PHP

In this tutorial we will learn how to send email's using  PHPMailer library which is open-source . We can send emails using  SMTP  protocol by using built in support of SMTP in this library.  PHPMailer library is available for download on github . We will use gmail smtp authorizations to send emails which also happens to be free.

So lets start by downloading this library from github and unzip it.

Now we can create our index.php file (you can name it anything) which we will use to send emails.

Step 1: Create index.php file

We can create our index.php file in "htdocs/www" folder if you are using (MAMP/WAMP/XAMPP) which is directly pointed by localhost.

Step 2: Copy below code in you index.php


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

use PHPMailer\PHPMailer\PHPMailer;

            $mail = new PHPMailer;

            $mail->Host = '';
            $mail->SMTPAuth = true;
            $mail->Username = '{your email address}';
            $mail->Password = '{you password}';
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;

            $mail->setFrom('{your email address}', '{Any title}');

            $mail->addAddress('{recipient email address}');

             $mail->Subject = '{Your email subject }';
            // Optional if you are using html in mail body

            $mail->Body = "Congratulations for your first email";

If you are not able to recieve mail or getting 500 error from server, then you may need to activate option to allow access to less secure apps in you Gmail setting from this link ""

Tags :