You are here: Home Blog Debugging with MockMailHost

Debugging with MockMailHost

by Ross Patterson last modified May 12, 2008 11:16 AM
— filed under: , ,

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)
'MailHost'
>>> 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!

Document Actions

MaildropHost

Posted by http://naro.myopenid.com/ at May 12, 2008 11:22 PM
I'm using MaildropHost for debugging emails. Until you start maildrophost system process, no emails will be sent from the site (except poor products which sends email directly through smtp module). You can check contents of the email in the spool directory and simply remove it if you don't want to send email from site.

Sounds much better

Posted by Ross Patterson at May 13, 2008 10:39 AM
The next time I have to debug emails I'll try that. Thanks!

You may also need registerUtility

Posted by http://silviot.myopenid.com/ at Nov 28, 2008 03:58 AM
Thanks for the tip.
Since my code calls getToolByName(obj, 'MailHost')
I had to add the following to register with the sitemanager.

from Products.MailHost.interfaces import IMailHost
sm = self.portal.getSiteManager()
sm.registerUtility(self.portal.MailHost, provided=IMailHost)

You may also need registerUtility

Posted by Ross Patterson at Nov 28, 2008 11:41 AM
Thanks! I'd run into that before and used the same solution. I've been meaning to document that here. Thanks for doing it for me!
Contact

me@rpatterson.net

IRC: zenwryly@irc.freenode.net
GTalk: merpattersonnet@gmail.com
Yahoo IM: patterson_ross
AIM: rosspatters
MSN: me@rpatterson.net
Skype: merpattersonnet

831-338-9197
Fax: 831-480-5894

PO Box 7775 #10587
San Francisco, CA
94120-7775