Debugging with MockMailHost

by Ross Patterson last modified 2008-05-12T20:16:52+02:00
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!