[Zope3-Users] Re: Render ZPT template to send rich-text HTML mail
Philipp von Weitershausen
philipp at weitershausen.de
Thu Jun 1 15:20:42 EDT 2006
thealx at poczta.onet.pl wrote:
> Hi, I need to send some rich-text (HTML) mail, containing information
> based on many objects. It would be nice to render it using ZPT
> provided by Zope instead of manual creation of HTML text.
>
> Can I (ab)use ZPT for such purpose? I need to design ZPT template and
> write corresponding View object. How to wire together the ZPT
> template and corresponding View object to render a HTML message and
> send it using mail utility?
Let's say that the 'mail.html' view provides the HTML for the email body
(whether that's ZPT, Python, or whatever doesn't matter), then you can
simply do:
>>> from zope.component import getMultiAdapter
>>> view = getMultiAdapter((obj, request), name="@@mail.html")
>>> html_email_body = view()
>>> from email.MIMEText import MIMEText
>>> message = MIMETExt(html_email_body.encode('utf-8'),
... 'text/html', 'utf-8')
>>> # etc.
>>> from zope.component import getUtility
>>> mailer = getUtility(IMailDelivery, 'your-delivery-utility')
>>> mailer.send(from, to, message.as_string())
> Could you suggest some points to start with?
Buy my book?
Philipp
More information about the Zope3-users
mailing list