To send through Gmail over SMTP, connect to smtp.gmail.com on port 465 (SSL) or port 587 (TLS), log in with your full Gmail address, and use an app password, not your normal password. That works for mail clients, scripts and small sites. For app email at any real volume, Gmail’s daily limits get in the way quickly.
Gmail SMTP settings
| Setting | Value |
|---|---|
| SMTP server | smtp.gmail.com |
| Port (SSL) | 465 |
| Port (TLS / STARTTLS) | 587 |
| Authentication | Required |
| Username | Your full address, for example you@gmail.com |
| Password | A 16-character app password |
| Daily limit, personal Gmail | 500 messages |
| Daily limit, Google Workspace | 2,000 messages (500 on trial accounts) |
The server, ports and Workspace limit come from Google’s send email from a printer, scanner or app page. The personal limit is from Gmail sending limits.
Create a Gmail app password
Google stopped accepting account passwords from third-party apps. It now says Gmail no longer supports apps or devices that ask you to share your Google username and password. Where an app offers “Sign in with Google,” use that. Where it only has username and password fields, use an app password:
- Turn on 2-Step Verification for the Google account. App passwords only work when it is on.
- Open your Google Account, search for App passwords, and create one. Give it a name you will recognize, like “Office printer.”
- Copy the 16-character code. Use it as the SMTP password, without spaces.
You will not see the App passwords option if your account uses only security keys for 2-Step Verification, is enrolled in Advanced Protection, or is a work or school account whose admin has turned it off.
App passwords bypass 2-Step Verification for that one app, so treat them like a real password. Revoke them when a device is retired.
Gmail SMTP with PHP
Most PHP sites send with PHPMailer. These are the settings that matter:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'you@gmail.com';
$mail->Password = getenv('GMAIL_APP_PASSWORD');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // port 465
$mail->Port = 465;
$mail->setFrom('you@gmail.com', 'Your Name');
$mail->addAddress('sam@example.com');
$mail->Subject = 'Test from PHP';
$mail->Body = 'It works.';
$mail->send();
For port 587, use PHPMailer::ENCRYPTION_STARTTLS and Port = 587. Mixing them, for example ENCRYPTION_SMTPS with port 587, makes the connection hang and time out.
Keep the From address the same as the Gmail account (or a verified “Send mail as” alias). Gmail rewrites other From addresses to the account you logged in with.
Gmail SMTP with Python
The same settings work from Python’s standard library, with no extra packages:
import os, smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["From"] = "you@gmail.com"
msg["To"] = "sam@example.com"
msg["Subject"] = "Test from Python"
msg.set_content("It works.")
with smtplib.SMTP("smtp.gmail.com", 587) as s:
s.starttls() # upgrade to TLS before logging in
s.login("you@gmail.com", os.environ["GMAIL_APP_PASSWORD"])
s.send_message(msg)
For port 465, use smtplib.SMTP_SSL("smtp.gmail.com", 465) and drop the starttls() call.
Gmail SMTP limits
Google publishes these limits:
- Personal Gmail: 500 messages a day, and no more than 500 recipients on one message.
- Google Workspace: 2,000 messages a day per user, 500 on trial accounts, and 100 recipients per message when sending over SMTP.
- What happens when you hit it: sending stops for up to 24 hours, though you can still receive mail. Limits are measured over a rolling 24 hours, not a calendar day.
Those numbers are generous for a person and tight for software. A sign-up flow, a batch of receipts or a password-reset spike can use a day’s allowance in minutes, and when that happens the account owner’s own email stops too.
Common Gmail SMTP errors
| Error | Meaning | Fix |
|---|---|---|
535 5.7.8 username and password not accepted |
Wrong password type | Use an app password, check for typos and spaces |
530 5.7.0 must issue a STARTTLS command first |
Connected on 587 without TLS | Enable STARTTLS, or switch to 465 with SSL |
| Connection timed out | Port blocked, or SSL/port mismatch | Try the other port; pair 465 with SSL, 587 with TLS |
| “You have reached a limit for sending mail” | Daily quota used | Wait up to 24 hours, or move app email elsewhere |
Google’s full list is on its SMTP errors and codes page, and our SMTP error codes guide explains the structure of every code.
Gmail SMTP vs the Google Workspace relay
If you have Google Workspace, an admin can enable smtp-relay.gmail.com. It authenticates by IP address or login, can send from any address in your domain, and allows up to 10,000 recipients per user per day. It is the better option for office printers and on-premises servers. The Google Workspace SMTP relay guide walks through the setup.
When to use a transactional relay instead
Use Gmail SMTP for your own mail client and small, personal jobs. Use a transactional email service when:
- The email is part of your product: confirmations, resets, receipts, alerts.
- You need more than a few hundred messages a day.
- You want to know which messages bounced or were marked as spam.
- You want mail to come from your own domain with SPF, DKIM and DMARC passing.
Mailcandor’s SMTP relay takes the same PHPMailer code with four changes: Host = 'smtp.mailcandor.com', Username = 'mailcandor', Password set to an API key, and your own verified domain in setFrom. Ports 465 and 587 work the same way. The free Starter plan includes 3,000 emails a month (100 a day); pricing has the rest.