Home >Backend Development >Python Tutorial >Why Does My Python Gmail Email Script Show 'SMTP AUTH Extension Not Supported,' and How Can I Fix It?

Why Does My Python Gmail Email Script Show 'SMTP AUTH Extension Not Supported,' and How Can I Fix It?

Susan Sarandon
Susan SarandonOriginal
2024-12-01 09:23:11977browse

Why Does My Python Gmail Email Script Show

Sending Emails with Gmail Using Python: Troubleshooting "SMTP AUTH Extension Not Supported" Error

When attempting to send emails via Gmail using Python, you may encounter the following error:

SMTPException: SMTP AUTH extension not supported by server.

To resolve this issue, we need to use the SMTP_SSL class and establish a secure connection instead of the default SMTP. Follow these steps:

  1. Import the smtplib module.
  2. Set up your email details: user (your Gmail address), pwd (your password), recipient (recipient's email address), subject (email subject), and body (email content).
  3. Create an SMTP_SSL object (Port 465):

    server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
  4. Call ehlo() to initiate the session.
  5. Login using your Gmail credentials:

    server_ssl.login(user, pwd)
  6. Send the email message:

    server_ssl.sendmail(user, recipient, message)
  7. Close the connection:

    server_ssl.close()

Here's an updated example using SMTP_SSL:

import smtplib

def send_email(user, pwd, recipient, subject, body):
    # ... Same as before ...

    # Use SMTP_SSL instead of SMTP
    server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
    server_ssl.ehlo()
    server_ssl.login(user, pwd)
    # ... Same as before ...

By using SMTP_SSL and port 465, you'll establish a secure connection and avoid the error "SMTP AUTH extension not supported by server."

The above is the detailed content of Why Does My Python Gmail Email Script Show 'SMTP AUTH Extension Not Supported,' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn