issue 630, encoding and plain emails, thanks spametki

This commit is contained in:
Massimo Di Pierro
2012-01-21 17:28:51 -06:00
parent b03c40f442
commit 44c7f576e5
2 changed files with 33 additions and 12 deletions
+1 -1
View File
@@ -1 +1 @@
Version 1.99.4 (2012-01-21 17:26:09) stable Version 1.99.4 (2012-01-21 17:28:44) stable
+32 -11
View File
@@ -241,6 +241,7 @@ class Mail(object):
bcc=None, bcc=None,
reply_to=None, reply_to=None,
encoding='utf-8', encoding='utf-8',
raw=False,
headers={} headers={}
): ):
""" """
@@ -320,11 +321,29 @@ class Mail(object):
else: else:
return key return key
# encoded or raw text
def encoded_or_raw(text):
if raw:
text = encode_header(text)
return text
if not isinstance(self.settings.server, str): if not isinstance(self.settings.server, str):
raise Exception('Server address not specified') raise Exception('Server address not specified')
if not isinstance(self.settings.sender, str): if not isinstance(self.settings.sender, str):
raise Exception('Sender address not specified') raise Exception('Sender address not specified')
payload_in = MIMEMultipart.MIMEMultipart('mixed')
if not raw:
payload_in = MIMEMultipart.MIMEMultipart('mixed')
else:
# no encoding configuration for raw messages
if isinstance(message, basestring):
text = message.decode(encoding).encode('utf-8')
else:
text = message.read().decode(encoding).encode('utf-8')
# No charset passed to avoid transport encoding
# NOTE: some unicode encoded strings will produce
# unreadable mail contents.
payload_in = MIMEText.MIMEText(text)
if to: if to:
if not isinstance(to, (list,tuple)): if not isinstance(to, (list,tuple)):
to = [to] to = [to]
@@ -346,7 +365,8 @@ class Mail(object):
else: else:
text = message text = message
html = None html = None
if not text is None or not html is None:
if (not text is None or not html is None) and (not raw):
attachment = MIMEMultipart.MIMEMultipart('alternative') attachment = MIMEMultipart.MIMEMultipart('alternative')
if not text is None: if not text is None:
if isinstance(text, basestring): if isinstance(text, basestring):
@@ -361,7 +381,7 @@ class Mail(object):
html = html.read().decode(encoding).encode('utf-8') html = html.read().decode(encoding).encode('utf-8')
attachment.attach(MIMEText.MIMEText(html, 'html',_charset='utf-8')) attachment.attach(MIMEText.MIMEText(html, 'html',_charset='utf-8'))
payload_in.attach(attachment) payload_in.attach(attachment)
if attachments is None: if (attachments is None) or raw:
pass pass
elif isinstance(attachments, (list, tuple)): elif isinstance(attachments, (list, tuple)):
for attachment in attachments: for attachment in attachments:
@@ -546,22 +566,23 @@ class Mail(object):
else: else:
# no cryptography process as usual # no cryptography process as usual
payload=payload_in payload=payload_in
payload['From'] = encode_header(self.settings.sender.decode(encoding))
payload['From'] = encoded_or_raw(self.settings.sender.decode(encoding))
origTo = to[:] origTo = to[:]
if to: if to:
payload['To'] = encode_header(', '.join(to).decode(encoding)) payload['To'] = encoded_or_raw(', '.join(to).decode(encoding))
if reply_to: if reply_to:
payload['Reply-To'] = encode_header(reply_to.decode(encoding)) payload['Reply-To'] = encoded_or_raw(reply_to.decode(encoding))
if cc: if cc:
payload['Cc'] = encode_header(', '.join(cc).decode(encoding)) payload['Cc'] = encoded_or_raw(', '.join(cc).decode(encoding))
to.extend(cc) to.extend(cc)
if bcc: if bcc:
to.extend(bcc) to.extend(bcc)
payload['Subject'] = encode_header(subject.decode(encoding)) payload['Subject'] = encoded_or_raw(subject.decode(encoding))
payload['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", payload['Date'] = time.strftime("%a, %d %b %Y %H:%M:%S +0000",
time.gmtime()) time.gmtime())
for k,v in headers.iteritems(): for k,v in headers.iteritems():
payload[k] = encode_header(v.decode(encoding)) payload[k] = encoded_or_raw(v.decode(encoding))
result = {} result = {}
try: try:
if self.settings.server == 'logging': if self.settings.server == 'logging':
@@ -576,12 +597,12 @@ class Mail(object):
if bcc: if bcc:
xcc['bcc'] = bcc xcc['bcc'] = bcc
from google.appengine.api import mail from google.appengine.api import mail
attachments = attachments and [(a.my_filename,a.my_payload) for a in attachments] attachments = attachments and [(a.my_filename,a.my_payload) for a in attachments if not raw]
if attachments: if attachments:
result = mail.send_mail(sender=self.settings.sender, to=origTo, result = mail.send_mail(sender=self.settings.sender, to=origTo,
subject=subject, body=text, html=html, subject=subject, body=text, html=html,
attachments=attachments, **xcc) attachments=attachments, **xcc)
elif html: elif html and (not raw):
result = mail.send_mail(sender=self.settings.sender, to=origTo, result = mail.send_mail(sender=self.settings.sender, to=origTo,
subject=subject, body=text, html=html, **xcc) subject=subject, body=text, html=html, **xcc)
else: else: