def send_mail(self, data):
        portal = api.portal.get()
        storage = IDownloadTokenStorage(portal)
        mail_template = self.context.restrictedTraverse('@@mail_downloadtoken')

        comment = data['comment']
        subject = translate(_(u'mail_subject',
                              default=u'[${title}] Download link',
                              mapping={'title': self.context.Title().decode(
                                  'utf-8')}),
                            context=self.request)

        for email in data['recipients']:
            downloadtoken = storage.add(self.context, email)

            options = {'user': api.user.get_current(),
                       'date': DateTime(),
                       'link': storage.url(downloadtoken),
                       'comment': comment}
            mail = mail_template(**options)

            composer = HTMLComposer(
                message=mail,
                subject=subject,
                to_addresses=[(email, email)])
            composed = composer.render()

            mh = api.portal.get_tool(name='MailHost')
            mh.send(composed.as_string(),
                    mto=composed['To'],
                    mfrom=composed['From'])
        notify(DownloadlinkSent(self.context, data['recipients'], comment))
def downloadlink_sent(event):
    emails = event.emails
    obj = event.obj
    action = _(
        u"label_send_dllink", default=u"Downloadlink sent",
        mapping=dict(
            mail_list=len(emails) > 0 and ', '.join(emails) + '\n' or '-'))
    time = DateTime()
    notify(JournalEntryEvent(obj, event.comment, action, time=time))
    def valid_email_addresses(data):
        expr = re.compile(r"^(\w&.%#$&'\*+-/=?^_`{}|~]+!)*[\w&.%#$&'\*+-/=" +
                          r"?^_`{}|~]+@(([0-9a-z]([0-9a-z-]*[0-9a-z])?" +
                          r"\.)+[a-z]{2,6}|([0-9]{1,3}\.){3}[0-9]{1,3})$",
                          re.IGNORECASE)

        for mail in data.recipients:
            if not expr.match(mail):
                raise Invalid(_(u'text_error_invalid_email',
                                default=u'You entered one or more invalid '
                                'email addresses.'))
    def send(self, action):
        data, errors = self.extractData()
        if errors:
            return
        self.send_mail(data)

        api.portal.show_message(
            message=_(u'text_send_mail',
                      default='The given recipients has been notified.'),
            request=self.request,
            type='info')

        self.redirect()
def downloadlink_opened(event):
    email = event.email
    obj = event.obj
    action = _(u"label_open_dllink", default=u"Downloadlink opened")
    time = DateTime()
    notify(JournalEntryEvent(obj, '-', action, actor=email, time=time))