classification
Title: smtplib.py doesn't capitalize EHLO.
Type: Stage:
Components: email, Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Lord Anton Hvornum, barry, maciej.szulik, r.david.murray
Priority: normal Keywords:

Created on 2017-03-20 14:32 by Lord Anton Hvornum, last changed 2017-04-04 20:09 by maciej.szulik.

Messages (6)
msg289886 - (view) Author: Lord Anton Hvornum (Lord Anton Hvornum) Date: 2017-03-20 14:32
```
  File "mail.py", line 9, in <module>
    smtp_server.starttls(context)
  File "/usr/lib/python3.6/smtplib.py", line 748, in starttls
    self.ehlo_or_helo_if_needed()
  File "/usr/lib/python3.6/smtplib.py", line 600, in ehlo_or_helo_if_needed
    (code, resp) = self.helo()
  File "/usr/lib/python3.6/smtplib.py", line 429, in helo
    (code, msg) = self.getreply()
  File "/usr/lib/python3.6/smtplib.py", line 393, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
```

This happens due to the server expecting commands (like EHLO, STARTTLS) being strict upper-case. And when the SMTP command isn't, it drops us.

This is a rare edge case since most mail servers handles shady client data in numerous different ways (such as gmail never sending QUIT for instance).

I don't know of a work-around for this and the documentation states `EHLO` is being sent (https://docs.python.org/3/library/smtplib.html), so I guess the lib assumes that's the case as well.
msg289888 - (view) Author: Lord Anton Hvornum (Lord Anton Hvornum) Date: 2017-03-20 14:59
Turns out, this goes for a lot more commands, such as:

```
Traceback (most recent call last):
  File "mail.py", line 12, in <module>
    smtp_server.sendmail(fromaddr, toaddrs, msg)
  File "/usr/lib/python3.6/smtplib.py", line 866, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
```

The command that the server refused was:

    mail FROM:<my_mail@gmail.com> size=11

Again, this is mostly because traditionally server commands are upper case (even tho RFC 821 defines that they can be any syntax, such as `mAiL FroM`).

But it would be nice if Python could keep consistency in it's syntax IMHO.
msg289889 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-03-20 15:07
It is interesting that in all the years smtplib has been in use, this is the first time (as far as I know) this has been reported as a problem.  I don't see any reason to object to changing it to send the commands in upper case, but the server you are talking to is definitely out of spec with the RFC :)
msg289890 - (view) Author: Lord Anton Hvornum (Lord Anton Hvornum) Date: 2017-03-20 15:10
Seeing as I'm the one who built the server, it sure is out of spec :)

I could also quickly correct for this "issue" server-side.
So this is more of a "style guideline" change client-side - If no one opposes of keeping commands stylistically the same.
msg289891 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-03-20 16:51
On the other hand, the current mixed case sending found a bug in your code, so it has some value.  I'm neither in favor nor in objection to the change, at this point.
msg289907 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2017-03-20 23:16
Is EHLO the only command sent in lower case?  I think it might not be.

I suppose I'm a solid ±0 on changing this (how's that for a completely neutral endorsement?).  I won't do the change myself, but I'd review a pull request.
History
Date User Action Args
2017-04-04 20:09:22maciej.szuliksetnosy: + maciej.szulik
2017-03-20 23:16:41barrysetmessages: + msg289907
2017-03-20 16:51:41r.david.murraysetmessages: + msg289891
2017-03-20 15:10:53Lord Anton Hvornumsetmessages: + msg289890
2017-03-20 15:07:54r.david.murraysetnosy: + barry, r.david.murray
messages: + msg289889
components: + email
2017-03-20 14:59:16Lord Anton Hvornumsetmessages: + msg289888
2017-03-20 14:32:53Lord Anton Hvornumcreate