Python通过smtplib发送邮件
1.邮件发送的步骤:
- 创建一个smtp对象
- 连接smtp服务器
- 登陆smtp服务器
- 发送邮件
- 退出登录
import smtplib
# 1.创建一个smtp对象
smtp = smtplib.SMTP()
# 2.连接smtp服务器
# mainhost: 设置的目标服务器 一般就是域名 比如smtp.qq.com /smtp.163.com
# mainport: 端口号 25 465 22等
smtp.connect(mainhost, mainport)
# 3.登陆smtp服务器
# user: 用户名
# password: 授权码
smtp.login(user, password)
# 4.发送邮件
# msg: 就是使用email模块包装出来的对象 然后将这个对象转换为字符串
# from_addr: 邮件发送者地址
# to_addrs: 字符串列表,邮件发送地址
# msg: 发送消息
smtp.sendmail(from_addr, to_addrs, msg.as_string())
# 5.退出登陆
smtp.quit()
2.邮箱设置
以qq为例,我们要先进行设置,找到服务器、端口号、用户名、授权码等信息,其他邮箱也可以自己去尝试:
查看服务器和端口号:
获取授权码:
3.发送一封qq邮件
代码如下:
from email.mime.text import MIMEText # 导包
import smtplib
from email.header import Header
# 设置第三方smtp相关服务信息
main_host = "smtp.qq.com" # 设置服务器 如果是163 写smtp.163.com即可
main_user = "xxxxxxxxxx@qq.com" # 用户名 或者是xxxxxxx@163.com
main_pass = "填写自己获取的授权码" # 授权码
# 设置接收与发送邮件者
sender = "xxxxxxxxxx@qq.com"
receiver = "xxxxxxxxxx@qq.com" # 第一次可以先测试自己发送给自己
# TODO 1.将发送的内容包装成为邮件需要的格式
content = "Hello World python email" # 发送内容
msg = MIMEText(content, 'plain', 'utf-8')
# 这里可以将xxxxxxxxxx@qq.com指定为中文
msg["from"] = Header("xxxxxxxxxx@qq.com", "utf-8")
msg["To"] = Header("xxxxxxxxxx@qq.com", "utf-8")
subject = "Python SMTP qq邮件测试尝试"
msg["subject"] = Header(subject, "utf-8")
# TODO 2.使用smtplib模块发送邮件
try:
smtp = smtplib.SMTP() # 1.创建一个smtp对象
smtp.connect(main_host, 25) # 2.连接smtp服务器 # 这里的端口号写587也是可以的
smtp.login(main_user, main_pass) # 3.登陆smtp服务器
smtp.sendmail(sender, receiver, msg.as_string()) # 4.发送邮件
smtp.quit() # 5.退出登陆
except smtplib.SMTPException:
print("无法发送邮件")
4.发送HTML格式的邮件
Python发送HTML格式的邮件与发送纯文本消息的邮件不同之处就是将MIMEText中_subtype设置为html。具体代码如下:
from email.mime.text import MIMEText # 导包
from email.header import Header
import smtplib
# TODO 2.配置发送邮件需要的基本信息
# 设置第三方smtp相关服务信息
main_host = "smtp.qq.com" # 设置服务器 如果是163 写smtp.163.com即可
main_user = "xxxxxxxxxx@qq.com" # 用户名 或者是xxxxxxx@163.com
main_pass = "填写自己获取的授权码" # 授权码
# 设置接收与发送邮件者
sender = "xxxxxxxxxx@qq.com"
receiver = "xxxxxxxxxx@qq.com" # 第一次可以先测试自己发送给自己
# TODO 1.将发送的内容包装成为邮件需要的格式
content = """
<p>Python 邮件发送测试...</p>
<p><a href="https://blog.csdn.net/guiyin1150?spm=1010.2135.3001.5113">点击进入guiyin1150的博客</a></p>
""" # 发送内容
msg = MIMEText(content, 'html', 'utf-8')
# 这里可以将xxxxxxxxxx@qq.com指定为中文
msg["from"] = Header("xxxxxxxxxx@qq.com", "utf-8")
msg["To"] = Header("xxxxxxxxxx@qq.com", "utf-8")
subject = "Python SMTP 邮件测试"
msg["subject"] = Header(subject, "utf-8")
# TODO 3.使用smtplib模块发送邮件
try:
smtp = smtplib.SMTP() # 1.创建一个smtp对象
smtp.connect(main_host, 25) # 2.连接smtp服务器 # 这里的端口号写587也是可以的
smtp.login(main_user, main_pass) # 3.登陆smtp服务器
smtp.sendmail(sender, receiver, msg.as_string()) # 4.发送邮件
smtp.quit() # 5.退出登陆
except smtplib.SMTPException:
print("无法发送邮件")
5.发送带附件的邮件
代码如下:
# TODO 1.倒入需要相关的模块/类
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import smtplib
# TODO 2.包装email
# 创建一个带附件的实例
msg = MIMEMultipart()
msg["from"] = "xxxxxxxx@qq.com"
msg["to"] = "xxxxxxxx@qq.com"
subject = "Python SMTP 邮件测试"
msg["subject"] = Header(subject, "utf-8")
# 邮件正文内容
msg.attach(MIMEText("这是guiyin 现在开始Python邮件发送测试请查收....", "plain", "utf-8"))
# 构造附件1 传送当前目录下的user_info.txt文件
att1 = MIMEText(open("user_info.txt", "rb").read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="user_info.txt"'
msg.attach(att1)
# 构造附件2 传送当前目录下的gobang.py文件
att2 = MIMEText(open("d1.py", "rb").read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="d1.py"'
msg.attach(att2)
# TODO 3.发送邮件
# 准备发送邮件需要的基本信息
main_host = "smtp.qq.com"
main_user = "xxxxxxxx@qq.com"
main_pass = "填写自己获取的授权码"
sender = "xxxxxxxx@qq.com"
receiver = "xxxxxxxx@qq.com"
try:
smtp = smtplib.SMTP()
smtp.connect(main_host, 25)
smtp.login(main_user, main_pass)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
except smtplib.SMTPException:
print("无法发送邮件...")
6.在HTML文本中添加图片
代码如下:
# TODO 1.倒入需要相关的模块/类
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib
# TODO 2.包装email
# 创建一个带附件的实例
msg = MIMEMultipart("related")
msg["from"] = "xxxxxxxxxx@qq.com"
msg["to"] = "xxxxxxxxxx@qq.com"
subject = "Python SMTP 邮件测试"
msg["subject"] = Header(subject, "utf-8")
msg2 = MIMEMultipart("alternative")
msg.attach(msg2)
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="https://blog.csdn.net/guiyin1150?spm=1010.2135.3001.5113">点击进入guiyin1150的博客</a></p>
<p>图片演示: </p>
<p><img src="cid:image1"></p>
"""
msg2.attach(MIMEText(mail_msg, "html", "utf-8"))
# 指定图片为当前目录
msgImage = MIMEImage(open('timg.jpeg', "rb").read())
# 定义图片id 在html文本中引用
msgImage.add_header("Content-ID", "<image1>")
msg.attach(msgImage)
# TODO 3.发送邮件
# 准备发送邮件需要的基本信息
main_host = "smtp.qq.com"
main_user = "xxxxxxxxxx@qq.com"
main_pass = "填写自己获取到的授权码"
sender = "xxxxxxxxxx@qq.com"
receiver = "xxxxxxxxxx@qq.com"
try:
smtp = smtplib.SMTP()
smtp.connect(main_host, 25)
smtp.login(main_user, main_pass)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
except smtplib.SMTPException:
print("无法发送邮件...")
7. python给同一个人发送多封邮件
# TODO 1.倒入需要相关的模块/类
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
import smtplib
# TODO 2.包装email
# 创建一个带附件的实例
msg = MIMEMultipart()
msg["from"] = "xxxxxxxxx@qq.com"
msg["to"] = "xxxxxxxxx@qq.com"
subject = "Python SMTP 邮件测试"
msg["subject"] = Header(subject, "utf-8")
# 邮件正文内容
msg.attach(MIMEText("这是guiyin 现在开始Python邮件发送测试请查收....", "plain", "utf-8"))
# 构造附件1 传送当前目录下的user_info.txt文件
att1 = MIMEText(open("user_info.txt", "rb").read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="user_info.txt"'
msg.attach(att1)
# 构造附件2 传送当前目录下的gobang.py文件
att2 = MIMEText(open("d1.py", "rb").read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="d1.py"'
msg.attach(att2)
# TODO 3.发送邮件
# 准备发送邮件需要的基本信息
main_host = "smtp.qq.com"
main_user = "xxxxxxxxx@qq.com"
main_pass = "填写授权码"
sender = "xxxxxxxxx@qq.com"
receiver = "xxxxxxxxx@qq.com"
try:
smtp = smtplib.SMTP()
smtp.connect(main_host, 25)
smtp.login(main_user, main_pass)
# 给同一个人发很多封 直接在这里加一个for循环即可 5:表示发送5封
for i in range(5):
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
except smtplib.SMTPException:
print("无法发送邮件...")
8.python给不同的人发送不同内容的邮件
from email.mime.text import MIMEText # 导包
from email.header import Header
import smtplib
# TODO 2.配置发送邮件需要的基本信息
# 设置第三方smtp相关服务信息
main_host = "smtp.qq.com" # 设置服务器 如果是163 写smtp.163.com即可
main_user = "xxxxxxxxxx@qq.com" # 用户名 或者是xxxxxxx@163.com
main_pass = "授权码" # 授权码
# 设置接收与发送邮件者
sender = "xxxxxxxxxx@qq.com"
receiver = ["xxxxxxxxxx@qq.com", "xxxxxxxxxx@163.com", "xxxxxxxxxx@rzsyznzcwwgc.onexmail.com",
"xxxxxxxxxx@qq.com"]
# 不同邮箱对应的人的姓名
receiver_name = ["name1", "name2", "name3", "name4"]
# TODO 1.将发送的内容包装成为邮件需要的格式
content = """
Dear {}:
恭喜你!通过软件自动化测试工程师面试,薪资20K,16薪,请携带相关证件,与2020-11-11日前来我司报道,谢谢。
""" # 发送内容
for i in range(len(receiver)):
msg = MIMEText(content.format(receiver_name[receiver.index(receiver[i])]), 'plain', 'utf-8')
msg["from"] = Header("xxxxxxxxx@qq.com", "utf-8")
msg["To"] = Header("xxxxxxxxx@qq.com", "utf-8")
subject = "Python SMTP 邮件测试"
msg["subject"] = Header(subject, "utf-8")
# TODO 3.使用smtplib模块发送邮件
try:
smtp = smtplib.SMTP() # 1.创建一个smtp对象
smtp.connect(main_host, 25) # 2.连接smtp服务器 # 这里的端口号写587也是可以的
smtp.login(main_user, main_pass) # 3.登陆smtp服务器
smtp.sendmail(sender, receiver[i], msg.as_string()) # 4.发送邮件
smtp.quit() # 5.退出登陆
except smtplib.SMTPException:
print("无法发送邮件")