Skip to content

Commit

Permalink
Added tests for cc, bcc
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeulette committed Sep 4, 2024
1 parent 44d961b commit c626a58
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/imio/helpers/emailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,13 @@ def send_email(eml, subject, mfrom, mto, mcc=None, mbcc=None, replyto=None, imme
# Add extra headers
_addHeaders(eml, Subject=Header(subject, charset),
**dict((k, Header(v, charset)) for k, v in iteritems(addrs) if v))
# Handle all recipients to add bcc, considered as bcc and not removed from header
# Handle all recipients to add bcc: smtplib sends to all but a recipient not found in header is considered as bcc...
all_recipients = [formataddr(pair) for pair in
getaddresses([addrs['To'], addrs['Cc'], mbcc])]
all_recipients = [a for a in all_recipients if a]
try:
# send is protected by permission 'Use mailhost'
# secureSend is deprecated and patched in Products/CMFPlone/patches/securemailhost.py
# send remove from headers bcc but if in all_recipients, it is handled as bcc in email ?!
# mail_host._send(addrs['From'], all_recipients, eml.as_string(), immediate=immediate)
# use send to work with MockMailHost
# send remove from headers bcc but if in all_recipients, it is handled as bcc in email...
mail_host.send(eml.as_string(), all_recipients, addrs['From'], immediate=immediate, charset=charset)
except (socket.error, SMTPException) as e:
logger.error(u"Cannot send email to '{}' with subject '{}': {}".format(mto, subject, e))
Expand Down
34 changes: 32 additions & 2 deletions src/imio/helpers/tests/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ def test_send_email(self):
filepath = os.path.join(path, barcode_resource)
add_attachment(eml, 'barcode.png', filepath=filepath)
mail_host = get_mail_host()
MockMailHost.secureSend = MockMailHost.send
mail_host.reset()
if six.PY3:
# Python 3 raises an error with accented characters in emails
# see https://github.com/zopefoundation/Products.MailHost/issues/29
# and https://stackoverflow.com/questions/52133735/how-do-i-send-email-to-addresses-with-non-ascii-characters-in-python
# and https://stackoverflow.com/questions/52133735/how-do-i-send-email-to-addresses-with-non-ascii-
# characters-in-python
send_email(eml, 'Email subject hé hé', 'noreply@from.org', 'dest@to.org')
self.assertIn(b'Subject: =?utf-8?q?Email_subject_h=C3=A9_h=C3=A9?=', mail_host.messages[0])
self.assertIn(b'From: noreply@from.org', mail_host.messages[0])
Expand All @@ -87,6 +87,7 @@ def test_send_email(self):
# self.assertIn('To: d\xc3\xa8st@to.org\n', mail_host.messages[0])
self.assertIn('To: =?utf-8?q?d=C3=A8st=40to=2Eorg?=\n', mail_host.messages[0])
mail_host.reset()
# multiple recipients
send_email(eml, u'Email subject', '<noreply@from.org>', ['dest@to.org', 'Stéphan Geulette <seg@to.org>'])
if six.PY3:
self.assertIn(b'To: dest@to.org, =?utf-8?q?St=C3=A9phan_Geulette?= <seg@to.org>', mail_host.messages[0])
Expand All @@ -95,6 +96,7 @@ def test_send_email(self):
self.assertIn('To: =?utf-8?q?dest=40to=2Eorg=2C_=3D=3Futf-8=3Fq=3FSt=3DC3=3DA9phan=5FGeulett?=\n',
mail_host.messages[0])
mail_host.reset()
# unicode parameters
if six.PY3:
self.assertTrue(send_email(eml, u'Email subject hé hé', u'noreply@from.org', u'dest@to.org'))
self.assertTrue(send_email(eml, u'Email subject', '<noreply@from.org>',
Expand All @@ -105,6 +107,34 @@ def test_send_email(self):
# not ok if in list
self.assertRaises(UnicodeEncodeError, send_email, eml, u'Email subject', '<noreply@from.org>',
['dest@to.org', u'Stéphan Geulette <seg@to.org>'])
# cc, bcc and reply_to
mail_host.reset()
call_args = []
orig_mmh_send = MockMailHost.send

def mock_send(*args, **kwargs):
call_args.append(args)
orig_mmh_send(*args, **kwargs)

MockMailHost.send = mock_send
send_email(eml, 'Email subject', 'noreply@from.org', 'dest@to.org', mcc='copy@to.org', mbcc='bcc@to.org',
replyto='reply@to.org')
MockMailHost.send = orig_mmh_send
# all recipients are in mto parameter
self.assertListEqual(call_args[0][2], ['dest@to.org', 'copy@to.org', 'bcc@to.org'])
if six.PY3:
self.assertIn(b'From: noreply@from.org', mail_host.messages[0])
self.assertIn(b'To: dest@to.org', mail_host.messages[0])
self.assertIn(b'Cc: =?utf-8?q?copy=40to=2Eorg?=', mail_host.messages[0])
self.assertIn(b'reply-to: =?utf-8?q?reply=40to=2Eorg?=', mail_host.messages[0])
self.assertNotIn(b'=?utf-8?q?bcc=40to=2Eorg?=', mail_host.messages[0])
else:
self.assertIn('From: noreply@from.org', mail_host.messages[0])
self.assertIn('To: =?utf-8?q?dest=40to=2Eorg?=\n', mail_host.messages[0])
self.assertIn('Cc: =?utf-8?q?copy=40to=2Eorg?=\n', mail_host.messages[0])
self.assertIn('reply-to: =?utf-8?q?reply=40to=2Eorg?=\n', mail_host.messages[0])
self.assertNotIn('=?utf-8?q?bcc=40to=2Eorg?=\n', mail_host.messages[0])


def test_validate_email_address(self):
self.assertTupleEqual(validate_email_address('name@domain.org'), (u'', u'name@domain.org'))
Expand Down

0 comments on commit c626a58

Please sign in to comment.