Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

email - python 3.9.0 smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server

I am trying to send emails with gmail using the smtp module for an amazon price tracker. A few days ago, it was working perfectly but today it came up with this error:

     File "C:Users61409AppDataLocalProgramsPythonPython39libsmtplib.py", line 755, in starttls
      raise SMTPNotSupportedError(
  smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.

This is the code that originally worked but gives the error:

import smtplib
 def send_email():
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()


    server.login('[email protected]', '*password*')
    subject = f'The price of your product has fallen'
    
    body = f'Check the amazon link - {URL}'
    
    msg = f"Subject: {subject}

{body}"
    
    server.sendmail(
        '[email protected]',
        '[email protected]',
        msg
    )
    print('The email has been sent')

    server.quit()

I have tried making another gmail account and using SSL (importing ssl too) but the other gmail account comes up with the same problem and when using SSL, it comes up with a SMTP AUTH problem (unable to solve this too). I have allowed less secure apps but the same problem still occurs. I have also tried removing the first server.ehlo() and I have also tried using the server.set_debuglevel(1) but both do not work. I have also tried using with stmplib.SMTP(*Host*, *Port*) as server: and I have tried just using SMTP without using SSl and TLS at all but both answers do not work. Most of the answers removed the smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server. but other problems occured such as smtplib.SMTPServerDisconnected: Connection unexpectedly closed which happened numerous times with other answers. I tried researching on solutions to the connection unexpectedly closing but none of those answers worked too.

These are the answers I have tried:

SMTPException: STARTTLS extension not supported by server

smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server

Python 2: SMTPServerDisconnected: Connection unexpectedly closed

Python3 SMTP 'Connection unexpectedly closed'

https://www.reddit.com/r/Python/comments/5grxy8/python_smtplib_connection_unexpectedly_closed/daumi2m/

How to send an email with Python?

How can I fix this problem?, am I missing something?? Will I need to use another module or library to send emails??? I apologise if this is a repeat question with a viable answer. I also apologise if one of the answers I mentioned as not working does actual work except I screwed up the code.

question from:https://stackoverflow.com/questions/65650345/python-3-9-0-smtplib-smtpnotsupportederror-starttls-extension-not-supported-by

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.

The SMTP connection to the server does not seem to offer the STARTTLS extension, at least as seen from the perspective of the client. This extension is announced inside the response to the initial EHLO command of the client:

$ telnet smtp.gmail.com 587
...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP h9sm19557267wre.24 - gsmtp
EHLO mail.example.com
250-smtp.gmail.com at your service, ...
250-SIZE 35882577
250-8BITMIME
250-STARTTLS                            <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
...

Given that smtp.gmail.com definitely supports this extension it is likely that there is some man in the middle somewhere in path of the connection, which rewrites the traffic to something like this:

220 smtp.gmail.com ESMTP h9sm19557267wre.24 - gsmtp
EHLO mail.example.com
250-smtp.gmail.com at your service, ...
250-SIZE 35882577
250-8BITMIME
250-XXXXXXXA                            <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
...

Here the response of the server is changed to no longer show support for STARTTLS, in the hope that the client continues without encryption and thus the mail can be analyzed be the man in the middle. Such man in the middle are typically corporate firewalls like Cisco ASA but can also be ISP or local antivirus products.

A few days ago, it was working perfectly but today it came up with this error:

Check if there were any changes regarding local security products (antivirus) or its settings or if there are changes to your environment (corporate firewall, different network you use ...).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...