Send Email Using Gmail SMTP Server From PHP Script

If we have a website then we obviously need to send emails to users. That email should go through your contact us page, through your newsletter, on user registration etc.

PHP provides a mail() function which 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 ended up into a Spam folder.

To get out of these problems, we need to use SMTP server to send emails.

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

Installation

We first need to install PHPMailer library in our project. Recommended way to install 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, we need to change some settings on our Google account. Login to your Google account and click on My Account. Once you are on My Account page then click on Sign-in & Security.

Sign In Security Gmail

On the next page, scroll down to the bottom and you will find ‘Allow less secure apps’ settings. Set it to ON.

Less Secure Apps Gmail

Next, we need to write a code using PHPMailer library along with Gmail SMTP server settings.

PHP Script For Sending Email Using Gmail SMTP Server

Open your PHP file where you need to write a code for emails. For instance, we are assuming you have 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, we have included the environment of PHPMailer library in our file. Next, for sending an emails using PHPMailer we need to pass 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

We have setup our Gmail SMTP server settings. Now, we all good to go for sending an email to a user.

$mail->setFrom('[email protected]', 'Artisans Web');
$mail->addAddress('[email protected]', 'User Name');

$mail->isHTML(true);

$mail->Subject = 'Email subject';
$mail->Body    = 'Email Body';

if (!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Sending Attachments In An Email

PHPMailer library provides a way for sending single or multiple attachments in an email. We all need to do is pass a directory path of our attachments to the method addAttachment.

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

Our final code is as follows.

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);

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('[email protected]', 'Artisans Web');
	$mail->addAddress('[email protected]', 'User Name');

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

    $mail->isHTML(true);

    $mail->Subject = 'Email Subject';
    $mail->Body    = 'Email Body';

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

We hope you understand send email using Gmail SMTP server from PHP script. You may also like our article Sending Email Via Gmail SMTP Server In Laravel. If you have any questions or suggestions please leave a comment below.

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

Recommended Tutorials For You

Posted in PHP

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

  1. 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

  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.

Leave a Reply

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