Skip to content
Snippets Groups Projects
Commit 425fa7d1 authored by Vermaat's avatar Vermaat
Browse files

Encode unicode email addresses before sending mail

parent 5b9c2c83
No related branches found
No related tags found
No related merge requests found
...@@ -106,6 +106,25 @@ class Scheduler() : ...@@ -106,6 +106,25 @@ class Scheduler() :
#TODO: Handle Connection errors in a try, except clause #TODO: Handle Connection errors in a try, except clause
#Expected errors: socket.error #Expected errors: socket.error
def encode_address(address):
"""
Unfortunately, smtplib can only handle ASCII email addresses.
The domain name part should be punycode-encoded. The part before
the '@' should be encoded as ASCII if possible or as UTF8 if the
first-hop mail server supports this.
This is way to complicated, so we just encode the entire address
as ASCII or punycode if that fails.
https://bugs.python.org/issue20084
https://bugs.python.org/issue20083
"""
try:
return address.encode('ascii')
except UnicodeEncodeError:
return address.encode('idna')
from_address = 'Mutalyzer batch job <%s>' % ( from_address = 'Mutalyzer batch job <%s>' % (
settings.BATCH_NOTIFICATION_EMAIL or settings.EMAIL) settings.BATCH_NOTIFICATION_EMAIL or settings.EMAIL)
download_url = website.url_for('batch_job_result', download_url = website.url_for('batch_job_result',
...@@ -126,7 +145,7 @@ Mutalyzer batch scheduler""" % download_url) ...@@ -126,7 +145,7 @@ Mutalyzer batch scheduler""" % download_url)
message["Subject"] = "Result of your Mutalyzer batch job" message["Subject"] = "Result of your Mutalyzer batch job"
message["From"] = from_address message["From"] = from_address
message["To"] = mailTo message["To"] = encode_address(mailTo)
try: try:
smtpInstance = smtplib.SMTP('localhost') smtpInstance = smtplib.SMTP('localhost')
...@@ -140,7 +159,8 @@ Mutalyzer batch scheduler""" % download_url) ...@@ -140,7 +159,8 @@ Mutalyzer batch scheduler""" % download_url)
return return
try: try:
smtpInstance.sendmail(from_address, mailTo, message.as_string()) smtpInstance.sendmail(from_address, encode_address(mailTo),
message.as_string())
except smtplib.SMTPRecipientsRefused as e: except smtplib.SMTPRecipientsRefused as e:
for address, (code, error) in e.recipients.items(): for address, (code, error) in e.recipients.items():
print 'Could not send email to %s: (%s) %s' % (address, print 'Could not send email to %s: (%s) %s' % (address,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment