Example #1
0
    def generateEmail(self, email, recipient, force_no_attachments=False):
        """Generate the email for this recipient.

        :param email: Email address of the recipient to send to.
        :param recipient: The Person to send to.
        :return: (headers, subject, body) of the email.
        """
        from_address = self._getFromAddress(email, recipient)
        to_addresses = self._getToAddresses(email, recipient)
        headers = self._getHeaders(email, recipient)
        subject = self._getSubject(email, recipient)
        body = self._getBody(email, recipient)
        expanded_footer = self._getExpandedFooter(headers, recipient)
        if expanded_footer:
            body = append_footer(body, expanded_footer)
        ctrl = self._mail_controller_class(from_address,
                                           to_addresses,
                                           subject,
                                           body,
                                           headers,
                                           envelope_to=[email])
        if force_no_attachments:
            ctrl.addAttachment('Excessively large attachments removed.',
                               content_type='text/plain',
                               inline=True)
        else:
            self._addAttachments(ctrl, email)
        return ctrl
Example #2
0
 def _getBody(self, email, recipient):
     """Return the complete body to use for this email."""
     template = get_email_template(self._template_name, app=self.app)
     params = self._getTemplateParams(email, recipient)
     body = template % params
     footer = self._getFooter(params)
     if footer is not None:
         body = append_footer(body, footer)
     return body
Example #3
0
 def _getBody(self, email, recipient):
     """Return the complete body to use for this email."""
     template = get_email_template(self._getTemplateName(email, recipient),
                                   app=self.app)
     params = self._getTemplateParams(email, recipient)
     body = template % params
     if self._wrap:
         body = MailWrapper().format(body,
                                     force_wrap=self._force_wrap) + "\n"
     footer = self._getFooter(email, recipient, params)
     if footer is not None:
         body = append_footer(body, footer)
     return body
    def _getBody(self, email, recipient):
        """Return the complete body to use for this email.

        If there was a vote, we prefix "Review: " to the message.
        We always append information about why this message was sent.  If
        there is an existing footer, we append it to that.  Otherwise, we
        we insert a new footer.
        """
        # Include both the canonical_url for the proposal and the reason
        # in the footer to the email.
        reason, rationale = self._recipients.getReason(email)
        footer = "%(proposal_url)s\n%(reason)s" % {
            'proposal_url': self.proposal_url,
            'reason': reason.getReason()}
        return ''.join((
            self.body_prefix, append_footer(self.body_main, footer)))
Example #5
0
    def build(self,
              from_address,
              to_person,
              body,
              subject,
              email_date,
              rationale=None,
              references=None,
              message_id=None,
              filters=None):
        """Construct the notification.

        :param from_address: The From address of the notification.
        :param to_person: The `IPerson` to use as the To address for the
            notification.
        :param body: The body text of the notification.
        :type body: unicode
        :param subject: The Subject of the notification.
        :param email_date: The Date for the notification.
        :param rationale: The rationale for why the recipient is
            receiving this notification.
        :param references: A value for the References header.
        :param message_id: A value for the Message-ID header.

        :return: An `email.mime.text.MIMEText` object.
        """
        headers = [
            ('Date', format_rfc2822_date(email_date)),
            ('From', from_address),
            ('To', str(removeSecurityProxy(to_person).preferredemail.email)),
        ]

        # Add the common headers.
        headers.extend(self.common_headers)

        if references:
            headers.append(('References', ' '.join(references)))
        if message_id is not None:
            headers.append(('Message-Id', message_id))

        subject_prefix = "[Bug %d]" % self.bug.id
        if subject is None:
            headers.append(('Subject', subject_prefix))
        elif subject_prefix in subject:
            headers.append(('Subject', subject))
        else:
            headers.append(('Subject', "%s %s" % (subject_prefix, subject)))

        if rationale is not None:
            headers.append(('X-Launchpad-Message-Rationale', rationale))
            # XXX cjwatson 2015-09-11: The ridiculously complicated way that
            # bug notifications are built means that we no longer have
            # direct access to the subscriber name at this point.  As a
            # stopgap, parse it out of the rationale.
            match = re.search(r'@([^ ]*)', rationale)
            if match is not None:
                message_for = match.group(1)
            else:
                message_for = removeSecurityProxy(to_person).name
            headers.append(('X-Launchpad-Message-For', message_for))

        if filters is not None:
            for filter in filters:
                headers.append(('X-Launchpad-Subscription', filter))

        # XXX cjwatson 2015-07-31: This is cloned-and-hacked from
        # BaseMailer; it would ultimately be better to convert bug
        # notifications to that framework.
        if removeSecurityProxy(to_person).expanded_notification_footers:
            lines = []
            for key, value in headers:
                if key.startswith('X-Launchpad-'):
                    lines.append('%s: %s\n' % (key[2:], value))
            if lines:
                body = append_footer(body, ''.join(lines))

        message = MIMEText(body.encode('utf8'), 'plain', 'utf8')
        for header in headers:
            message.add_header(*header)
        return message