吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 966|回复: 10
收起左侧

[Python 转载] python发送带中文附件名的邮件

[复制链接]
harry20222022 发表于 2023-7-31 16:37
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

#设置登录及服务器信息
mail_host = 'smtp.163.com'
mail_user = 'USER'
mail_pass = 'PASS'
sender = 'SEND'
receivers = ['recive']

#设置eamil信息
#添加一个MIMEmultipart类,处理正文及附件
message = MIMEMultipart()
message["Accept-Language"] = "zh-CN"
message['From'] = sender
message['To'] = receivers[0]
message['Subject'] = 'title'
#推荐使用html格式的正文内容,这样比较灵活,可以附加图片地址,调整格式等
with open('ce.html','r',encoding='utf-8') as f:
    content = f.read()
#设置html格式参数
part1 = MIMEText(content,'html','utf-8')
#添加一个txt文本附件
with open('美国.txt','r')as h:
    content2 = h.read()
#设置txt参数
part2 = MIMEText(content2,'plain','utf-8')
#附件设置内容类型,方便起见,设置为二进制流
part2['Content-Type'] = 'application/octet-stream'
#设置附件头,添加文件名
part2.add_header("Content-Disposition", "attachment", filename=("gbk", "", "测试结果.txt"))
#part2['Content-Disposition'] = 'attachment;filename="demo.txt"'

#添加照片附件
with open('无标题2.png','rb')as fp:
    picture = MIMEImage(fp.read())
    #txt文件设置相似
    picture['Content-Type'] = 'application/octet-stream'
    picture['Content-Disposition'] = 'attachment;filename="picture.png"'
#将内容附加到邮件主体中
message.attach(part1)
message.attach(part2)
message.attach(picture)

#登录并发送
try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host,25)
    smtpObj.login(mail_user,mail_pass)
    smtpObj.sendmail(
        sender,receivers,message.as_string())
    print('success')
    smtpObj.quit()
except smtplib.SMTPException as e:
    print('error',e)

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
q444833950 + 1 + 1 鼓励转贴优秀软件安全工具和文档!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

vethenc 发表于 2023-7-31 17:16
感谢分享,帮楼主格式美化一下。


import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

#设置登录及服务器信息
mail_host = 'smtp.163.com'
mail_user = 'USER'
mail_pass = 'PASS'
sender = 'SEND'
receivers = ['recive']

#设置eamil信息
#添加一个MIMEmultipart类,处理正文及附件
message = MIMEMultipart()
message["Accept-Language"] = "zh-CN"
message['From'] = sender
message['To'] = receivers[0]
message['Subject'] = 'title'
#推荐使用html格式的正文内容,这样比较灵活,可以附加图片地址,调整格式等
with open('ce.html','r',encoding='utf-8') as f:
    content = f.read()
#设置html格式参数
part1 = MIMEText(content,'html','utf-8')
#添加一个txt文本附件
with open('美国.txt','r')as h:
    content2 = h.read()
#设置txt参数
part2 = MIMEText(content2,'plain','utf-8')
#附件设置内容类型,方便起见,设置为二进制流
part2['Content-Type'] = 'application/octet-stream'
#设置附件头,添加文件名
part2.add_header("Content-Disposition", "attachment", filename=("gbk", "", "测试结果.txt"))
#part2['Content-Disposition'] = 'attachment;filename="demo.txt"'

#添加照片附件
with open('无标题2.png','rb')as fp:
    picture = MIMEImage(fp.read())
    #与txt文件设置相似
    picture['Content-Type'] = 'application/octet-stream'
    picture['Content-Disposition'] = 'attachment;filename="picture.png"'
#将内容附加到邮件主体中
message.attach(part1)
message.attach(part2)
message.attach(picture)

#登录并发送
try:
    smtpObj = smtplib.SMTP()
    smtpObj.connect(mail_host,25)
    smtpObj.login(mail_user,mail_pass)
    smtpObj.sendmail(
        sender,receivers,message.as_string())
    print('success')
    smtpObj.quit()
except smtplib.SMTPException as e:
    print('error',e)
天轩科技 发表于 2023-7-31 19:08
vethenc 发表于 2023-7-31 17:16
感谢分享,帮楼主格式美化一下。

[md]```

【懂车帝】 1. 朋友同事一律不能到家里。 2. 不要随便带上司来家里吃饭。 3. 不要让她一个人在家点外卖倒垃圾。 4. 不要随便让邻居进门。 5. 有弟弟的话不要让她一个人在家。 6. 父亲老了让他



你这条签名。看的时候可真费劲
JokerDa 发表于 2023-7-31 18:33
choujie1689 发表于 2023-7-31 18:39
学习了,期待更多
CRG_44 发表于 2023-7-31 18:54
这个我当时写了程序有页面的,直接双击.exe就可以运行
鹿鸣 发表于 2023-7-31 19:55
感谢分享,学习一下
q444833950 发表于 2023-7-31 21:26
感谢楼主,真的很方便,替换直接就能使用了
siliverfox 发表于 2023-7-31 23:34
我是初入门的  该从哪里学啊?
vethenc 发表于 2023-8-1 07:02
天轩科技 发表于 2023-7-31 19:08
【懂车帝】 1. 朋友同事一律不能到家里。 2. 不要随便带上司来家里吃饭。 3. 不要让她一个人在家点外卖倒 ...

论坛等级对应的签名档大小有限制,系统自动做了节选,原文很长很精彩
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 02:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表