Comprehensive Guide to Sending Emails with Python Using Gmail SMTP

Nov 19, 2025 · Programming · 13 views · 7.8

Keywords: Python | Email | SMTP | Gmail | Authentication

Abstract: This article provides a detailed guide on how to send emails using Python's smtplib module with Gmail SMTP. It covers common errors like SMTP AUTH extension not supported, step-by-step solutions with code examples, security configurations, and alternative methods such as SMTP_SSL and Gmail API. Aimed at developers, it ensures reliable email sending in applications.

Introduction

Sending emails is a common requirement in many applications, and Python's smtplib module simplifies this process. However, when using Gmail as the SMTP provider, developers may encounter authentication errors. This article delves into the causes of these errors and provides comprehensive solutions.

Error Analysis

A frequent error is "SMTP AUTH extension not supported by server," which often occurs due to incorrect SMTP protocol steps. Gmail requires sending the EHLO command before STARTTLS to negotiate extensions.

Solution: Correct SMTP Setup

To successfully send emails, follow these steps: establish an SMTP connection, send the EHLO command, start TLS encryption, log in to the account, and finally send the email. Below is a detailed code implementation.

Code Implementation

Here is a rewritten Python script example based on best practices:

import smtplib

fromaddr = 'user_me@gmail.com'
toaddrs = 'user_you@gmail.com'
msg = "\r\n".join([
    "From: user_me@gmail.com",
    "To: user_you@gmail.com",
    "Subject: Just a message",
    "",
    "Why, oh why"
])
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

In this code, we first import the smtplib module. Then, define variables for the sender, recipient, message content, etc. The message uses CRLF as line endings and includes necessary header fields. Next, establish the SMTP connection, call ehlo() to identify the client, start TLS encryption, log in to the account, send the email, and finally close the connection.

Security Considerations

Gmail's security settings may block non-Google apps from logging in. Users need to enable "Allow less secure apps" in their Google account or use an App Password. If two-step verification is enabled, an App Password must be generated for login.

Alternative Methods

Besides using port 587 with STARTTLS, you can use port 465 with SMTP_SSL for an SSL-encrypted connection. Additionally, the Gmail API offers advanced features but requires OAuth 2.0 authentication. Here is an example of SMTP_SSL:

import smtplib

server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.login('user@gmail.com', 'app_password')
message = "From: user@gmail.com\nTo: recipient@gmail.com\nSubject: Test\n\nThis is a test email."
server_ssl.sendmail('user@gmail.com', 'recipient@gmail.com', message)
server_ssl.close()

When using the Gmail API, OAuth 2.0 credentials and scopes are needed, but this article focuses on SMTP methods.

Conclusion

By correctly configuring SMTP steps and security settings, you can reliably send Gmail emails using Python. It is recommended to always use App Passwords for enhanced security and test the code for compatibility.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.