Пример #1
0
                        h_value = mail.get_all(h)
                        print repr(h)
                        print repr(h_value)
                        if type(h_value) == type([]):
                            for v in h_value:
                                new_mail.add_header(h, v)
                        else:
                            new_mail.add_header(h, h_value)
                    mail = new_mail

                for f in non_mail_files:
                    part = MIMEBase('application', "octet-stream")
                    part.set_payload(open(f, "rb").read())
                    Encoders.encode_base64(part)
                    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
                    mail.attach(part)
                    os.remove(f)

                mail_files[0] = (mail_files[0][0], mail)
            else:
                print "Can't decide how to bundle the %s non-mail files among the %s mails. Skip the folder..." % (len(non_mail_files), len(mail_files))
                continue

        parent_folder_name = os.path.split(path)[-1]
        parent_folders = path.split(os.sep)[::-1]

        for (filepath, mail) in mail_files:

            # The mail was saved from an IMP account
            if mail.get('X-Mailer-Version', None) == 'v3.57 (r)' and not mail.get('From', None) and not mail.get('Date', None):
                mail.add_header('From', '*****@*****.**')
Пример #2
0
                        print repr(h_value)
                        if type(h_value) == type([]):
                            for v in h_value:
                                new_mail.add_header(h, v)
                        else:
                            new_mail.add_header(h, h_value)
                    mail = new_mail

                for f in non_mail_files:
                    part = MIMEBase('application', "octet-stream")
                    part.set_payload(open(f, "rb").read())
                    Encoders.encode_base64(part)
                    part.add_header(
                        'Content-Disposition',
                        'attachment; filename="%s"' % os.path.basename(f))
                    mail.attach(part)
                    os.remove(f)

                mail_files[0] = (mail_files[0][0], mail)
            else:
                print "Can't decide how to bundle the %s non-mail files among the %s mails. Skip the folder..." % (
                    len(non_mail_files), len(mail_files))
                continue

        parent_folder_name = os.path.split(path)[-1]
        parent_folders = path.split(os.sep)[::-1]

        for (filepath, mail) in mail_files:

            # The mail was saved from an IMP account
            if mail.get(
Пример #3
0
    def construct(cls, raw_headers=None, from_email=None, to=None, cc=None, subject=None, headers=None,
                  text=None, text_charset='utf-8', html=None, html_charset='utf-8',
                  attachments=None):
        """
        Returns a new AnymailInboundMessage constructed from params.

        This is designed to handle the sorts of email fields typically present
        in ESP parsed inbound messages. (It's not a generalized MIME message constructor.)

        :param raw_headers: {str|None} base (or complete) message headers as a single string
        :param from_email: {str|None} value for From header
        :param to: {str|None} value for To header
        :param cc: {str|None} value for Cc header
        :param subject: {str|None} value for Subject header
        :param headers: {sequence[(str, str)]|mapping|None} additional headers
        :param text: {str|None} plaintext body
        :param text_charset: {str} charset of plaintext body; default utf-8
        :param html: {str|None} html body
        :param html_charset: {str} charset of html body; default utf-8
        :param attachments: {list[MIMEBase]|None} as returned by construct_attachment
        :return: {AnymailInboundMessage}
        """
        if raw_headers is not None:
            msg = Parser(cls, policy=default_policy).parsestr(raw_headers, headersonly=True)
            msg.set_payload(None)  # headersonly forces an empty string payload, which breaks things later
        else:
            msg = cls()

        if from_email is not None:
            del msg['From']  # override raw_headers value, if any
            msg['From'] = from_email
        if to is not None:
            del msg['To']
            msg['To'] = to
        if cc is not None:
            del msg['Cc']
            msg['Cc'] = cc
        if subject is not None:
            del msg['Subject']
            msg['Subject'] = subject
        if headers is not None:
            try:
                header_items = headers.items()  # mapping
            except AttributeError:
                header_items = headers  # sequence of (key, value)
            for name, value in header_items:
                msg.add_header(name, value)

        # For simplicity, we always build a MIME structure that could support plaintext/html
        # alternative bodies, inline attachments for the body(ies), and message attachments.
        # This may be overkill for simpler messages, but the structure is never incorrect.
        del msg['MIME-Version']  # override raw_headers values, if any
        del msg['Content-Type']
        msg['MIME-Version'] = '1.0'
        msg['Content-Type'] = 'multipart/mixed'

        related = cls()  # container for alternative bodies and inline attachments
        related['Content-Type'] = 'multipart/related'
        msg.attach(related)

        alternatives = cls()  # container for text and html bodies
        alternatives['Content-Type'] = 'multipart/alternative'
        related.attach(alternatives)

        if text is not None:
            part = cls()
            part['Content-Type'] = 'text/plain'
            part.set_payload(text, charset=text_charset)
            alternatives.attach(part)
        if html is not None:
            part = cls()
            part['Content-Type'] = 'text/html'
            part.set_payload(html, charset=html_charset)
            alternatives.attach(part)

        if attachments is not None:
            for attachment in attachments:
                if attachment.is_inline_attachment():
                    related.attach(attachment)
                else:
                    msg.attach(attachment)

        return msg
Пример #4
0
    msg = Parser().parsestr(msg_content)

    print_info(msg)

file.close()

server.close()

from_addr = email
# to_addr = '*****@*****.**'
to_addr = input('To Email address: ')
smtp_server = 'smtp.163.com'

text = 'Dear' + '\n\n' + u'交大邮件通知,请查收附件。' + '\n\n' + 'best regards' + '\n' + 'Andy Zhang'

msg = MIMEMultipart()
msg['From'] = _format_addr('Andy Zhang <%s>' % from_addr)
msg['To'] = _format_addr('Gmail <%s>' % to_addr)
msg['Subject'] = Header(u'交大邮件通知', 'utf-8')

msg.attach(MIMEText(text, 'plain', 'utf-8'))

with codecs.open('/Users/zhangzhifan/Desktop/mail_log.txt', 'rb', encoding='utf-8') as f:
    mime = MIMEText(f.read())
    mime.add_header('Content-Disposition', 'attachment', filename='mail_log.txt')
    msg.attach(mime)

server = smtplib.SMTP(smtp_server, 25)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()