Skip to content

Commit

Permalink
Merge r117102 from trunk; encode usernames and passwords to utf-8 if …
Browse files Browse the repository at this point in the history
…unicode
  • Loading branch information
Martijn Pieters committed Oct 1, 2010
1 parent 4a94c95 commit e1dd8f7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -4,6 +4,9 @@ Change history
3.5.2 (unreleased)
------------------

- Handle unicode usernames and passwords, encoding them to UTF-8. Fix for
https://bugs.launchpad.net/zope.sendmail/+bug/597143


3.5.1 (2009-01-26)
------------------
Expand Down
7 changes: 6 additions & 1 deletion src/zope/sendmail/mailer.py
Expand Up @@ -64,7 +64,12 @@ def send(self, fromaddr, toaddrs, message):

if connection.does_esmtp:
if self.username is not None and self.password is not None:
connection.login(self.username, self.password)
username, password = self.username, self.password
if isinstance(username, unicode):
username = username.encode('utf-8')
if isinstance(password, unicode):
password = password.encode('utf-8')
connection.login(username, password)
elif self.username:
raise RuntimeError('Mailhost does not support ESMTP but a username '
'is configured')
Expand Down
27 changes: 27 additions & 0 deletions src/zope/sendmail/tests/test_mailer.py
Expand Up @@ -112,6 +112,30 @@ def test_send_auth(self):
self.assert_(self.smtp.quitted)
self.assert_(self.smtp.closed)

def test_send_auth_unicode(self):
fromaddr = 'me@example.com'
toaddrs = ('you@example.com', 'him@example.com')
msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n'
self.mailer.username = u'f\u00f8\u00f8' # double o slash
self.mailer.password = u'\u00e9vil' # e acute
self.mailer.hostname = 'spamrelay'
self.mailer.port = 31337
self.mailer.send(fromaddr, toaddrs, msgtext)
self.assertEquals(self.smtp.username, 'f\xc3\xb8\xc3\xb8')
self.assertEquals(self.smtp.password, '\xc3\xa9vil')

def test_send_auth_nonascii(self):
fromaddr = 'me@example.com'
toaddrs = ('you@example.com', 'him@example.com')
msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n'
self.mailer.username = 'f\xc3\xb8\xc3\xb8' # double o slash
self.mailer.password = '\xc3\xa9vil' # e acute
self.mailer.hostname = 'spamrelay'
self.mailer.port = 31337
self.mailer.send(fromaddr, toaddrs, msgtext)
self.assertEquals(self.smtp.username, 'f\xc3\xb8\xc3\xb8')
self.assertEquals(self.smtp.password, '\xc3\xa9vil')

def test_send_failQuit(self):
self.mailer.smtp.fail_on_quit = True
try:
Expand Down Expand Up @@ -162,6 +186,9 @@ def test_send_auth(self):
# here, so pass.
pass

test_send_auth_unicode = test_send_auth
test_send_auth_nonascii = test_send_auth

def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestSMTPMailer))
Expand Down

0 comments on commit e1dd8f7

Please sign in to comment.