我有一个小问题。
我使用:
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文件附件。你能帮我吗?
您可以这样实现:
filename = ...
with open(filename,'r') as f:
message = MIMEText(f.read())
message.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(message)
其中CCD_ 1是CCD_。
我找到了答案,但非常感谢!:)
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)