python中的SMTP发送邮件

一. 介绍

python3中自带了smtplib模块和email模块

smtplib模块:负责与邮件服务器的交互

email模块:负责组织邮件内容

二. smtplib模块

smtplib模块:主要是通过SMTP类来与邮件系统进行交互

1. 实例化一个SMTP对象

s = smtplib.SMTP(邮件服务器地址, 端口号)

s = smtplib.SMTP_SSL(邮件服务器地址,端口号)

2. 登录邮箱--权限验证

s.login(用户名,授权码)

3. 发送邮件

s.sendmail(发件人邮箱,收件人邮箱,发送内容)

4. 断开连接

s.close()

5. 实例:我们来看一个小练习,即发送邮件程序 v1.0

import smtplib

#实例化一个SMTP对象
s = smtolib.SMTP_SSL("smtp.qq.com", 465)
#输出与SMTP服务器交互的详细信息
s.set_debuglevel(1)
#登录邮箱
s.login("1069966476", "xxxxxx")
#发送邮件
s.sendmail("[email protected]", "[email protected]", "hello, test!")
#断开连接
s.close()

这就是一个很简单的邮件发送程序,我们只利用了smtplib模块,可以看看效果

打开邮件之后,发现发件人也没有,收件人也没有,主题也没有,内容也没有。至于为什么内容也没有,我猜和没有使用email模块相关,因为这个模块就是负责邮件内容的。

三. email模块

email模块:支持发送的邮件内容为纯文本、html内容、图片、添加附件

email模块有几大类用来针对不同的邮件内容形式,常用如下:

MIMEText:内容形式为纯文本,或者html页面

MIMEImage:内容形式为图片

MIMEMultipart:多形式组合。即内容包含文本和附件

每一类对应的导入方式:

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.image import MIMEImage

1. MIMEText

msg:文本内容

type:默认为plain,即纯文本,发送html格式的时候,修改为html,但同时要求msg的内容也是html格式

charset:文本编码,有中文时选择utf-8

#构造TEXT格式的消息

msg = MIMEText("hello, test!", charset = "utf-8")

msg["From"] = "XXXX"

msg["To"] = "XXXX"

msg["CC"] = "XXXX"

msg["Subject"] = "python发邮件测试"

#发送以上构造的邮件内容,要使用as_string来将构造的邮件内容转换成string形式

s.sendmail("XXXX", "XXXX" , msg.as_string())

2. 实例:发送邮件程序v2.0,添加主题、发件人、收件人

import smtplib
from email.mime.text import MIMEText

#实例化一个MIMEText对象
msg = MIMEText("hello, test!", _charset="utf-8")
#发件人信息
msg["From"] = "[email protected]"
#收件人信息
msg["To"] = "[email protected]"
#邮件主题
msg["Subject"] = "python发邮件测试"

#利用smrplib模块发送邮件
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.set_debuglevel(1)
s.login("1069966476", "xxxxxx")
s.sendmail("[email protected]", "[email protected]", msg.as_string())
s.close()

我们看看效果如何

实际上,比刚才的效果要好很多。因为我们明确的看到了有主题、发件人、收件人以及邮件内容等相关信息

3. 实例:发送邮件程序v3.0,发送html文件

import smtplib, os
from email.mime.text import MIMEText

#在当前目录下存在一个test.html文件,我们需要先把html文件内容读取出来,作为内容传入到MIMEText中
html_content = open(os.getcwd() + "/test.html", encoding="utf-8").read()
msg = MIMEText(html_content, "html", "utf-8")
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
msg["Subject"] = "python发送--html邮件测试"

s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.set_debuglevel(1)
s.login("1069966476", "xxxxxx")
s.sendmail("[email protected]", "[email protected]", msg.as_string())
s.close()

此时,我们就可以把html文件的内容作为邮件内容发送出去,效果如下:

 而html文件在浏览器中打开的效果是这样的

可以明显的感受到,邮件的内容就是html文件在浏览器中展示的内容

4. 实例:发送邮件程序v4.0,我们还可以传一个写一个html语言的文本发送过去

import smtplib
from email.mime.text import MIMEText

content = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
"""
msg = MIMEText(content, "html", "utf-8")
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
msg["Subject"] = "python发送--html邮件测试2"

s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.set_debuglevel(1)
s.login("1069966476", "xxxxxx")
s.sendmail("[email protected]", "[email protected]", msg.as_string())
s.close()

效果如下:

5. MIMEImage、MIMEMultipart

msg = MIMEMultipart()

msg_sub = MIMEText("hello.test!", _charset = "utf-8")

msg.attach(msg_sub)         #将text消息添加到MIMEMultipart中,作为邮件正文

#图片作为附件

import os

img_data = open(os.getcwd() + "/reports/python33安装-添加path.png", "rb").read()

msg_img = MIMEImage(img_data)

msg_img.add_header("Content-Disposition", "attachment", filename = "python34.png")

msg_img.add_header("Content-ID", "<0>")

msg.attach(msg_img)         #将图片消息添加到MIMEMultipart中,作为附件发送

6. 实例:发送邮件程序v5.0,将图片和html文件作为附件发送

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

#带附件的邮件可以看做包含若干部分的邮件:文本和各个附件本身
#可以构造一个MIMEMultipart对象代表邮件本身
#然后往里面加上一个MIMEText作为邮件正文,再继续往里面加上表示附件的MIMEBase对象即可

msg = MIMEMultipart()
msg["From"] = "[email protected]"
#一定要注意多人发送是字符串形式,中间用,号分隔
msg["To"] = "[email protected], [email protected]"
#抄送
msg["CC"] = "[email protected]"
msg["Subject"] = "python--发送附件测试"

#文本内容
msg_sub = MIMEText("hello.test!", _charset="utf-8")
#将文本内容添加到msg当中
msg.attach(msg_sub)

#图片作为附件
import os
#读取图片内容,要选择二进制的方式
img_data = open(os.getcwd() + "/Image 17.png", "rb").read()
msg_img = MIMEImage(img_data)
msg_img.add_header("Content-Disposition", "attachment", filename = "Image 17.png")
msg_img.add_header("Content-ID", "<0>")
msg.attach(msg_img)

#html作为附件
html_content = open(os.getcwd() + "/test.html", encoding="utf-8").read()
msg_html = MIMEText(html_content, "html", "utf-8")
msg_html.add_header("Content-Disposition", "attachment", filename = "test.html")
msg.attach(msg_html)

#邮件发送
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.set_debuglevel(1)
s.login("1069966476", "xxxxxx")
#收件人如果是多人,应该用列表的形式表示
s.sendmail("[email protected]", ["[email protected]", "[email protected]"], msg.as_string())
s.close()

这种效果就是我们最终想要的形式。

猜你喜欢

转载自www.cnblogs.com/cnhkzyy/p/9070165.html