从Python email模块理解邮件生命周期及MIME

  • Chapter One : email 模块

    1. Python Documents
    2. Documents Examples
    3. 19.1 email-An email and MIME handling package

    The email package is a library for managing email messages.

    The overall structure of the email package can be divided into three major componets, plus a fourth component that controls the behavior of the other components.

    1. message.py核心模块,提供EmailMessage类
    2. parser.py解析bytes转成EmailMessage
    3. generator.py将EmailMessage转成bytes stream
    4. policy.py控制模块

    The central component of the package is an “object model” that represents email messages.即lib/email/message.py模块中定义的用于构建、解析邮件内容的EmailMessage()类。

  • Chapter Two : 常用概念辨析

    MUA(Mail User Agent): 邮件用户代理,Foxmail等邮件客户端。

    MTA(Mail Transfer Agent): 邮件传输代理,网易新浪等Email服务提供商。

    MDA(Mail Delivery Agent): 邮件投递代理,邮件最终目的地。

    因此,程序实现的功能是:

    • 作为MUA向MTA发邮件;使用SMTP协议
    • 作为MUA从MDA拿邮件;使用POP3/IMAP4

    Python对SMTP的支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。

    邮件主题、如何显示发件人、收件人等信息并不是通过SMTP协议发送给MTA的,而是包含在发给MTA的文本中的。因此可以像key:value一样将From To Subject 添加到MIMEText中。

    • 对于纯文本,MIMEText(‘文本内容’, ‘plain’, ‘utf-8’) 返回‘text/plain’格式;

    • 对于HTML邮件,MIMEText(‘HTML字符串’, ‘html’, ‘utf-8’) 返回‘text/html’格式;

      扫描二维码关注公众号,回复: 11272888 查看本文章
    • 对于附件,msg=MIMEMultipart() msg.attach(MIMEText) msg.attach(MIMEBase)

    带附件的邮件可以看作包含若干部分的邮件:文本(MIMEText)和各附件(MIMEBase)本身。

    SMTP通过构造一个MIME对象实现发送邮件,POP3则可以收取邮件,POP3收取的不是一个已经可以阅读的邮件本身,而是有邮件的原始文件,就是SMTP发送的经过编码之后的那些东西,需要email模块解析原始文件,变成可阅读的邮件对象。

  • Chapter Three : EmailMessage

    经上述生命周期详解可以看到,时间过程中最核心的就是***EmailMessage***类。

    An email message consists of headers and a payload(which is also referred to as the content), payload might be a list of sub-EmailMessage objects.

    headers 就像是信封。

    The EmailMessage dictionary-like interface is indexed by the header names.

    *email.message_from_string()*返回的是Message对象,此Message是MIMEPart的父类,而MIMEPart是这里提到的EmailMessage对象的父类。

    Message object structures can be created in one of two ways:

    • can be created from whole cloth by creating an EmailMessage object, adding headers using the dictionary interface, and adding payload(s) using set _content() and related methods.
    • can be created by parsing a serialized representation of the email message

    EmailMessage是email的中军,parser.py模块就是机动部队。email的parser模块可以解析bytes、string、file,但是对内容进行解析的第一个分类判断就是:non-MIME和MIME message。这就要求先搞懂MIME。

  • MIME message

    For MIME messages, the root object will return True from its is_multipart() method, and the subparts can be accessed via the payload manipulation methods, such as get_body(), oter_parts(), walk().

  • Chapter Four : Parser and FeedParser from parser.py

    email包里有parser.py和feedparser.py两个模块分别提供了两个类Parser和FeedParser。

    如果内存中有完整邮件的数据,采用Parser;如果需要从文件系统中分批的读取部分邮件内容,则FeedParser更适合便解析便消费数据。

    普通需求,Parser应该就够了。

  • References

  1. python高阶教程-使用imap接收邮箱的附件(中文字符编码与MIME)
  2. 廖雪峰的官方网站:电子邮件

猜你喜欢

转载自blog.csdn.net/The_Time_Runner/article/details/106310970