Skip to content

Commit

Permalink
Fixed email creation bug in MailNotifier.
Browse files Browse the repository at this point in the history
All text is now encoded as utf8 before it goes into the message object.
  • Loading branch information
brasse committed Mar 12, 2010
1 parent 0b7f431 commit 5f9b2e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
15 changes: 9 additions & 6 deletions buildbot/status/mail.py
Expand Up @@ -26,6 +26,8 @@

VALID_EMAIL = re.compile("[a-zA-Z0-9\.\_\%\-\+]+@[a-zA-Z0-9\.\_\%\-]+.[a-zA-Z]{2,6}")

ENCODING = 'utf8'

class Domain(util.ComparableMixin):
implements(interfaces.IEmailLookup)
compare_attrs = ["domain"]
Expand Down Expand Up @@ -403,10 +405,10 @@ def getCustomMesgData(self, mode, name, build, results, master_status):

def createEmail(self, msgdict, builderName, projectName, results,
patch=None, logs=None):
text = msgdict['body']
text = msgdict['body'].encode(ENCODING)
type = msgdict['type']
if 'subject' in msgdict:
subject = msgdict['subject']
subject = msgdict['subject'].encode(ENCODING)
else:
subject = self.subject % { 'result': Results[results],
'projectName': projectName,
Expand All @@ -418,10 +420,10 @@ def createEmail(self, msgdict, builderName, projectName, results,

if patch or logs:
m = MIMEMultipart()
m.attach(MIMEText(text, type))
m.attach(MIMEText(text, type, ENCODING))
else:
m = Message()
m.set_payload(text)
m.set_payload(text, ENCODING)
m.set_type("text/%s" % type)

m['Date'] = formatdate(localtime=True)
Expand All @@ -430,7 +432,7 @@ def createEmail(self, msgdict, builderName, projectName, results,
# m['To'] is added later

if patch:
a = MIMEText(patch[1])
a = MIMEText(patch[1].encode(ENCODING), _charset=ENCODING)
a.add_header('Content-Disposition', "attachment",
filename="source patch")
m.attach(a)
Expand All @@ -439,7 +441,8 @@ def createEmail(self, msgdict, builderName, projectName, results,
name = "%s.%s" % (log.getStep().getName(),
log.getName())
if self._shouldAttachLog(log.getName()) or self._shouldAttachLog(name):
a = MIMEText(log.getText())
a = MIMEText(log.getText().encode(ENCODING),
_charset=ENCODING)
a.add_header('Content-Disposition', "attachment",
filename=name)
m.attach(a)
Expand Down
31 changes: 30 additions & 1 deletion buildbot/test/unit/test_status_mail_MailNotifier.py
Expand Up @@ -5,12 +5,41 @@
from buildbot.status.builder import SUCCESS
from buildbot.status.mail import MailNotifier

class FakeLog(object):
def __init__(self, text):
self.text = text

def getName(self):
return 'log-name'

def getStep(self):
class FakeStep():
def getName(self):
return 'step-name'
return FakeStep()

def getText(self):
return self.text

class TestMailNotifier(unittest.TestCase):
def test_sendMessage_message_contains_unicode(self):
def test_createEmail_message_without_patch_and_log_contains_unicode(self):
msgdict = dict(body=u'Unicode body with non-ascii (åäö).',
type='plain')
mn = MailNotifier('from@example.org')
m = mn.createEmail(msgdict, u'builder-näme', u'project-näme', SUCCESS)
try:
print m.as_string()
except UnicodeEncodeError:
self.fail('Failed to call as_string() on email message.')

def test_createEmail_message_with_patch_and_log_contains_unicode(self):
msgdict = dict(body=u'Unicode body with non-ascii (åäö).',
type='plain')
patch = ['', u'åäö', '']
logs = [FakeLog(u'Unicode log with non-ascii (åäö).')]
mn = MailNotifier('from@example.org', addLogs=True)
m = mn.createEmail(msgdict, u'builder-näme', u'project-näme', SUCCESS,
patch, logs)
try:
m.as_string()
except UnicodeEncodeError:
Expand Down

0 comments on commit 5f9b2e7

Please sign in to comment.