附件在python中使用smptplib连接两次

Shr*_*ava 7 python smtplib

我试图在python中实现一个功能,我想发送一个文件作为电子邮件提醒的附件一切正常.我收到了所需主题的电子邮件提醒,但唯一的问题是我在电子邮件提醒中两次获得相同的附件.

    fileMsg = email.mime.base.MIMEBase('application','octet-stream')
    fileMsg.set_payload(file('/home/bsingh/python_files/file_dict.txt').read())
    #email.encoders.encode_base64(fileMsg)
    fileMsg.add_header('Content-Disposition','attachment;filename=LogFile.txt')
    emailMsg.attach(fileMsg)

  # send email
    server = smtplib.SMTP(smtp_server)
    server.starttls()
    server.login(username, password)
    server.sendmail(from_add, to_addr,emailMsg.as_string())
    server.quit()
Run Code Online (Sandbox Code Playgroud)

小智 5

我自己一直遇到这个问题.我有'alternative'我的消息的MIMEMultipart类型.当我更改为默认值时'mixed',副本消失了.

因此,如果您创建了emailMsg使用MIMEMultipart('alternative'),则可能会遇到相同的问题.

我相信'alternative'是提供邮件正文的文本和html版本,所以我认为如果你使用它,除了你的附件之外你还需要提供它们.

我希望有所帮助.

我还没有找到任何好的解释; 电子邮件可能变得非常复杂

  • 将MIMEMultipart类型设置为"mixed"会中断具有正文的文本和HTML版本的电子邮件,即正文重复两次. (2认同)

Shr*_*ava 0

版本有问题..已解决