Home >Backend Development >Python Tutorial >How to Resolve 'SMTP AUTH extension not supported by server' Errors When Sending Emails with Gmail and Python?
Sending Emails with Gmail Using Python
When attempting to send emails using Python's SMTP library through Gmail, you may encounter an error stating that SMTP AUTH is not supported by the server.
Error Description:
The error "SMTP AUTH extension not supported by server" occurs when you try to authenticate with the SMTP server using the login() method without enabling TLS encryption.
Resolution Using TLS:
To resolve this issue, enable TLS encryption by following these steps:
import smtplib # Enable TLS encryption server = smtplib.SMTP('smtp.gmail.com:587') server.starttls()
Once TLS is enabled, you can authenticate with the server using the login() method.
Using Port 465 with SSL:
Alternatively, you can use Port 465 with SSL encryption. For this, you need to create an SMTP_SSL object:
import smtplib server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465) server_ssl.ehlo() # optional, called by login() server_ssl.login(gmail_user, gmail_pwd)
Note that SSL servers do not support or require TLS encryption, so do not call server_ssl.starttls().
Example Script for Sending Emails:
Here's an improved version of the Python script provided in the question:
def send_email(user, pwd, recipient, subject, body): FROM = user TO = recipient if isinstance(recipient, list) else [recipient] SUBJECT = subject TEXT = body # Prepare actual message message = """From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT) try: server = smtplib.SMTP("smtp.gmail.com", 587) server.ehlo() server.starttls() server.login(user, pwd) server.sendmail(FROM, TO, message) server.close() print('successfully sent the mail') except: print("failed to send mail")
The above is the detailed content of How to Resolve 'SMTP AUTH extension not supported by server' Errors When Sending Emails with Gmail and Python?. For more information, please follow other related articles on the PHP Chinese website!