Skip to content

Commit

Permalink
Encode username and password to utf-8 if they are unicode values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martijn Pieters committed Oct 1, 2010
1 parent 6649e0f commit c4cc14b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ CHANGES

- Added not declared, but needed test dependency on `zope.component [test]`.

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

3.7.2 (2010-04-30)
------------------

Expand Down
7 changes: 6 additions & 1 deletion src/zope/sendmail/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,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 @@ -160,6 +184,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 c4cc14b

Please sign in to comment.