当我使用MIME和SMTPLIB而不是发送附件“ random.jpg”时,它发送的“ noname.eml”无法打开且看不见,因此,我无法正确发送附件。为什么会造成这种情况,我该如何解决?
我试图将扩展名从“ png”更改为“ jpg”,但问题仍然存在。
fromaddr1 = ""
toaddr1 = ""
accpass1 = ""
msg1 = MIMEMultipart()
msg1['From'] = fromaddr1
msg1['To'] = toaddr1
msg1['Subject'] = "YOUR COMPUTER HAS BEEN ACCESSED"
body1 = "Someone has gained access to your computer"
msg1.attach(MIMEText(body1, 'plain'))
filename1 = "random.jpg"
attachment1 = open("random.jpg","rb")
part1 = MIMEBase('application', 'octet-stream')
part1.set_payload((attachment1).read())
encoders.encode_base64(part1)
part1.add_header('Content-Disposition', "attachment1; filename1= %s" %
filename1)
msg1.attach(part1)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr1, accpass1)
text = msg1.as_string()
server.sendmail(fromaddr1, toaddr1, text)
server.quit()
答案 0 :(得分:0)
在标题中:
from email.mime.application import MIMEApplication
代替附件:
with open(PATH_TO_ZIP_FILE,'rb') as file:
msg.attach(MIMEApplication(file.read(), Name='filename.zip'))