From d0ca7376da1d4470d4b70821c0bb45ad5da2da54 Mon Sep 17 00:00:00 2001 From: RamiC Date: Sat, 21 Oct 2017 23:10:10 +0300 Subject: [PATCH] `DebugMailer` optionally include bcc information on send #86 --- CONTRIBUTORS.txt | 2 ++ docs/index.rst | 2 ++ pyramid_mailer/mailer.py | 12 ++++++++++-- pyramid_mailer/tests/test_mailer.py | 15 +++++++++++++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 3b3e4ba..f35560f 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -140,3 +140,5 @@ Contributors - Ingmar Steen, 2015-08-20 - Daniel Kraus, 2015-08-27 + +- Rami Chousein 2017-10-17 \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index 4409b5d..a1ced01 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -211,6 +211,7 @@ Setting Default **mail.debug** **0** SMTP debug level **mail.sendmail_app** **/usr/sbin/sendmail** Sendmail executable **mail.sendmail_template** **{sendmail_app} -t -i -f {sender}** Template for sendmail execution +**mail.debug_include_bcc** **False** Include Bcc headers when :ref:`debugging` ========================== ==================================== =============================== **Note:** SSL will only work with **pyramid_mailer** if you are using Python @@ -392,6 +393,7 @@ out will instead get written to files so you can inspect them:: pyramid_debugtoolbar pyramid_tm +Set the ``mail.debug_include_bcc`` flag to ``True`` if you want the bcc recipients written to the file Unit tests ---------- diff --git a/pyramid_mailer/mailer.py b/pyramid_mailer/mailer.py index d2225a8..5ef65bb 100644 --- a/pyramid_mailer/mailer.py +++ b/pyramid_mailer/mailer.py @@ -35,10 +35,11 @@ class DebugMailer(object): Stores messages as files in the specified directory. """ - def __init__(self, top_level_directory): + def __init__(self, top_level_directory, include_bcc=False): if not exists(top_level_directory): makedirs(top_level_directory) self.tld = top_level_directory + self.include_bcc = include_bcc @classmethod def from_settings(cls, settings, prefix='mail.'): @@ -52,7 +53,10 @@ def from_settings(cls, settings, prefix='mail.'): if top_level_directory is None: raise ValueError("DebugMailer: must specify " "'%stop_level_directory'" % prefix) - return cls(top_level_directory) + + include_bcc = settings.get(prefix+'debug_include_bcc', False) + + return cls(top_level_directory, include_bcc) def bind(self, **kw): """Get mailer with the same server configuration but with @@ -76,6 +80,10 @@ def _send(self, message, fail_silently=False): file_part1 = datetime.now().strftime('%Y%m%d%H%M%S') file_part2 = ''.join(sample(seeds, 4)) filename = join(self.tld, '%s_%s.eml' % (file_part1, file_part2)) + + if self.include_bcc: + message.extra_headers['Bcc'] = ', '.join(message.bcc) + with open(filename, 'w') as fd: if not message.sender: message.sender = 'nobody' diff --git a/pyramid_mailer/tests/test_mailer.py b/pyramid_mailer/tests/test_mailer.py index 4351acc..bb7e3df 100644 --- a/pyramid_mailer/tests/test_mailer.py +++ b/pyramid_mailer/tests/test_mailer.py @@ -24,10 +24,10 @@ def _getTargetClass(self): from pyramid_mailer.mailer import DebugMailer return DebugMailer - def _makeOne(self, tld=None): + def _makeOne(self, tld=None, include_bcc=False): if tld is None: tld = self._makeTempdir() - return self._getTargetClass()(tld) + return self._getTargetClass()(tld, include_bcc) def _listFiles(self): from os import listdir @@ -70,6 +70,17 @@ def test__send(self): self.assertEqual(len(files), 1) self.assertEqual(files[0][-4:], '.eml') + def test__send_w_include_bcc(self): + mailer = self._makeOne(include_bcc=True) + msg = _makeMessage(bcc=['recipient@example.com']) + mailer.send_sendmail(msg) + files = self._listFiles() + + self.assertIn('Bcc', msg.extra_headers) + self.assertEqual(len(files), 1) + with open(os.path.join(self._tempdir, files[0]), 'r') as msg: + self.assertTrue('recipient@example.com' in msg.read()) + def test_default_sender(self): mailer = self._makeOne() msg = _makeMessage(sender=None)