Plone中可编程发送邮件实例
Plone中可编程发送邮件实例
http://www.315ok.org/blogfolder/72
http://www.315ok.org/logo.png
Plone中可编程发送邮件实例
Plone中可编程发送邮件实例
Sending email¶[align=left]Description[/align][align=left]How to programmatically send email in Plone[/align]
Introduction¶This document tells how to send email from Plone.
Email can be sent:
try:
host = getToolByName(self, 'MailHost')
# The `immediate` parameter causes an email to be sent immediately
# (if any error is raised) rather than sent at the transaction
# boundary or queued for later delivery.
return host.send(mail_text, immediate=True)
except SMTPRecipientsRefused:
# Don't disclose email address on failure
raise SMTPRecipientsRefused('Recipient address rejected by server')
Preparing mail text¶mail_text can be generated by calling a page template (.pt) withkeyword arguments. The values are accessed in the template asoption/keyword. For example, take a sample template:
<tal:root define="lt string:<;
gt string:>;
dummy python:request.RESPONSE.setHeader('Content-Type', 'text/plain;; charset=%s' % options['charset']);
member python:options['member'];"
>From: "<span tal:replace="python:here.email_from_name" />" <span tal:replace="structure lt"/><span tal:replace="python:here.email_from_address" /><span tal:replace="structure gt"/>
To: <span tal:replace="python:member.getProperty('email')" />
Subject: <span i18n:domain="yourproduct" i18n:translate="yoursubjectline" tal:omit-tag="">Subject Line</span>
Content-Type: text/plain; charset=<span tal:replace="python:options['charset']" />
Dear <span tal:replace="member/getFullname" />:
You can now log in as <span tal:replace="member/getId" /> at <span tal:replace="python:options['portal_url']" />
Cheers!
The website team
</tal:root>
This can be called with a member object and the portal_url:
mail_template = portal.mail_template_id
mail_text = mail_template(member=member,
portal_url=portal.absolute_url(),
charset=email_charset,
request=REQUEST)
For more complete examples (with i18n support, etc.) see the password resetmodules (particularly Products.remember.tools.registration).
[align=left]Note[/align][align=left]If you don't need to have third parties to override your email templatesit might be cleaned to use Python string templates, as XML based TALtemplates are not designed for plain text templating.[/align]
Introduction¶This document tells how to send email from Plone.
Email can be sent:
- manually, by calling MailHost;
- using a Content Rule (content rules have an email-out action by default)which can be activated by a workflow transition, for example;
- triggering email-based password reset.
try:
host = getToolByName(self, 'MailHost')
# The `immediate` parameter causes an email to be sent immediately
# (if any error is raised) rather than sent at the transaction
# boundary or queued for later delivery.
return host.send(mail_text, immediate=True)
except SMTPRecipientsRefused:
# Don't disclose email address on failure
raise SMTPRecipientsRefused('Recipient address rejected by server')
Preparing mail text¶mail_text can be generated by calling a page template (.pt) withkeyword arguments. The values are accessed in the template asoption/keyword. For example, take a sample template:
<tal:root define="lt string:<;
gt string:>;
dummy python:request.RESPONSE.setHeader('Content-Type', 'text/plain;; charset=%s' % options['charset']);
member python:options['member'];"
>From: "<span tal:replace="python:here.email_from_name" />" <span tal:replace="structure lt"/><span tal:replace="python:here.email_from_address" /><span tal:replace="structure gt"/>
To: <span tal:replace="python:member.getProperty('email')" />
Subject: <span i18n:domain="yourproduct" i18n:translate="yoursubjectline" tal:omit-tag="">Subject Line</span>
Content-Type: text/plain; charset=<span tal:replace="python:options['charset']" />
Dear <span tal:replace="member/getFullname" />:
You can now log in as <span tal:replace="member/getId" /> at <span tal:replace="python:options['portal_url']" />
Cheers!
The website team
</tal:root>
This can be called with a member object and the portal_url:
mail_template = portal.mail_template_id
mail_text = mail_template(member=member,
portal_url=portal.absolute_url(),
charset=email_charset,
request=REQUEST)
For more complete examples (with i18n support, etc.) see the password resetmodules (particularly Products.remember.tools.registration).
[align=left]Note[/align][align=left]If you don't need to have third parties to override your email templatesit might be cleaned to use Python string templates, as XML based TALtemplates are not designed for plain text templating.[/align]