PHPMailer + Gmail SMTP: Simple Configuration Tutorial

To configure SMTP in PHPMailer, you need three main things: Host, Username, and Password.

  • Host: This is your SMTP server. You can use Gmail, Amazon SES, Outlook, or even your own web host. In this example, we are using the Gmail SMTP server, so we will set the host to smtp.gmail.com. (The host changes depending on which SMTP service you use.)

  • Username: This is simply your email address that you will use to send emails.

  • Password: This is not your normal email password. For Gmail, you need to generate a special App Password (explained below).

Here’s the sample configuration:

// Server settings (configure how your email is sent)
$mail->isSMTP();                                      // Tell PHPMailer to use SMTP
$mail->Host       = 'smtp.gmail.com';                 // Gmail SMTP server
$mail->SMTPAuth   = true;                             // Enable SMTP authentication
$mail->Username   = 'your_email@gmail.com';           // Your Gmail address
$mail->Password   = 'your_app_password';              // Your Gmail App Password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;      // Enable SSL/TLS encryption
$mail->Port       = 465;                              // Gmail SMTP port (use 587 for STARTTLS)

How to Get the Gmail SMTP Password

Since Gmail does not allow you to use your normal password for SMTP, you need to create an App Password.

Follow these steps:

  1. Go to your Google Account (https://myaccount.google.com)

  1. Click on the Security tab.

  1. Turn on 2-Step Verification (if not already enabled).

  1. In the search bar, type App Passwords and click on it.

  1. Enter an app name (for example: "PHPMailer") and generate a password.

  1. Copy the 16-digit password shown on the screen.

  1. Paste this password into your PHPMailer SMTP configuration ($mail->Password).

I hope this article helps you configure PHPMailer SMTP using Gmail. If you face any problems, feel free to comment below. We’ll be happy to help you resolve them.

📩 Subscribe for more dev-friendly tutorials. Happy Coding! 💻

0
Subscribe to my newsletter

Read articles from Mohd Ahsan Raza Khan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohd Ahsan Raza Khan
Mohd Ahsan Raza Khan

👋 Hi, I'm Mohd Ahsan Raza Khan! I know you might be thinking my name is quite long, right? Don't worry, you can call me MARK. I'm a software developer with a passion for building web applications and sharing knowledge through writing. I love exploring new technologies and frameworks, and I'm particularly interested in JavaScript, ReactJS, NodeJS, ExpressJS, and MongoDB. In my free time, I enjoy learning about new technologies and writing tutorials to help others learn and grow in their coding journey. I'm always excited to connect with fellow developers, so feel free to reach out to me on LinkedIn. Let's build something amazing together!