Debugging with MockMailHost

Using the mock MailHost on a real instance

I often find the need to debug user registration or other functionality that involves sending mail through the MailHost where I either don't want the mail to go out, or I want to intercept it for examination or both.  Using "zopectl debug" I replace the MailHost with a MockMailHost as follows::

>>> app.Plone._delObject('MailHost')
>>> from Products.PasswordResetTool.tests import utils
>>> mail_host = utils.MockMailHost('MailHost')
>>> app.Plone._setObject('MailHost', mail_host)
>>> app.Plone.MailHost
<MockMailHost at /Plone/MailHost>
>>> import transaction
>>> transaction.commit()

Then using PDBDebugMode or "zopectl debug" again, the messages sent through the MailHost can be examined as follows::

(Pdb) print self.MailHost.messages[-1]

I have long intended to build a package with a GenericSetup profile that will do this for me, but I wanted to get this out there for now.  Enjoy!

Updated on 12 May 2008

Imported from Plone on Mar 15, 2021. The date for this update is the last modified date in Plone.


comments powered by Disqus