New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
smtplib gets out of sync if server returns a 421 status #49963
Comments
RFC821 upon which smtplib was originally based does not define a 421 Subsequent extensions in RFC2821 (and now RFC5321) define situations An smtplib.SMTP() instance is created and its sendmail() method is However, with the Postfix server at least, after 20 rejects, the server The caller may see the exception as retryable and may retry the send |
So, if I understand the issue and the RFC correctly, when receiving a What return code would be give back if we get 421 in other situations?. |
I'm not completely sure about this, but here's my thoughts. In the If the 421 comes at another time, I think the current process does the I have attached a patch against the 2.6.1 smtplib.py which I think does |
Smptlib is documented as RFC821/1869 based. So this is a request for a new feature. I presume more would be required to bring it up to 2821/5321. Assuming so, what is the rationale for adding just one new feature. This would mean tacking something like "and RFC 2821 for 421 status" on the end of "For details of SMTP and ESMTP operation, consult RFC 821 (Simple Mail Transfer Protocol) and RFC 1869 (SMTP Service Extensions)." Can a test be written for this? There seem to be some coded status tests already. I |
The latest relevant RFC is 5321: http://www.faqs.org/rfcs/rfc5321.html smtplib should be reviewed for compliance with this updated spec. |
Here is a patch along with test case update which can be cleanly applied into default. |
I think this should be considered a bug fix. While it is true that we are handling something that is a "more advanced" feature of the smpt protocl than what we are (currently) formally supporting in the module, the reality is that if one uses smtplib to communicate with a foreign mail server, you currently get unhandleable errors even if the library sticks to the currently supported RFC level. I think that the fact that smtplib does the wrong then when it receives an unexpected error code followed by a connection close is a bug in smtplib regardless of what RFC level we are talking about. In fact, this is one instance of a larger problem. I have also seen a server that returns a 451 error code at the end of a data command *and then disconnects*. I think that is out of spec even with the newer RFCs, but it is something that happens in the real world and so we should handle it. The general problem in smtplib is that it does an rset on error, and if the other end has closed the connection, the rset raises a ServerDisconnectedError, masking the error code that the server actually reported. This also should be fixed. Clearly for 421 we should be doing a close and not an rset, and I think we may as well go ahead and do that even if we haven't upgraded the library to fully comply with the newer RFC yet, especially since we've got a patch here. It is not a publicly visible API change, so I don't think the "new feature" rule needs to be applied strictly. Correctly handling the case of rset finding the socket already closed would also have fixed this bug, and we should make that fix as well, since it seems that servers may unexpectedly close the connection after any error. |
Given that David is usually more conservative about calling things bugs than I am, knows more about this subject than me, and has explained that the change will make smptlib act more sanely in the face of external errors, I am reversing myself and agreeing with him. The patch seems to tighten the test in a way relevant to the code change. I see that there are several open smptlib issues. |
Sure, a new issue for making sure smtplib conforms to 5321 would be good (we already did it for smptd; hopefully we didn't miss anything). For the moment 5321 is the standard. There's a new one coming, where utf-8 will be allowed, but we're not quite ready to tackle that yet (soon, though, hopefully). |
I had to rewrite the tests for 3.2, and I added tests to test all three cases where 421 is returned. I'm in the process of merging this, but I'll upload the patch so that Kushal can see the test structure for the followon patch. |
New changeset bc538bc8e65d by R David Murray in branch '3.2': New changeset cf5c40025af2 by R David Murray in branch '3.3': New changeset ce4154268c47 by R David Murray in branch 'default': |
New changeset fbf54209de75 by R David Murray in branch '3.2': New changeset 0de74602692f by R David Murray in branch '3.3': New changeset 6dc948e4ea8f by R David Murray in branch 'default': |
Thanks Mark and Kushal. |
New changeset d068fcbe5009 by R David Murray in branch '3.3': New changeset fcef6a33de17 by R David Murray in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: