当我将name设置为变量时,作为xlsx的Python电子邮件附件不起作用



我想不时向发送自动邮件,并在邮件中附加一个excel文件。如果我直接写下excel文件的名称是可以的,但如果我将其链接到一个变量,则会出现一个附加的无格式文件。收件箱中的电子邮件视图

简言之,我希望这一行使用一个名为attachmentFile的变量,而不是写";"2020-01-01.xlsx";part.add_header("Content-Disposition","attachment;filename="attachmentFile"(

import smtplib,ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders
attachmentFile="2021-01-02.xlsx"
def send_mail():
send_from= 'spotalert@****.com'
send_to='deniz.yildirim@****.com'
subject = 'Uygunsuz Punta Bildirimi'
body_1='Merhaban'
body_2= 'xxxxx Nolu Punta Tahribatli kontrolde uygunsuz cikmistir.n'
body_3='Uygunsuzluk sebebi =  xxxxxxx '      + 'Puntanin Bulundugu Posta :'+'n'
body_4= ' Bu otomatik bir maildir lutfen cevap vermeyiniz.n'
body_5='Uygunsuzlugun ciktigi tarih'+ 'n'

text = body_1 + body_2 + body_3 + body_4 +body_5 
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', "octet-stream") 
part.set_payload(open(attachmentFile, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="2021-01-02.xlsx"')
msg.attach(part)

在代码的倒数第二行,而不是:

part.add_header('Content-Disposition', 'attachment; filename="2021-01-02.xlsx"')

使用这个:

part.add_header('Content-Disposition', 'attachment', filename=attachmentFile)

相关内容

  • 没有找到相关文章

最新更新