Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)

Unified Diff: Lib/smtplib.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/shutil.py ('k') | Lib/ssl.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/smtplib.py Tue Jul 26 09:37:46 2011 +0300
+++ b/Lib/smtplib.py Mon Jul 25 09:47:18 2011 -0400
@@ -49,7 +49,6 @@
import email.generator
import base64
import hmac
-import copy
from email.base64mime import body_encode as encode_base64
from sys import stderr
@@ -133,25 +132,24 @@
combination provided.
"""
-def quoteaddr(addrstring):
+def quoteaddr(addr):
"""Quote a subset of the email addresses defined by RFC 821.
Should be able to handle anything email.utils.parseaddr can handle.
"""
- displayname, addr = email.utils.parseaddr(addrstring)
- if (displayname, addr) == ('', ''):
- # parseaddr couldn't parse it, use it as is and hope for the best.
- if addrstring.strip().startswith('<'):
- return addrstring
- return "<%s>" % addrstring
- return "<%s>" % addr
-
-def _addr_only(addrstring):
- displayname, addr = email.utils.parseaddr(addrstring)
- if (displayname, addr) == ('', ''):
- # parseaddr couldn't parse it, so use it as is.
- return addrstring
- return addr
+ m = (None, None)
+ try:
+ m = email.utils.parseaddr(addr)[1]
+ except AttributeError:
+ pass
+ if m == (None, None): # Indicates parse failure or AttributeError
+ # something weird here.. punt -ddm
+ return "<%s>" % addr
+ elif m is None:
+ # the sender wants an empty return address
+ return "<>"
+ else:
+ return "<%s>" % m
# Legacy method kept for backward compatibility.
def quotedata(data):
@@ -508,14 +506,14 @@
def verify(self, address):
"""SMTP 'verify' command -- checks for address validity."""
- self.putcmd("vrfy", _addr_only(address))
+ self.putcmd("vrfy", quoteaddr(address))
return self.getreply()
# a.k.a.
vrfy = verify
def expn(self, address):
"""SMTP 'expn' command -- expands a mailing list."""
- self.putcmd("expn", _addr_only(address))
+ self.putcmd("expn", quoteaddr(address))
return self.getreply()
# some useful methods
@@ -678,7 +676,7 @@
msg may be a string containing characters in the ASCII range, or a byte
string. A string is encoded to bytes using the ascii codec, and lone
- \\r and \\n characters are converted to \\r\\n characters.
+ \r and \n characters are converted to \r\n characters.
If there has been no previous EHLO or HELO command this session, this
method tries ESMTP EHLO first. If the server does ESMTP, message size
@@ -761,49 +759,24 @@
"""Converts message to a bytestring and passes it to sendmail.
The arguments are as for sendmail, except that msg is an
- email.message.Message object. If from_addr is None or to_addrs is
- None, these arguments are taken from the headers of the Message as
- described in RFC 2822 (a ValueError is raised if there is more than
- one set of 'Resent-' headers). Regardless of the values of from_addr and
- to_addr, any Bcc field (or Resent-Bcc field, when the Message is a
- resent) of the Message object won't be transmitted. The Message
- object is then serialized using email.generator.BytesGenerator and
- sendmail is called to transmit the message.
-
+ email.message.Message object. If from_addr is None, the from_addr is
+ taken from the 'From' header of the Message. If to_addrs is None, its
+ value is composed from the addresses listed in the 'To', 'CC', and
+ 'Bcc' fields. Regardless of the values of from_addr and to_addr, any
+ Bcc field in the Message object is deleted. The Message object is then
+ serialized using email.generator.BytesGenerator and sendmail is called
+ to transmit the message.
"""
- # 'Resent-Date' is a mandatory field if the Message is resent (RFC 2822
- # Section 3.6.6). In such a case, we use the 'Resent-*' fields. However,
- # if there is more than one 'Resent-' block there's no way to
- # unambiguously determine which one is the most recent in all cases,
- # so rather than guess we raise a ValueError in that case.
- #
- # TODO implement heuristics to guess the correct Resent-* block with an
- # option allowing the user to enable the heuristics. (It should be
- # possible to guess correctly almost all of the time.)
- resent =msg.get_all('Resent-Date')
- if resent is None:
- header_prefix = ''
- elif len(resent) == 1:
- header_prefix = 'Resent-'
- else:
- raise ValueError("message has more than one 'Resent-' header block")
if from_addr is None:
- # Prefer the sender field per RFC 2822:3.6.2.
- from_addr = (msg[header_prefix+'Sender']
- if (header_prefix+'Sender') in msg
- else msg[header_prefix+'From'])
+ from_addr = msg['From']
if to_addrs is None:
- addr_fields = [f for f in (msg[header_prefix+'To'],
- msg[header_prefix+'Bcc'],
- msg[header_prefix+'Cc']) if f is not None]
+ addr_fields = [f for f in (msg['To'], msg['Bcc'], msg['CC'])
+ if f is not None]
to_addrs = [a[1] for a in email.utils.getaddresses(addr_fields)]
- # Make a local copy so we can delete the bcc headers.
- msg_copy = copy.copy(msg)
- del msg_copy['Bcc']
- del msg_copy['Resent-Bcc']
+ del msg['Bcc']
with io.BytesIO() as bytesmsg:
g = email.generator.BytesGenerator(bytesmsg)
- g.flatten(msg_copy, linesep='\r\n')
+ g.flatten(msg, linesep='\r\n')
flatmsg = bytesmsg.getvalue()
return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,
rcpt_options)
« no previous file with comments | « Lib/shutil.py ('k') | Lib/ssl.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+