How to Send an Email via Gmail SMTP Server using PHP

Introduction

In this tutorial, you will learn how to send email in PHP using PHPMailer library via Gmail SMTP.  Like PHPMailer, there are few more good email sending libraries in PHP e.g. PEAR::Mail interface, Swiftmailer etc which can help you easily send mail in PHP using Gmail SMTP.

 

Prerequisites

Before starting with steps on how to send mail using SMTP in PHP example, lets first see what are few limits with Gmail SMTP servers and how to overcome some of these:

  • Gmail limits the number of recipients in a single email and the number of emails that can be sent per day. The current limit is 500 Emails in a day or 500 recipients in a single email. You can't really increase this limit. If you want to send above these limit, then you need to integrate with third-party email delivery platform like Pepipost Sendgrid etc.
  • On reaching threshold limits, you won't be able to send messages for the next 24 hours. Once this temporary suspension period is over, the counter gets reset automatically, and the user can resume sending emails.
  • By default, any third-party apps/codes are not allowed to send emails using your Gmail account. And, hence there are few settings which need to be done at your end:

How To Enable Email Sending In Gmail?

  1. Before sending emails using the Gmail's SMTP Server, you to make some of the security and permission level settings under your Google Account Security Settings.

  2. Make sure that 2-Step-Verification is disabled.

  3. Turn ON the "Less Secure App" access.

  4. If 2-step-verification is enabled, then you will have to create app password for your application or device.

  5. For security measures, Google may require you to complete this additional step while signing-in. Click here to allow access to your Google account using the new device/app.

Note: It may take an hour or more to reflect any security changes

 

Writing the PHP Code to Send Email using Gmail SMTP

Step 1:  Download PHPMailer library from this github link. To directly download the .zip file, use this link.

Unzip the master.zip in your application directory and run following command from your application directory.

composer require phpmailer/phpmailer

Composer is the recommended way to install PHPMailer.

 

Step 2: Writing the PHP Code to make an SMTP connection

  • Using your Gmail credentials, connect to host "smtp.gmail.com"
  • Click here for some more Examples and Tutorials of PHPMailer

 

Step 3: Include packages and files for PHPMailer and SMTP protocol:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';

 

Step 4: Initialize PHP Mailer and set SMTP as mailing protocol:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = "smtp";

 

Step 5: Set required parameters for making an SMTP connection like server, port and account credentials. SSL and TLS are both cryptographic protocols that provide authentication and data encryption between servers, machines and applications operating over a network. SSL is the predecessor to TLS.

$mail->SMTPDebug  = 1;  
$mail->SMTPAuth   = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port       = 587;
$mail->Host       = "smtp.gmail.com";
$mail->Username   = "your-email@gmail.com";
$mail->Password   = "your-gmail-password";

 

Step 6: Set the required parameters for email header and body:

$mail->IsHTML(true);
$mail->AddAddress("recipient-email@domain", "recipient-name");
$mail->SetFrom("from-email@gmail.com", "from-name");
$mail->AddReplyTo("reply-to-email@domain", "reply-to-name");
$mail->AddCC("cc-recipient-email@domain", "cc-recipient-name");
$mail->Subject = "Test is Test Email sent via Gmail SMTP Server using PHP Mailer";
$content = "<b>This is a Test Email sent via Gmail SMTP Server using PHP mailer class.</b>";

 

Step 7: Send the email and catch required exceptions:

$mail->MsgHTML($content); 
if(!$mail->Send()) {
  echo "Error while sending Email.";
  var_dump($mail);
} else {
  echo "Email sent successfully";
}

 

Working PHP Code to Send Email Using SMTP Server

Click here to download the complete working PHP code to send email using Gmail SMTP server. You need to just change a few values and it should work.

 

List of Possible Errors And Exceptions

 

Error 1: Password Command Failed: 534-5.7.9 Application-Specific Password Required

  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

How to Set Up a Mac for Your Kids

Providing children with access to a computer and the internet is increasingly important, but so...

How to Get Help With a Command from the Linux Terminal

Whether you’re an inexperienced terminal user or a grizzled veteran, you won’t always know the...

How to change the ssh port on Linux

To Change the SSH Port for Linux Server Connect to your server via SSH Switch to the root...

How To Install screenFetch in Linux

If you've browsed Linux groups on social media, you've probably seen a lot of screenshots that...

Static IP vs. Dynamic IP Address

A static IP address is one that remains fixed and never changes. The PC always sees the same...

Powered by WHMCompleteSolution