Error when python3 sends email with attachment name in Chinese

problem description

I wrote an email class, and everything went well, but something went wrong when the attachment name was changed to Chinese

the environmental background of the problems and what methods you have tried

uses python3* and email packages

Yes, not MIMEText, MIMEApplication, MIMEBase

related codes

/ / Please paste the code text below (do not replace the code with pictures)

//MIMEText
att1 = MIMEText(open(v, "rb").read(), "base64", "utf-8")
att1["Content-Type"] = "application/octet-stream"
-sharp
filename = (v.split("\\"))[-1]
att1["Content-Disposition"] = "attachment; filename="+filename
-sharp
self.message.attach(att1)

//MIMEApplication
xlsxpart = MIMEApplication(open(v, "rb").read())
basename = (v.split("\\"))[-1]
xlsxpart.add_header("Content-Disposition", "attachment", filename=("gbk", "", basename))
self.message.attach(xlsxpart)

what result do you expect? What is the error message actually seen?

you can also see an attachment by opening the mailbox with a browser, but the name is not correct. You can"t see the attachment directly by opening it with foxmail. Ask for expert advice. Thank you ~

May.29,2021

attachment name can be in Chinese. Here is a previous code snippet of myself, using make_header

.
from email.header import  make_header
file_msg = MIMEText(open(file,'rb').read(), 'base64', 'UTF-8')
file_msg["Content-Type"] = 'application/octet-stream;name="%s"'% make_header([(file,'UTF-8')]).encode('UTF-8')
file_msg["Content-Disposition"] = 'attachment;filename= "%s"' % make_header([(file, 'UTF-8')]).encode('UTF-8')
msg.attach(file_msg)

Mail transfer attachment name is abnormal in Chinese.

Menu