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
Release: 2024-12-01 09:23:11
Original
983 people have browsed it

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.
Copy after login

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)
    Copy after login
  4. Call ehlo() to initiate the session.
  5. Login using your Gmail credentials:

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

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

    server_ssl.close()
    Copy after login

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 ...
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template