首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中使用MIME发送电子邮件时向雷鸟发送附件?

如何在python中使用MIME发送电子邮件时向雷鸟发送附件?
EN

Stack Overflow用户
提问于 2021-04-19 19:45:26
回答 1查看 115关注 0票数 0

我正在尝试使用MIME从Python发送电子邮件,电子邮件工作得很好,附件也显示在web电子邮件上,但不会出现在像雷鸟这样的电子邮件客户端。

这是我写的代码

代码语言:javascript
复制
#!/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"

任何帮助都意味着很多。我离这个太近了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-01 02:03:41

Close -- MIME编码规则相当挑剔。

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67161387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档