2026-07-15

How to send an email with Gmail as provider using Python?

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

How to send an email with Gmail as provider using Python?

How to Send Emails with Gmail as Provider Using Python: Fixing the Authentication Error

Sending emails programmatically is a common task in backend development, whether you are building an automated notification system or a data synchronization tool. When developers attempt to use standard Python libraries like smtplib with services like Gmail, they often run into frustrating authentication errors.

The traceback you provided—smtplib.SMTPException: SMTP AUTH extension not supported by server—is a classic indicator that the method of authentication being used is either outdated or blocked by the service provider for security reasons. As a senior developer, understanding why this happens and implementing the modern solution is crucial.

This post will dissect the error, explain the underlying cause related to Gmail’s security policies, and provide the robust, modern solution for sending emails via Python.

Why Your Original Script Failed: The Security Barrier

The error you encountered stems from how Google (and many other major providers) handle email account security, especially when two-factor authentication (2FA) is enabled.

When you try to log in with your regular Gmail username and password directly using smtplib, the server recognizes that standard password login for external applications is often insufficient or blocked without specific security extensions being properly negotiated. This limitation exists to protect user accounts from unauthorized access, even if the application has the correct credentials. The system is essentially telling your script: "I don't allow this simple form of authentication right now."

The code snippet you used relies on basic SMTP authentication, which modern providers restrict for security.

# Original problematic line causing the error:
server.login(username,password) 

To successfully send emails programmatically through Gmail, we must bypass this restriction by providing a more secure, specific type of credential that Google accepts for third-party applications.

The Correct Solution: Using App Passwords

The industry-standard and most secure way to authenticate an external application (like your Python script) to a Gmail account is by generating an App Password.

What are App Passwords?

An App Password is a unique, randomly generated password that you can use instead of your regular account password when connecting to less secure applications. If your Google account has 2FA enabled, this method ensures that even if the application is compromised, the actual primary account remains secure.

Steps to Implement:

  1. Enable 2-Factor Authentication (2FA): Ensure 2FA is enabled on your Google account (this is a prerequisite for security).
  2. Generate the App Password: Log into your Google Account Security settings. Navigate to the App Passwords section and generate a new password specifically for an application.
  3. Use the Generated Password: Use this unique 16-character password in place of your regular account password in your Python script.

Revised Python Implementation

By using the generated App Password, you satisfy the server's authentication requirements, resolving the SMTP AUTH extension not supported error.

Here is the corrected and best-practice implementation:

import smtplib

# --- Configuration (Use the App Password here) ---
fromaddr = 'user_me@gmail.com'
toaddrs  = 'user_you@gmail.com'
msg = 'Why,Oh why! This email was sent securely.'
username = 'user_me@gmail.com'
# IMPORTANT: Use the generated App Password, NOT your regular Gmail password
password = 'your_generated_app_password_here' 

try:
    # Connect to the SMTP server for Gmail
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()  # Secure the connection
    
    # Log in using the App Password
    server.login(username, password)
    
    # Send the email
    server.sendmail(fromaddr, toaddrs, msg)
    
    print("Email sent successfully!")

except smtplib.SMTPAuthenticationError as e:
    print(f"Authentication failed. Check your username and App Password.")
    print(e)
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    if 'server' in locals() and server:
        server.quit()

Conclusion: Security First in Backend Development

This experience highlights a core principle of modern backend development: Security must precede functionality. Relying on simple username/password pairs for sensitive operations is an open invitation to security risks.

When building services—whether you are managing data synchronization, handling notifications, or integrating external APIs (much like how robust services are built in frameworks like Laravel)—always prioritize secure authentication methods. For email delivery via Python, using App Passwords transforms a brittle script into a reliable, secure communication channel. As you develop more complex applications, focus on utilizing established security patterns rather than fighting against outdated server limitations.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.