PHPMailer: Sending Emails via Gmail SMTP Server

This example shows how to generate app credentials in Google account and settings for PHPMailer to use when sending via Google’s Gmail servers.

1. Creating credentials in your Gmail account

Login to your Google account which you are going to use for sending emails

Your account should have 2-Step Verification enabled. If it doesn’t click here to get it.

Create App passwords in Manage your Google Account > Security

Sign in, if required

  • Under “Select the app and device you want to generate the app password for.” click Select app > Mail
  • In Select device select Other (Custom name)
  • Type a name for the app
  • Click Generate
  • Copy and paste somewhere the 16 character password generated, you’ll need it later

2. Setting up PHPMailer for sending via Google’s Gmail servers

//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
//Enable SMTP debugging. SMTP::DEBUG_SERVER = client and server messages - uncomment this if you are debugging
//$mail->SMTPDebug = SMTP::DEBUG_SERVER;
//Set the hostname of the mail server
$mail->Host = '';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption mechanism to use - STARTTLS or SMTPS
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail account you created credentials earlier
$mail->Username = '';
//Password to use for SMTP authentication - the 16 character password generated you copied before (no brakes)
$mail->Password = 'mhmrblznijnkeiqk';
//Set who the message is to be sent from. If you want to use different email address as sentFrom, then you need to add the email in gmail settings > Accounts and import > Send mail as
$mail->setFrom('', 'First-name Last-name');




Lead Web Developer at Vue Health. Husband of a wonderful wife, Entrepreneur, Dad

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Real-time dashboarding with Azure Event Hubs and Stream Analytics

How we set up the Aircall — Intercom integration

Store your git hooks in a repository

Why Is Standardization Important For IoT?


Docker & its Command line

CoDEVIANT #1 (3/21/19)

My Journey from Accounting to Software Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Max Shestov

Max Shestov

Lead Web Developer at Vue Health. Husband of a wonderful wife, Entrepreneur, Dad

More from Medium

Site Tracker App with HMS Toolkit

Deploy a Deepl app for slack on EC2 Instance

Creating First Smart Contract — Nothing Different From a Container