Exemple #1
0
    def translate_message(self, msg):
        sender = msg.get_unixfrom() or msg['From']
        if not sender:
            if self.fix_sender:
                sender = self.default_sender
            else:
                raise BadMessageError("No sender specified")
        to = msg['To']
        if not to:
            raise BadMessageError("No destination addresses specified")
        message = EmailMessage(sender=sender or msg['From'], to=to)
        # Go through all the headers which Google will let us use
        cc = msg['Cc']
        if cc:
            message.cc = cc
        bcc = msg['Bcc']
        if bcc:
            message.bcc = cc
        reply_to = msg['Reply-To']
        if reply_to:
            message.reply_to = reply_to
        subject = msg['Subject']
        if subject:
            message.subject = subject

        # If there's just a plain text body, use that, otherwise
        # iterate over all the attachments
        payload = msg.get_payload(decode=True)
        if isinstance(payload, basestring):
            message.body = payload
        else:
            body = ''
            html = ''
            attachments = []
            # GAE demands we specify the body explicitly - we use the first text/plain attachment we find.
            # Similarly, we pull in the first html we find and use that for message.html
            # We pull in any other attachments we find; but we ignore the multipart structure,
            # because GAE doesn't give us enough control there.
            for part in msg.walk():
                if part.get_content_type() == 'text/plain' and not body:
                    body = part.get_payload(decode=True)
                elif part.get_content_type() == 'text/html' and not html:
                    html = part.get_payload(decode=True)
                elif not part.get_content_type().startswith('multipart'):
                    attachments.append(
                        (get_filename(part), part.get_payload(decode=True)))
            if not body:
                raise BadMessageError("No message body specified")
            message.body = body
            if html:
                message.html = html
            if attachments:
                message.attachments = attachments
        return message
Exemple #2
0
    def translate_message(self, msg):
        sender = msg.get_unixfrom() or msg["From"]
        if not sender:
            if self.fix_sender:
                sender = self.default_sender
            else:
                raise BadMessageError("No sender specified")
        to = msg["To"]
        if not to:
            raise BadMessageError("No destination addresses specified")
        message = EmailMessage(sender=sender or msg["From"], to=to)
        # Go through all the headers which Google will let us use
        cc = msg["Cc"]
        if cc:
            message.cc = cc
        bcc = msg["Bcc"]
        if bcc:
            message.bcc = cc
        reply_to = msg["Reply-To"]
        if reply_to:
            message.reply_to = reply_to
        subject = msg["Subject"]
        if subject:
            message.subject = subject

        # If there's just a plain text body, use that, otherwise
        # iterate over all the attachments
        payload = msg.get_payload(decode=True)
        if isinstance(payload, basestring):
            message.body = payload
        else:
            body = ""
            html = ""
            attachments = []
            # GAE demands we specify the body explicitly - we use the first text/plain attachment we find.
            # Similarly, we pull in the first html we find and use that for message.html
            # We pull in any other attachments we find; but we ignore the multipart structure,
            # because GAE doesn't give us enough control there.
            for part in msg.walk():
                if part.get_content_type() == "text/plain" and not body:
                    body = part.get_payload(decode=True)
                elif part.get_content_type() == "text/html" and not html:
                    html = part.get_payload(decode=True)
                elif not part.get_content_type().startswith("multipart"):
                    attachments.append((get_filename(part), part.get_payload(decode=True)))
            if not body:
                raise BadMessageError("No message body specified")
            message.body = body
            if html:
                message.html = html
            if attachments:
                message.attachments = attachments
        return message
Exemple #3
0
def _gae_send_mail(sender, to, cc=None, bcc=None, reply_to=None, subject='', body='', html='', attachments=[], headers={}):
    email = EmailMessage()
    email.sender = sender
    email.to = to
    email.subject = subject
    email.body = body
    if cc: email.cc = cc
    if bcc: email.bcc = bcc
    if reply_to: email.reply_to = reply_to
    if html: email.html = html
    if attachments: email.attachments = attachments
    if headers: email.headers = headers
    
    return email.send()
Exemple #4
0
def _gae_send_mail(sender, to, cc=None, bcc=None, reply_to=None, subject='', body='', html='', attachments=[], headers={}):
    email = EmailMessage()
    email.sender = sender
    email.to = to
    email.subject = subject
    email.body = body
    if cc: email.cc = cc
    if bcc: email.bcc = bcc
    if reply_to: email.reply_to = reply_to
    if html: email.html = html
    if attachments: email.attachments = attachments
    if headers: email.headers = headers
    
    return email.send()
Exemple #5
0
    def send(self):

        """
        Build and send an email message from this model instance
        """

        # create new email message
        new_email = EmailMessage()

        # set sender address
        new_email.sender = self.get_sender()

        # set to addresses
        new_email.to = self.to

        # set cc addresses
        if self.cc:
            new_email.cc = self.cc

        # set bcc addresses
        if self.bcc:
            new_email.bcc = self.bcc

        # set reply to address
        if self.reply_to:
            new_email.reply_to = self.reply_to
        else:
            new_email.reply_to = self.get_sender()

        # set email subject
        new_email.subject = self.subject

        # set plaintext body
        if self.body_plain:
            new_email.body = self.body_plain

        # set html body
        if self.body_html:
            new_email.html = self.body_html

        # check that the email has been properly initialized and send
        new_email.check_initialized()
        new_email.send()