python 发送邮件的四种方法汇总
作者:小公瑾 时间:2022-04-09 05:44:18
这里针对smtplib做了一系列封装,可以完成以下四种场景:
发送纯文本的邮件
发送html页面的邮件
发送带附件文件的邮件
发送能展示图片的邮件
以上四种场景,已经做好了二次封装,经测试OK,使用时直接传入对应参数即可,直接上代码
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
class SendEMail(object):
"""封装发送邮件类"""
def __init__(self, host, port, msg_from, pwd):
self.msg_from = msg_from
self.password = pwd
# 邮箱服务器地址和端口
self.smtp_s = smtplib.SMTP_SSL(host=host, port=port)
# 发送方邮箱账号和授权码
self.smtp_s.login(user=msg_from, password=pwd)
def send_text(self, to_user, content, subject, content_type='plain'):
"""
发送文本邮件
:param to_user: 对方邮箱
:param content: 邮件正文
:param subject: 邮件主题
:param content_type: 内容格式:'plain' or 'html'
:return:
"""
msg = MIMEText(content, _subtype=content_type, _charset="utf8")
msg["From"] = self.msg_from
msg["To"] = to_user
msg["subject"] = subject
self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)
def send_file(self, to_user, content, subject, reports_path, filename, content_type='plain'):
"""
发送带文件的邮件
:param to_user: 对方邮箱
:param content: 邮件正文
:param subject: 邮件主题
:param reports_path: 文件路径
:param filename: 邮件中显示的文件名称
:param content_type: 内容格式
"""
file_content = open(reports_path, "rb").read()
msg = MIMEMultipart()
text_msg = MIMEText(content, _subtype=content_type, _charset="utf8")
msg.attach(text_msg)
file_msg = MIMEApplication(file_content)
file_msg.add_header('content-disposition', 'attachment', filename=filename)
msg.attach(file_msg)
msg["From"] = self.msg_from
msg["To"] = to_user
msg["subject"] = subject
self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)
def send_img(self, to_user, subject, content, filename, content_type='html'):
'''
发送带图片的邮件
:param to_user: 对方邮箱
:param subject: 邮件主题
:param content: 邮件正文
:param filename: 图片路径
:param content_type: 内容格式
'''
subject = subject
msg = MIMEMultipart('related')
# Html正文必须包含<img src="cid:imageid" alt="imageid" width="100%" height="100%>
content = MIMEText(content, _subtype=content_type, _charset="utf8")
msg.attach(content)
msg['Subject'] = subject
msg['From'] = self.msg_from
msg['To'] = to_user
with open(filename, "rb") as file:
img_data = file.read()
img = MIMEImage(img_data)
img.add_header('Content-ID', 'imageid')
msg.attach(img)
self.smtp_s.sendmail(self.msg_from, to_user, msg.as_string())
来源:https://www.cnblogs.com/xiaogongjin/p/13180836.html
标签:python,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Oracle轻松取得建表和索引的DDL语句
2009-02-26 10:26:00
CSS鼠标悬停tip效果
2007-08-26 17:32:00
![](https://img.aspxhome.com/file/UploadPic/20078/26/200782618155189s.gif)
数字人组件反写[asp组件开发实例4]
2009-06-09 13:20:00
wxpython+pymysql实现用户登陆功能
2023-01-06 11:32:04
![](https://img.aspxhome.com/file/2023/5/66875_0s.jpg)
PHP高级编程实例:编写守护进程
2023-10-27 02:03:22
python 异常捕获详解流程
2022-06-12 15:31:04
![](https://img.aspxhome.com/file/2023/9/71519_0s.jpg)
[翻译]标记语言和样式手册 Chapter 7 锚点
2008-01-28 14:01:00
![](https://img.aspxhome.com/file/UploadPic/20081/28/2008128141614907s.jpg)
无法通过Google网站管理工具验证的部分情况解决办法
2009-07-28 12:56:00
ASP 统计某字符串中“A”出现过的次数
2010-08-12 10:17:00
php中json_decode()和json_encode()的使用方法
2023-06-11 16:31:56
python替换字符串中的子串图文步骤
2021-06-23 22:02:47
![](https://img.aspxhome.com/file/2023/6/69006_0s.jpg)
通过排序引导用户的行为方式
2008-05-17 09:30:00
![](https://img.aspxhome.com/file/UploadPic/20085/17/200851794651896s.gif)
Dreaweaver MX 2004新功能:图片处理
2010-09-02 12:38:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113029322s.gif)
python 用所有标点符号分隔句子的示例
2022-09-18 01:41:57
python获取网页中所有图片并筛选指定分辨率的方法
2021-07-28 04:57:23
![](https://img.aspxhome.com/file/2023/6/71446_0s.jpg)
python 发送邮件的示例代码(Python2/3都可以直接使用)
2023-05-12 08:53:56
Python3 用什么IDE开发工具比较好
2022-10-03 13:43:07
![](https://img.aspxhome.com/file/2023/8/81588_0s.jpg)
uniapp页面跳转的五种方式总结
2023-08-23 19:46:24
![](https://img.aspxhome.com/file/2023/6/56136_0s.png)
关于mysql与mysqli
2011-02-23 12:03:00
Python逐行读取文件中内容的简单方法
2023-03-02 16:01:09
![](https://img.aspxhome.com/file/2023/9/79689_0s.jpg)