Back to index

plone3  3.1.7
mail.py
Go to the documentation of this file.
00001 from Acquisition import aq_inner
00002 from OFS.SimpleItem import SimpleItem
00003 from zope.component import adapts
00004 from zope.component.interfaces import ComponentLookupError
00005 from zope.interface import Interface, implements
00006 from zope.formlib import form
00007 from zope import schema
00008 
00009 from plone.app.contentrules.browser.formhelper import AddForm, EditForm 
00010 from plone.contentrules.rule.interfaces import IRuleElementData, IExecutable
00011 
00012 from Products.CMFCore.utils import getToolByName
00013 from Products.CMFPlone import PloneMessageFactory as _
00014 from Products.CMFPlone.utils import safe_unicode
00015 
00016 
00017 class IMailAction(Interface):
00018     """Definition of the configuration available for a mail action
00019     """
00020     subject = schema.TextLine(title=_(u"Subject"),
00021                               description=_(u"Subject of the message"),
00022                               required=True)
00023     source = schema.TextLine(title=_(u"Email source"),
00024                              description=_("The email address that sends the \
00025 email. If no email is provided here, it will use the portal from address."),
00026                              required=False)
00027     recipients = schema.TextLine(title=_(u"Email recipients"),
00028                                 description=_("The email where you want to \
00029 send this message. To send it to different email addresses, just separate them\
00030  with ,"),
00031                                 required=True)
00032     message = schema.Text(title=_(u"Message"),
00033                           description=_(u"Type in here the message that you \
00034 want to mail. Some defined content can be replaced: ${title} will be replaced \
00035 by the title of the item. ${url} will be replaced by the URL of the item."),
00036                           required=True)
00037 
00038 class MailAction(SimpleItem):
00039     """
00040     The implementation of the action defined before
00041     """
00042     implements(IMailAction, IRuleElementData)
00043 
00044     subject = u''
00045     source = u''
00046     recipients = u''
00047     message = u''
00048 
00049     element = 'plone.actions.Mail'
00050 
00051     @property
00052     def summary(self):
00053         return _(u"Email report to ${recipients}",
00054                  mapping=dict(recipients=self.recipients))
00055 
00056 
00057 class MailActionExecutor(object):
00058     """The executor for this action.
00059     """
00060     implements(IExecutable)
00061     adapts(Interface, IMailAction, Interface)
00062 
00063     def __init__(self, context, element, event):
00064         self.context = context
00065         self.element = element
00066         self.event = event
00067 
00068     def __call__(self):
00069         recipients = [str(mail.strip()) for mail in \
00070                       self.element.recipients.split(',')]
00071         mailhost = getToolByName(aq_inner(self.context), "MailHost")
00072         if not mailhost:
00073             raise ComponentLookupError, 'You must have a Mailhost utility to \
00074 execute this action'
00075 
00076         source = self.element.source
00077         urltool = getToolByName(aq_inner(self.context), "portal_url")
00078         portal = urltool.getPortalObject()
00079         email_charset = portal.getProperty('email_charset')
00080         if not source:
00081             # no source provided, looking for the site wide from email
00082             # address
00083             from_address = portal.getProperty('email_from_address')
00084             if not from_address:
00085                 raise ValueError, 'You must provide a source address for this \
00086 action or enter an email in the portal properties'
00087             from_name = portal.getProperty('email_from_name')
00088             source = "%s <%s>" % (from_name, from_address)
00089 
00090         obj = self.event.object
00091         event_title = safe_unicode(obj.Title())
00092         event_url = obj.absolute_url()
00093         message = self.element.message.replace("${url}", event_url)
00094         message = message.replace("${title}", event_title)
00095 
00096         subject = self.element.subject.replace("${url}", event_url)
00097         subject = subject.replace("${title}", event_title)
00098 
00099         for email_recipient in recipients:
00100             mailhost.secureSend(message, email_recipient, source,
00101                                 subject=subject, subtype='plain',
00102                                 charset=email_charset, debug=False,
00103                                 From=source)
00104         return True
00105 
00106 class MailAddForm(AddForm):
00107     """
00108     An add form for the mail action
00109     """
00110     form_fields = form.FormFields(IMailAction)
00111     label = _(u"Add Mail Action")
00112     description = _(u"A mail action can mail different recipient.")
00113     form_name = _(u"Configure element")
00114 
00115     def create(self, data):
00116         a = MailAction()
00117         form.applyChanges(a, self.form_fields, data)
00118         return a
00119 
00120 class MailEditForm(EditForm):
00121     """
00122     An edit form for the mail action
00123     """
00124     form_fields = form.FormFields(IMailAction)
00125     label = _(u"Edit Mail Action")
00126     description = _(u"A mail action can mail different recipient.")
00127     form_name = _(u"Configure element")