python库smtplib和email可用于发送邮件。

smtplib:主要用于发送邮件,包括连接邮件服务器、登陆第三方邮箱、发送邮件、关闭会话等。

email:主要用于构造邮件,包括发送人、接收人、主题、内容、附件等。

1、常用方法

Python的smtplib库 python email库_发送邮件

 

其中各模块或类之间的继承关系如下:

MIMEBase

    -- MIMENonMultipart

            --MIMEText

            --MIMEImage

    --MIMEMultipart

2、发送给第3方邮件服务商,包括163邮箱、126邮箱、QQ邮箱等。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.header import Header


def send_mail():
    server = "smtp.163.com"
    port = 25
    sender = "whdlut66@163.com"
    license = "IHPHAYVHYJIHLWAL"    # 注意:是授权码,而不是登陆密码
    receivers = ["whdlut66@163.com"]   # 注意:接收者是列表list,而不是一个字符串
    msg = "The content."

    try:
        smtp = smtplib.SMTP(host=server, port=port)   # 连接邮件服务器
        smtp.login(sender, license)   # 登陆
        smtp.sendmail(sender, receivers, msg)  # 发送邮件
        smtp.quit()   # 断开会话
    except smtplib.SMTPException:
        print("Check the smtp method.")


send_mail()

3、邮件内容为文本、图片

3.1、邮件内容为纯文本plain

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.header import Header


def send_mail():
    server = "smtp.163.com"
    port = 25
    sender = "whdlut66@163.com"
    license = "IHPHAYVHYJIHLWAL"
    receivers = ["whdlut66@163.com", "testtttt@126.com"]
    msg = "The content"
    msg = MIMEText(msg, "plain", "utf-8")
    msg["From"] = sender
    msg["To"] = ",".join(receivers)  # 注意,这里不是直接将列表给msg
    msg["Subject"] = "发送主题"

    try:
        smtp = smtplib.SMTP(host=server, port=port)
        smtp.login(sender, license)
        smtp.sendmail(sender, receivers, msg.as_string())
        smtp.quit()
    except smtplib.SMTPException:
        print("Check the smtp method.")


send_mail()

3.2、邮件内容为网页html

网页html和纯文本plain的区别,就是MIMEText(msg, "plain", "utf-8")中的编码类型plain修改为“html”。内容即支持一个网页html的展示。

3.3、邮件内容为图片,图片直接嵌入在邮件内容中

发送图片,一般用MIMEImage这个类,来构造图片的头、内容等信息,但是会发现图片会默认以附件的形式发送。

如果图片需嵌入到邮件内容中,需要用到MIMEMultipart,MIMEText类。问题:图片无法展示?

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.header import Header


def send_mail():
    server = "smtp.163.com"
    port = 25
    user = "whdlut66@163.com"
    password = "IHPHAYVHYJIHLWAL"
    receivers = ["whdlut66@163.com", "testttt@126.com"]

    msg = MIMEMultipart()
    msg["From"] = user
    msg["To"] = ",".join(receivers)
    msg["Subject"] = "主题-测试报告"

    content = "<h>Hello</p><p><img src='cid: test_value'></p>"
    msg_text = MIMEText(content, "html", "utf-8")
    msg.attach(msg_text)

    img_name = "smtplib和email.jpg"
    f = open(img_name, "rb")
    msg_img = MIMEImage(f.read())
    f.close()
    msg.add_header("Content-ID", "<test_value>")
    msg.attach(msg_img)

    try:
        smtp = smtplib.SMTP(host=server, port=port)
        smtp.login(user, password)
        smtp.sendmail(user, receivers, msg.as_string())
        smtp.quit()
    except smtplib.SMTPException:
        print("Check the smtp method.")


send_mail()

 

4、带附件

4.1、附件中为纯文本plain

4.2、附件中为网页html

4.3、附件中为图片

4.3、附件中为压缩包

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.header import Header


def send_mail():
    server = "smtp.163.com"
    port = 25
    user = "whdlut66@163.com"
    password = "IHPHAYVHYJIHLWAL"
    receivers = ["whdlut66@163.com", "testtttt@126.com"]

    msg = MIMEMultipart()
    msg["From"] = user
    msg["To"] = ",".join(receivers)
    msg["Subject"] = "主题-测试报告"

    # 邮件正文
    content = MIMEText("邮件正文test", "plain", "utf-8")
    msg.attach(content)

    # 带附件1,html格式
    file1 = "result.html"
    f = open(file1, "rb").read()
    att1 = MIMEText(f, "html", "utf-8")
    att1["Content-Disposition"] = "attachment; filename=result.html"
    msg.attach(att1)

    # 带附件2,text格式
    file2 = "test_aa.log"
    f = open(file2, 'r').read()
    att2 = MIMEText(f, "plain", "utf-8")
    att2["Content-Disposition"] = "attachment; filename=test_aa.log"
    msg.attach(att2)

    # 带附件3,zip压缩包
    file_zip = "/Users/test/OneDrive/zentaoclient.mac64.zip"
    att3 = MIMEText(file_zip, "plain", "utf-8")
    att3["Content-Disposition"] = "attachment; filename=zentaoclient.mac64.zip"
    msg.attach(att3)

    try:
        smtp = smtplib.SMTP(host=server, port=port)
        smtp.login(user, password)
        smtp.sendmail(user, receivers, msg.as_string())
        smtp.quit()
    except smtplib.SMTPException:
        print("Check the smtp method.")


send_mail()

 

5、遇到的问题

5.1、登陆中的password不是登陆密码,是授权码,怎样获取授权码呢?以163有限为例。 

5.2、outlook怎样登陆呢?如果不能开启POP3或SMTP协议,可以用此类方法来发送吗? 

5.3、如果邮件内容没有用MIMEText来构造,msg直接为字符串,为什么邮件内容没有呢?

5.4、图片直接嵌入到邮件内容中,为什么无法展示呢?

5.5、smtplib.sendmail(from, to , msg), from和to分别表示发送者和接收者,但是msg中仍可以设置from和to,或有冲突吗?