Kiran Bhor
3 min readMay 11, 2020

Send email using PHPMailer with your Gmail account details. M

It is very common to send email in web applications. We will see how we can use PHPMailer (third party library) to send email

Create a new directory maildemo in your web server document root directory

Document root for linux based systems
/var/www/html
Document root for mac
/Library/WebServer/Documents

Open your terminal and go to the newly-created directory. We will use composer to install PHPMailer. Composer is used to manage dependencies in PHP projects. We can use PHPMailer without using composer but it is good practice to use composer whenever possible. Click here to follow non-composer approach.

Install the composer on your machine globally. You can refer to this official guide to install composer. If you are using ubuntu system, the composer can be installed with apt-get

apt-get install composer

You can check if the composer is installed successfully by using below command

composer --version
You should see the output like this

Now once the composer is installed. We will initialize our project. Run the following command inside our maildemo folder user document root

composer init

This will as you some information. refer below screenshot how you can

Initialize new project with composer

You can see new file composer.json is created under the mail demo folder. This file contains project information and dependencies required for your project. The composer uses this file to manage the dependencies.

{
"name": "accunity/maildemo",
"description": "Sample project to send mail in php",
"type": "project",
"authors": [
{
"name": "Swati",
"email": "swati@accunityservices.com"
}
],
"require": {}
}

Now, let's add PHPMailer to our project. You can refer to phpmailer github page. Run the following command to install PHPMailer

composer require phpmailer/phpmailer

This will install phpmailer to vendor directory of your project. And also update the require section of composer.json file

{
"name": "accunity/maildemo",
"description": "Sample project to send mail in php",
"type": "project",
"authors": [
{
"name": "Swati",
"email": "swati@accunityservices.com"
}
],
"require": {
"phpmailer/phpmailer": "^6.1"
}

}

As we are going to use Gmail account to send an email we need to enable less secure apps to access Gmail (you can create a temporary Gmail account if you don’t want to enable less secure app access to your Gmail account). Click here to know about less secure app settings. Click here to enable the settings for your Gmail account

Once the setting is enabled, we will write the code to send the an email. Create a new file mail.php inside maildemo folder and copy the following code.

<?php
require_once "vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;$mail = new PHPMailer;$mail->isSMTP();

$mail->Host = " smtp.gmail.com";
$mail->SMTPAuth = true;
//Provide username and password and port
$mail->Username = "Put your gmail email id here";
$mail->Password = "Put your gmail password here";
$mail->Port = 465;
//From email address and name
$mail->From = "Put your gmail email id here";
$mail->FromName = "Put your name here";

$mail->addAddress("email id to whom you want to send an email");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Your email subject here";
$mail->Body = "<h1>Mail using php mailer This is html mail</h1>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}

Access the mail.php file from your browser.

http://localhost/maildemo/mail.php

If you find any problem in sending the mail you can enable debugging in PHPMailer. Just add following line in mail.php after creating PHPMailer object i.e. after $mail = new PHPMailer;

$mail->SMTPDebug = 3;

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kiran Bhor
Kiran Bhor

Written by Kiran Bhor

Cofounded Accunity Services (Software Consultancy Firm) and Passionate Coder at Heart. reach me at kiran@accunityservices.com

No responses yet

Write a response