smtplib.quoteaddr() returns malformed angle-bracket address for input '<'
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Description
smtplib.quoteaddr() formats addresses for SMTP MAIL FROM: and RCPT TO: commands per RFC 821, which requires angle-bracket format (<addr>). It first delegates to email.utils.parseaddr(); when parsing succeeds, the result is correctly wrapped in <...>.
When parseaddr fails — which happens for inputs that don't resemble any recognizable email address format, like '<', '< ', or '@' — a fallback path kicks in. The fallback checks if the input starts with < and returns it verbatim, without verifying it also ends with >. This produces structurally invalid output (e.g. MAIL FROM:< instead of MAIL FROM:<>).
The existing code intentionally chooses to be lenient with unparseable input rather than raising an exception (the comment reads "use it as is and hope for the best"). Given that design choice, the output should at least be structurally valid — a half-open < with no > is neither rejecting bad input nor producing well-formed protocol output.
Reproducer
from smtplib import quoteaddr
print(repr(quoteaddr('<'))) # '<' — missing closing >
print(repr(quoteaddr('< '))) # '< ' — missing closing >
print(repr(quoteaddr('<user@example.com'))) # '<user@example.com' — missing >
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-145553
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at smtplib.quoteaddr() and inspect the fallback used when email.utils.parseaddr() cannot parse the input. Review linked PR gh-145553 before making changes, then add coverage for the malformed inputs shown in the reproducer and verify that the returned address is structurally valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100