【发布时间】:2019-09-13 19:13:02
【问题描述】:
我做了一些谷歌研究,发现了很多东西。不幸的是,我不明白我发现了什么,所以我需要打开一个新的“公共请求”。正如我在标题中所说的,我想在我的电子邮件中附加一个 .txt 文件。很抱歉在成千上万的人已经这样做之后再次问这个问题,但这是我发送邮件的方法:
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
import config
#creating file to write the keys in later
file = open("my_filename.txt", "w+")
#creating subject and message for the email
subject = "Test123 new / updated"
msg = "look in attachment"
def send_mail(subject, msg)
try:
server = smtplib.SMTP("smtp.gmail.com:587")
server.ehlo()
server.starttls()
server.login(config.EMAIL_ADDRESS, config.PASSWORD)
message = "Subject: {}\n\n{}".format(subject, msg)
server.sendmail(config.EMAIL_ADDRESS, config.EMAIL_RECEIVER, message)
server.quit()
print("success: Email sent!")
except:
print("Email failed to send")
这对我来说效果很好(只是通过一些研究来管理它。最后我发现了类似的文件:
msg = MIMEMultipart()
#some other code to create a mail with subject and stuff like that
msg.attach(MIMEText(text))
如果我将“text”更改为“my_filename.txt”,它将不起作用。我需要改变什么?
【问题讨论】:
标签: python email attachment