我有一个小问题。
我使用:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
msg = MIMEMultipart()
msg['From'] = 'me@gmail.com'
msg['To'] = 'you@gmail.com'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))
mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('me@gmail.com', 'mypassword')
mailserver.sendmail('me@gmail.com','you@gmail.com',msg.as_string())
mailserver.quit()
一切都很好 - 但我想添加一个TXT文件附件。你可以帮助我吗?
答案 0 :(得分:0)
你可以这样做:
filename = ...
with open(filename,'r') as f:
message = MIMEText(f.read())
message.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(message)
其中msg
是MIMEMultiPart
对象。
答案 1 :(得分:0)
我找到了回答,但感谢很多! :)
filename='/www/pages/DANE/komunikaty.txt'
fp=open(filename,'rb')
att = email.mime.application.MIMEApplication(fp.read(),_subtype="txt")
fp.close()
att.add_header('Content-Disposition','attachment',filename=filename)
msg.attach(att)