Пример #1
0
def send_mail_as_noreply(address,
                         subject,
                         msg=None,
                         html=None,
                         attachments=None):
    logging.info(u"Sending email to %s: [%s] %s" % (address, subject, msg))
    try:
        mail = EmailMessage()
        mail.sender = NO_REPLY_SENDER
        mail.to = address
        mail.subject = subject

        if attachments:
            mail.attachments = attachments

        if html:
            mail.html = html
        elif msg:
            mail.body = msg
        else:
            mail.body = "---"  # mail.body cannot be empty string, raises exception

        mail.check_initialized()
        mail.send()
    except Exception, e:
        logging.error("Email sending failed: %s" % e.message)
Пример #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
Пример #3
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
Пример #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()
Пример #5
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()
Пример #6
0
def send_mail_as_noreply(address, subject, msg=None, html=None, attachments=None):
    logging.info(u"Sending email to %s: [%s] %s" % (address, subject, msg))
    try:
        mail = EmailMessage()
        mail.sender = NO_REPLY_SENDER
        mail.to = address
        mail.subject = subject

        if attachments:
            mail.attachments = attachments

        if html:
            mail.html = html
        elif msg:
            mail.body = msg
        else:
            mail.body = "---" # mail.body cannot be empty string, raises exception

        mail.check_initialized()
        mail.send()
    except Exception, e:
        logging.error("Email sending failed: %s" % e.message)