python3实现带多张图片、附件的邮件发送
作者:SoaringXu 时间:2023-05-11 06:51:10
本文实例为大家分享了python3实现多张图片附件邮件发送的具体代码,供大家参考,具体内容如下
直接上代码,没有注释!
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.header import Header
class Mail(object):
def __init__(self, host, nickname, username, password, postfix):
self.host = host
self.nickname = nickname
self.username = username
self.password = password
self.postfix = postfix
def send_mail(self, to_list, subject, content, cc_list=[], encode='gbk', is_html=True, images=[]):
me = str(Header(self.nickname, encode)) + "<" + self.username + "@" + self.postfix + ">"
msg = MIMEMultipart()
msg['Subject'] = Header(subject, encode)
msg['From'] = me
msg['To'] = ','.join(to_list)
msg['Cc'] = ','.join(cc_list)
if is_html:
mail_msg = ''
for i in range(len(images)):
mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
msg.attach(MIMEText(content + mail_msg, 'html', 'utf-8'))
for i, img_name in enumerate(images):
with open(img_name, 'rb') as fp:
img_data = fp.read()
msg_image = MIMEImage(img_data)
msg_image.add_header('Content-ID', '<image%d>' % (i+1))
msg.attach(msg_image)
# 将图片作为附件
# image = MIMEImage(img_data, _subtype='octet-stream')
# image.add_header('Content-Disposition', 'attachment', filename=images[i])
# msg.attach(image)
else:
msg_content = MIMEText(content, 'plain', encode)
msg.attach(msg_content)
try:
s = smtplib.SMTP()
# s.set_debuglevel(1)
s.connect(self.host)
s.login(self.username, self.password)
s.sendmail(me, to_list + cc_list, msg.as_string())
s.quit()
s.close()
return True
except Exception as e:
print(e)
return False
def send_mail(to_list, title, content, cc_list=[], encode='utf-8', is_html=True, images=[]):
content = '<pre>%s</pre>' % content
m = Mail('smtp.163.com', 'TV-APP TEST', 'tvapp_qa', 'ujlnluutpfespgxz', '163.com')
m.send_mail(to_list, title, content, cc_list, encode, is_html, images)
if __name__ == '__main__':
images = [
'1.png',
'2.png',
'3.png',
'4.png',
]
import time
title = 'new images %s' % time.strftime('%H:%M:%S')
content = 'this is attach images %s' % time.time()
send_mail(['x@163.com'], title, content, ['xx@163.com', 'xxx@163.com'], 'utf-8', True, images)
后记
调试发送多张图片的时候遇到的问题:
用for循环生成的mail_msg,不能直接attach,需要和content一起attach
mail_msg = ''
for i in range(len(images)):
mail_msg += '<p><img src="cid:image%d" height="240" width="320"></p>' % (i+1)
msg.attach(MIMEText(**content** + mail_msg, 'html', 'utf-8'))
来源:https://blog.csdn.net/weixin_44152831/article/details/89214911
标签:python3,邮件发送
0
投稿
猜你喜欢
超详细注释之OpenCV构建透明的叠加层
2021-08-18 14:51:01
JS HTML5拖拽上传图片预览
2024-04-22 13:03:38
Python 标准库 fileinput与文件迭代器
2023-10-31 22:36:50
如何优雅安全的备份MySQL数据
2024-01-28 01:07:31
python使用htmllib分析网页内容的方法
2022-05-22 13:28:33
python Django框架快速入门教程(后台管理)
2022-04-17 11:43:12
Python实现一个简单的递归下降分析器
2022-10-05 16:23:13
5款最强且免费的Python IDE小结
2022-12-07 05:25:19
Go语言常见哈希函数的使用
2024-02-04 16:09:23
Python 敏感词过滤的实现示例
2021-07-04 12:17:28
Pytest+request+Allure实现接口自动化框架
2023-08-12 17:29:33
python去除字符串中的换行符
2021-07-11 12:35:23
根据表名和索引获取需要的列名的存储过程
2024-01-23 04:24:14
通过Python实现电脑定时关机的两种方法
2023-05-09 03:23:50
JavaScript学习心得之如何走出初学困境
2008-12-24 13:30:00
Django-Model数据库操作(增删改查、连表结构)详解
2024-01-20 14:11:17
Python利用Redis计算经纬度距离案例
2021-03-05 04:51:35
在ie6下的hover伪类的使用
2008-06-01 13:51:00
go语言日志记录库简单使用方法实例分析
2024-05-02 16:25:40
mysql分表分库的应用场景和设计方式
2024-01-22 05:49:39