1

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:\Users\61409\AppData\Local\Programs\Python\Python39\lib\smtplib.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('xxxxxxxx@gmail.com', '*password*')
    subject = f'The price of your product has fallen'
    
    body = f'Check the amazon link - {URL}'
    
    msg = f"Subject: {subject}\n\n{body}"
    
    server.sendmail(
        'xxxxxxx@gmail.com',
        'xxxxxxx@gmail.com',
        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.

Marz15
  • 21
  • 1
  • 4

2 Answers2

3
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 ...).

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Hi thank you Steffen Ullrich, although I’m pretty sure my network and my local security products haven’t changed recently but I’m not entirely sure. With this information how can I solve the problem???? – Marz15 Jan 11 '21 at 06:57
  • @Marz15: *"With this information how can I solve the problem?"*. First you need to find out more about what is happening exactly and where it is happening. Use [set_debuglevel](https://docs.python.org/3/library/smtplib.html#smtplib.SMTP.set_debuglevel) to get more information about what is happening in the SMTP dialog, specifically what the answer to EHLO is. Switch off local antivirus to disable local TLS inspection. Try to use the computer inside a different network to work around network wide TLS inspection. – Steffen Ullrich Jan 11 '21 at 07:26
  • Thank you for all of your help @SteffenUllrich, I tried a different network and it worked luckily but as suggest I wanted to figure out what was happening. I used set_debuglevel and I noticed that it said my laptop domain name or IP or DNS or something was invalid thus closing the connection then it directed me to this link [link](https://support.google.com/mail/?p=helo). Is the problem something to do with my Domain name or IP?? – Marz15 Jan 12 '21 at 08:54
  • This was exactly what it said: reply: retcode (501); Msg: b'5.5.4 HELO/EHLO argument "LAPTOP-xxxxxxxx.(my service provider) net au" invalid, closing\n5.5.4 connection.\n5.5.4 https://support.google.com/mail/?p=helo u68sm2112203pfb.70 - gsmtp' – Marz15 Jan 12 '21 at 08:56
  • @Marz15: I think the link clearly says what is expected: *"This identification should be the sending machine's __fully-qualified domain name__ (like mail.example.com), or if that’s not available, its __IP address__. Any other value is considered invalid and may cause your email to be rejected."*. My guess is that the name you give in the EHLO does not actually resolve to an IP address or not to the IP address used. Use the [local_hostname](https://docs.python.org/3/library/smtplib.html#smtplib.SMTP) argument to fix it. – Steffen Ullrich Jan 12 '21 at 12:15
  • Thank you for your help @SteffenUllrich, After using the local_hostname, the code worked like a charm. Thank you for all your advice. – Marz15 Jan 19 '21 at 08:54
  • I do have one question, is there anyway to not specify the local host name in the code. This is because if I used another computer with a different host name, I would have to manually enter the host name into the code. Is there anyway to automatically detect the local host name from any computer I log into (so I don’t have to enter the local host name)??? – Marz15 Jan 19 '21 at 09:00
  • @Marz15: There is no reliable way to detect the hostname for your external visible IP address from a computer somewhere in the internal network. – Steffen Ullrich Jan 19 '21 at 16:22
  • Kk thanks Steffen Ullrich for all of your help and advice!! – Marz15 Jan 23 '21 at 03:37
0

I had the same problem: smtplib working properly and then returning this same error, without any change in the server (outlook). It just so happens that I had refactored my code, and the function that sent the email was deeper in the modules. If I used the old way, it worked! When I turned off the anti-virus (AVG in my case), all worked just fine! So, for some reason, the code refactoring made the sending procedure to be caught by anti-virus.

milcent
  • 11
  • 2