Handle non-ASCII characters in body and subject

Change-Id: I522482c9d8cca1493206988694c8d497bc9d32d5
This commit is contained in:
Adrien Cunin 2016-08-02 16:12:32 +02:00
parent 72d281a0cd
commit a62a7ec2ae
2 changed files with 16 additions and 19 deletions

View File

@ -17,6 +17,7 @@
import csv
import email.utils
import email.mime.text
import settings
import smtplib
import sys
@ -27,7 +28,7 @@ from string import Template
class ATC(object):
def __init__(self, row):
self.lpid = row[0]
self.name = row[1]
self.name = unicode(row[1], 'utf8')
self.emails = row[2:]
@ -67,22 +68,17 @@ if __name__ == '__main__':
content = template.substitute(name=committer.name,
code=code,
signature=settings.EMAIL_SIGNATURE)
msg = (
"From: %s\r\n"
"To: %s\r\n"
"Date: %s\r\n"
"Message-ID: %s\r\n"
"Subject: %s\r\n"
"%s"
% (
settings.EMAIL_FROM,
','.join(committer.emails),
email.utils.formatdate(),
email.utils.make_msgid(),
settings.EMAIL_SUBJECT,
content))
session.sendmail(settings.EMAIL_FROM, committer.emails, msg)
msg = email.mime.text.MIMEText(content, 'plain',
'utf8')
msg["From"] = settings.EMAIL_FROM
msg["To"] = ','.join(committer.emails)
msg["Date"] = email.utils.formatdate()
msg["Message-ID"] = email.utils.make_msgid()
msg["Subject"] = settings.EMAIL_SUBJECT
session.sendmail(settings.EMAIL_FROM, committer.emails,
msg.as_string())
print "%s,ATC,%s" % (code, committer.name)
session.quit()
time.sleep(settings.EMAIL_PAUSE)

View File

@ -1,3 +1,4 @@
# coding=utf8
# Settings for the passcode-sending application
#
# Copyright 2013 Thierry Carrez <thierry@openstack.org>
@ -15,9 +16,9 @@
# License for the specific language governing permissions and limitations
# under the License.
EMAIL_SUBJECT = "Your registration code for OpenStack Summit in Portland"
EMAIL_SUBJECT = u"Your registration code for OpenStack Summit in Portland"
EMAIL_TEMPLATE = """
EMAIL_TEMPLATE = u"""
Hello $name,
As you may already know, we'll be having our next OpenStack Summit
@ -42,7 +43,7 @@ $signature
"""
EMAIL_FROM = "thierry@openstack.org"
EMAIL_SIGNATURE = "Thierry Carrez\nOpenStack Foundation"
EMAIL_SIGNATURE = u"Thierry Carrez\nOpenStack Foundation"
EMAIL_HOST = "secure.emailsrvr.com"
EMAIL_PORT = 465
EMAIL_USER = "thierry.carrez@openstack.org"