随笔 - 33  文章 - 0 评论 - 1 阅读 - 34936
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

截图模块

 

邮件正文发送图片,并附带附件

复制代码
 1 def attach_mail_module(mail_title,mail_content,mail_to_list,mail_ccto_list,attachment,attachment_file_name):
 2     print('==进入attach_mail_module模块==')
 3     
 4     #读取配置与密码,所有邮件信息都从配置文件来
 5     conf_file='e:\\p.txt'
 6     conf = configparser.ConfigParser()
 7     conf.read(conf_file,encoding="utf-8")
 8     mail_conf="mail_address_info"
 9     
10     abnormal_mail_reciever = conf.get(mail_conf,"mail_reciever")
11     smtp_address_ip = conf.get(mail_conf,"smtp_address_ip")
12     smtp_port = int(conf.get(mail_conf,"smtp_port"))
13     login_user_name = conf.get(mail_conf,"login_user_name")
14     login_passwd = conf.get(mail_conf,"login_passwd")
15     send_mail_from = conf.get(mail_conf,"send_mail_from")
16 
17     #设置主题,收件人,抄送人
18     msg = MIMEMultipart('mixed')
19     msg['Subject'] = Header(mail_title,'utf-8')
20     msg['From'] = send_mail_from
21     msg['To'] = ",".join(to_list)
22     msg['cc'] = ','.join(ccto_list)
23     receive = to_list
24     receive.extend(ccto_list)
25 
26     #设置附件(可以是中文名)
27     att = MIMEText(open(attachment, 'rb').read(), 'base64', 'utf-8') 
28     att["Content-Type"] = 'application/octet-stream'
29     att.add_header("Content-Disposition", "attachment", filename=("gbk", "", attachment_file_name+".xlsx"))
30     msg.attach(att) 
31     
32     #设置图片
33     content=mail_content
34     html_img = f'<p>{content}<br><img src="cid:image1"></br></p>' # html格式添加图片
35     f = open(r"E:\LHD6_base.png", 'rb')  #打开图片
36     msgimage = MIMEImage(f.read())
37     f.close()
38     msgimage.add_header('Content-ID', '<image1>')  # 设置图片
39     msg.attach(msgimage)
40     msg.attach(MIMEText(html_img,'html','utf-8'))  # 添加到邮件正文
41 
42     #发送邮件
43     smtp = smtplib.SMTP()
44     smtp.connect(smtp_address_ip,smtp_port)
45     smtp.login(login_user_name,login_passwd)
46     smtp.sendmail(send_mail_from, receive , msg.as_string())
47     smtp.quit()
48     print('==attach_mail_module结束==\n')
View Code
复制代码

 

posted on   洛丹伦的雪  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示