Skip to content

Commit

Permalink
Merge pull request #87 from RaHus/86_debug_include_bcc
Browse files Browse the repository at this point in the history
`DebugMailer` optionally include bcc information on send
  • Loading branch information
mmerickel committed Oct 22, 2017
2 parents 789f2af + d0ca737 commit 863cc02
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -140,3 +140,5 @@ Contributors
- Ingmar Steen, 2015-08-20

- Daniel Kraus, 2015-08-27

- Rami Chousein 2017-10-17
2 changes: 2 additions & 0 deletions docs/index.rst
Expand Up @@ -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
Expand Down Expand Up @@ -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
----------
Expand Down
12 changes: 10 additions & 2 deletions pyramid_mailer/mailer.py
Expand Up @@ -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.'):
Expand All @@ -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
Expand All @@ -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'
Expand Down
15 changes: 13 additions & 2 deletions pyramid_mailer/tests/test_mailer.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 863cc02

Please sign in to comment.