Send Email Using Gmail SMTP Server From PHP Script

Email plays an important role on the website. Every website usually needs to send emails to users. These emails can be sent from contact us page, newsletter, registration form, etc.

PHP provides a mail() function which is used to send an email. But there are limitations while using mail() method. You can’t send email from a local development server. Another drawback is, there is a high possibility of your email ending up into a Spam.

In most cases, mail() method even does not send an email. This may be because of the wrong server configuration or something else.

To get out of these problems or limitations, one can use the SMTP server to send the emails.

In this article, we study how to use PHPMailer and Gmail SMTP server for sending emails.

Installation

You first need to install the PHPMailer library in your project. Recommended way to install a library is through Composer.

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

composer require phpmailer/phpmailer

As we are using Gmail SMTP, you need to change some settings on your Google account. Login to your Google account and click on My Account. Once you are on My Account page, click on Security. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.

less-secure-apps

After this, we need to write a code that sends an email using the PHPMailer library and Gmail SMTP server.

PHP Script for Sending Email using Gmail SMTP Server

Open your PHP file where you need to write a code for emails. For instance, I am assuming you have a sendemail.php file in the root directory.

sendemail.php

<?php
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once 'vendor/autoload.php';

$mail = new PHPMailer(true);
?>

In the above code, I have included the environment of the PHPMailer library in the PHP file.

Next, for sending emails using PHPMailer the user needs to pass the Gmail SMTP server address, SMTP port for Gmail, and SMTP authentication(which is nothing but your username and password of a Google account).

$mail->isSMTP();
$mail->Host = 'smtp.googlemail.com';  //gmail SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'GMAIL_USERNAME';   //username
$mail->Password = 'GMAIL_PASSWORD';   //password
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;                    //SMTP port

That’s it! You are done with the configuration. Now, you are good to go ahead with sending an email to a user.

$mail->setFrom('FROM_EMAIL_ADDRESS', 'FROM_NAME');
$mail->addAddress('RECEPIENT_EMAIL_ADDRESS', 'RECEPIENT_NAME');

$mail->isHTML(true);

$mail->Subject = 'Email subject';
$mail->Body    = '<b>Email Body</b>';

$mail->send();
echo 'Message has been sent';

Replace the placeholders with actual values. Run this file on a browser and your email should send on the recipient’s email address.

Sending Attachments in an Email

Using the PHPMailer library one can send single or multiple attachments in an email. You all need to do is pass a directory path of your attachments to the method addAttachment as follows.

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

Our final code is as follows.

sendemail.php

<?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();
    $mail->Host = 'smtp.googlemail.com';  //gmail SMTP server
    $mail->SMTPAuth = true;
    $mail->Username = GMAIL_USERNAME;   //username
    $mail->Password = GMAIL_PASSWORD;   //password
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;                    //smtp port
 
    $mail->setFrom('FROM_EMAIL_ADDRESS', 'FROM_NAME');
    $mail->addAddress('RECEPIENT_EMAIL_ADDRESS', 'RECEPIENT_NAME');

    $mail->addAttachment(__DIR__ . '/attachment1.png');
    $mail->addAttachment(__DIR__ . '/attachment2.png');

    $mail->isHTML(true);
    $mail->Subject = 'Email Subject';
    $mail->Body    = '<b>Email Body</b>';

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

I hope you understand how to send email using Gmail SMTP server from a PHP script. Please share your thoughts and suggestions in a comment below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

13 thoughts on “Send Email Using Gmail SMTP Server From PHP Script

  1. The great thing about PHPMailer is that you can also send email from local server as well. For this you have to define different variables than the ones you do in smtp. There is no need to define host or other things. Just define email fields, like from, subject and other things. Source: How to use PHPMailer

  2. When i downloaded the PHPMAILER from the github.com, I don’t see the file of “autoload.php”. Was it a old version of PHPMAILER you used in the tutorial?

    1. autoload.php is generated when you install the library through Composer. As mentioned in the article, you need to run composer require phpmailer/phpmailer command to install this library.

  3. thanks 🙂 I have some question
    -does that gmail Smtp is free to use
    -does every hosts allow that method , if not ,do you know if 00webhost host allows user to send email by using gmail SMTP

Leave a Reply

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