110. Network Programming -mail

Network Programming -mail

  • MUA: MailUserAgent mail user agent
  • MTA: MailTransferAgent Mail Transfer Agent
  • MDA: MailDeliveryAgent mail delivery agent
  • Process
    • QQ_MUA -> QQ_MTA -> SINA_MTA -> SINA_MDA -> SINA_MUA
  • Procedure flow chart
    • Send: MUA -> MTA with SMTP
    • 接收:MDA -> MUA with POP3 and IMAP

python for mail

  • SMTP protocol is responsible for sending mail

    • email module constructed Mail
      • Plain text messages
    • Send e-mail in HTML format
      • Ready html code content
      • The message subtpye set to html
    • Send e-mail with attachments
      • It can be seen as a text message and a mail attachments fit
      • A plurality of portions of a letter design, need MINEMultipart (format Construction)
      • Add a text MIMEText
      • Add a MINMEBase as an attachment or MINMEText
    • Add headers, cc and other information
      • Properties: mail [ "From"] represents the sender's information, including name and e-mail
      • Properties: mail [ "To"] represents the recipient information, including the name and mailing address
      • Properties: mail [ "Subject"] represents a summary of the information or topic
    • Supports html and text formats
      • If the other party supports html, on the other side with html, if it is supported by the other side to use text
      • E-mail format to build a MIMEMultipart
      • MIMEMultipart subtype attribute set to the alternative
      • Add html and text messages
    • smtplib module sends a message
  • write an email

#构建一个纯文本的邮件
import socket
import smtplib
from email.mime.text import MIMEText
from email.header import Header

#MIMEText写一封邮件的内容
#三个参数
#1.邮件内容
#2.MIME子类型,(plain 为text类型)(html 为html类型)
#3.编码类型
neirong = MIMEText("你好~~", "plain", "utf-8")
#添加发送者信息属性
neirong["From"] = Header("发送者信息", "utf-8")
#添加接收者信息属性
neirong["To"] = Header("接收者信息", "utf-8")
#添加摘要信息属性
neirong["Subject"] = Header("摘要或主题信息", "utf-8")

#准备参数
from_addr = "自己的邮箱"
from_pwd = "自己邮箱的授权码"
to_addr = "对方邮箱"
smtp_addr = "STMP服务器地址" #自己SMTP服务器一般是smtp.qq.com

try:
    #生成一个服务器实例
    #第一个参数 服务器地址要编码
    #第二个参数 服务器接收访问的端口 465是默认的ssl端口
    youjian = smtplib.SMTP_SSL(smtp_addr.encode(), 465)

    #登录自己的邮箱邮箱
    youjian.login(from_addr, from_pwd)

    #发送邮件
    #三个参数
    #1. 自己的地址
    #2. 接收的地址,必须是list类型,其中可填多个地址
    #3. 发送的内容,作为字符串发送
    youjian.sendmail(from_addr, [to_addr], neirong.as_string())
    
    #关闭通道
    youjian.quit()

except Exception as err:
    print(err)
#构建一个有附件的邮件

from email.mime.text import MIMEText
from email.mime.multipart import MIMEBase, MIMEMultipart

#构建一个邮件内容实例
#alternative意思是 若对方支持html,对方就用html,若不支持就用,对方就用text
youjian = MIMEMultipart("alternative")

#构建一个文本内容邮件实例
youjian_t = MIMEtext("你好~~", "plain", "utf-8")
#加入内容实例中
youjian.attach(youjian_t)

#构建一个邮件其他内容实例
with open("xx.html", "rb") as f:
    
    #构建一个html内容邮件实例
    youjian_f = MIMEText(f.read(), "base64", "utf-8")
    #设置属性
    youjian_f["Content-Type"] = "application/octet-stream"
    youjian_f["Content-Disposition"] = "attachment; filename='xx.html'"
    #加入内容实例中
    youjian.attach(youjian_f)

#其他和发文本邮件一样
  • POP3 protocol is responsible for incoming mail
    • MDA to give essentially a process of MUA
    • Download from MDA is down a full message structure, we need to resolve

    • Resolution process

      1. Poplib structure with original content downloading mail
        1. Prepare the appropriate content
        2. Authentication
        3. A complete list of messages in the mailbox
        4. The respective serial number, a message data stream to obtain the
        5. Using the analytic function corresponding to the parsed message structure
      2. Parse email messages with specific content
#负责从MDA到MUA下载
import poplib

#负责解析邮件结构
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr

#得到邮件
def getMsg():

    #建立ssl安全通道
    server = poplib.POP3_SSL("pop服务器地址") #一般是"pop.qq.com"
    server.user("对方邮件地址")
    server.pass_("授权码")

    #.stat()返回一个tupole,内容是 邮件数量 占用空间
    msgs, counts = server.stat()
    print("数量:{0}, 占用空间:{1}".format(msgs, counts))

    #.list()返回所有邮件编号列表
    #rsp 响应信息
    #mails 所有邮件编号
    #octets 返回的数据大小
    rsp, mails, octets = server.list()

    #获取最新邮件的编号,邮件索引号是从1开始,越新越大
    index = len(mails)
    
    #获取最新一封邮件
    #rsp 相应信息
    #retr 返回一个具体索引号的一封信的内容,此内容不具可读性
    #lines 存储邮件的最原始文本的每一行
    #octets 返回的数据大小
    rsp, lines, octets = server.retr(index)

    #加入回车等,并解码,形成整个邮件的原始文本,内容不可读
    msg_count = b"\r\n".join(lines)decode("utf-8")

    #解析出邮件整个结构体
    neirong = Patser().parsestr(msg_count)

    #关闭连接
    server.quit()

    return neirong

#笔记未完待续

Guess you like

Origin www.cnblogs.com/TK-tank/p/12332253.html