一、smtp协议发送邮件
1.发送文本
# -*- coding: utf-8 -*-
# @Time : 2024/1/26 13:32
# @Author : 居里夫人吃橘子
# @File : demo01.py
# @Software: PyCharm
import smtplib
from email.mime.text import MIMEText
# 1.连接邮箱服务器
con = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 2.登录邮箱
con.login(user='QQ号@qq.com', password='授权码')
# 3.文本内容 _text:内容, _subtype='plain':文本格式 _charset=None:字符集编码
message=MIMEText('今天星期五,明天放假',_subtype='plain',_charset='utf-8')
# 设置标题
message['Subject'] = '邮件测试'
# 发送人
sender = 'QQ号@qq.com'
message['from'] = sender
# 收件人
receive = 'xxx@163.cn'
message['to'] = receive
try:
# 发送邮件
con.sendmail(sender, receive, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败,报错了{}'.format(e))
2.发送html
# -*- coding: utf-8 -*-
# @Time : 2024/1/26 13:54
# @Author : 居里夫人吃橘子
# @File : demo02.py
# @Software: PyCharm
# 发送html内容
import smtplib
from email.mime.text import MIMEText
# 1.连接邮箱服务器
con = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 2.登录邮箱
con.login(user='QQ号@qq.com', password='授权码')
# 3.打开html文件
with open(r'./file/report_20240111_10_17_30.html', 'rb') as f:
htmlContent = f.read()
message = MIMEText(str(htmlContent, encoding='utf-8'), 'html', 'utf-8')
# 邮件标题
message['Subject'] = '邮件HTML内容测试'
# 发送人
sender = 'QQ号@qq.com'
message['from'] = sender
# 收件人
receive = 'xxx@163.cn'
message['to'] = receive
try:
# 发送邮件
con.sendmail(sender, receive, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败,报错了{}'.format(e))
3.发送附件
# -*- coding: utf-8 -*-
# @Time : 2024/1/26 14:00
# @Author : 居里夫人吃橘子
# @File : demo03.py
# @Software: PyCharm
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 1.连接邮箱服务器
con = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 2.登录邮箱
con.login(user='QQ号@qq.com', password='授权码')
# 3.示例附件相当于盒子
message = MIMEMultipart()
with open(r'./file/report_20240111_10_17_30.html', 'rb') as f:
content = f.read()
# 把内容写在纸上
paper = MIMEText(content, 'base64', 'utf-8')
# 纸取名
paper['Content-Disposition'] = 'attachment;filename="111.html"'
# 把纸放入盒子
message.attach(paper)
# 正文内容
msg = MIMEText('正文内容', 'plain', 'utf-8')
# 把内容放入盒子
message.attach(msg)
# 标题
message['Subject'] = '邮件附件测试'
# 发送人
sender = 'QQ号@qq.com'
message['from'] = sender
# 收件人
receive = 'xxx@163.cn'
message['to'] = receive
try:
# 发送邮件
con.sendmail(sender, receive, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败,报错了{}'.format(e))
4.发送图片
# -*- coding: utf-8 -*-
# @Time : 2024/1/26 14:30
# @Author : 居里夫人吃橘子
# @File : demo04.py
# @Software: PyCharm
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# 1.连接邮箱服务器
con = smtplib.SMTP_SSL('smtp.qq.com', 465)
# 2.登录邮箱
con.login(user='QQ号@qq.com', password='授权码')
# 3.实例化图片 盒子
message = MIMEMultipart()
with open('file/123.jpg', 'rb') as f:
image = f.read()
# 放在纸上,图片
image_data = MIMEImage(image)
# 图片重新命名
image_data['Content-Disposition'] = 'attachment;filename="2.jpg"'
# 图片放入盒子
message.attach(image_data)
# 正文内容
msg = MIMEText('图片', 'html', 'utf-8')
# 正文放入盒子
message.attach(msg)
# 标题
message['Subject'] = '邮件图片测试'
# 发送人
sender = 'QQ号@qq.com'
message['from'] = sender
# 收件人
receive = 'xxx@163.cn'
message['to'] = receive
try:
# 发送邮件
con.sendmail(sender, receive, message.as_string())
print('邮件发送成功')
except Exception as e:
print('邮件发送失败,报错了{}'.format(e))
- 给多人发送
receive = ['xxx@163.cn','QQ号@qq.com']
message['to'] = ';'.join(receive)
二、zmail库发送邮件
1.安装
pip install zmail
2.代码实例
# -*- coding: utf-8 -*-
# @Time : 2024/1/26 14:32
# @Author : 居里夫人吃橘子
# @File : zmail_test.py
# @Software: PyCharm
import zmail
# 发送人
sender={'username':'QQ号@qq.com','password':'授权码'}
# 登录邮箱
server=zmail.server(sender['username'],sender['password'])
# 邮件内容
mail_content={
'subject':'我是标题',
# Content_text和Content_html只能选择一个使用
'Content_text':'我是邮件内容http://www.baidu.com',
# 'Content_html':'<a href="http://www.baidu.com">嗨,你好</a>',
'Attachments':'./file/123.jpg'
}
# 收件人
receive='xxx@163.cn'
# 发送邮件
server.send_mail(receive,mail_content)
- 发送多个附件
'Attachments':['./file/123.jpg','./file/234.jpg']
- 给多人发送邮件
receive=['duke@piesat.cn','xxxx@.com']