【问题标题】:How to send Attachments to Thunderbird when sending email using MIME in python?在 python 中使用 MIME 发送电子邮件时如何将附件发送到 Thunderbird?
【发布时间】:2021-04-19 11:45:25
【问题描述】:

我正在尝试使用 MIME 从 Python 发送电子邮件,电子邮件工作正常,附件也显示在网络电子邮件上,但不会出现在电子邮件客户端(如 Thunderbird)中。

这是我写的代码

#!/usr/bin/python
import smtplib
from smtplib import SMTPException
import base64

filename = "ticketexport.csv"

fo = open(filename, "rb")
filecontent = fo.read()
encodedcontent = base64.b64encode(filecontent) 

sender = 'erp@at.biz'
receivers = ["shravya@at.biz"]

marker = "AUNIQUEMARKER"

body ="""
LO CUSTOMER SUPPORT TICKETS.
"""

part1 = """From: AT <erp@atc.erp>
To: SS<shravya@at.biz>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)


part2 = """Content-Type: text/plain
Content-Transfer-Encoding:8bit

%s
--%s
""" % (body,marker)

# Define the attachment section
part3 = """Content-Type: multipart/mixed; name=\"%s\"
Content-Transfer-Encoding:base64
Content-Disposition: attachment; filename=%s

%s
--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3

try:
   smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465)
   smtpObj.login('erp@at.biz', 'jklO4d')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException as e:
   print(e)
   print "Error: unable to send email"

任何帮助肯定意义重大。我很接近这个。

【问题讨论】:

  • part1 中,Content-Type 和第一个标记之间需要有一个空行。
  • part3 中,Content-Type 应该是application/octet-stream。该部分不是多部分的。
  • 蒂姆·罗伯茨,你的回答真的很有帮助,你能把它评论为答案吗?这样我就可以投票了

标签: python mime-types sendmail smtplib


【解决方案1】:

关闭 -- MIME 编码规则非常挑剔。

part1 中,Content-Type 和第一个标记之间需要有一个空行。

part3 中,Content-Type 应该是application/octet-stream。该部分不是多部分的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 2021-10-13
    • 2016-06-25
    • 2017-11-20
    • 2014-05-27
    • 2016-09-20
    • 2015-04-24
    • 2015-06-19
    相关资源
    最近更新 更多