python 发送邮件不显示附件_通过python发送时,附件不会显示在Outlook中

使用Python发送带附件的电子邮件时,Outlook用户无法显示附件,转发给Gmail用户后显示正常,Gmail用户再转发给Outlook用户也能显示。文中给出了发送邮件的代码,附加文件为Excel文件,因邮件实现碎片化,难寻相关信息,正查找Outlook版本详情。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们尝试发送带有附件的电子邮件,但由于某些原因,使用outlook的用户无法显示附件。在

如果他们将电子邮件转发给使用gmail的人,则在gmail中显示良好。

如果gmail用户将电子邮件转发给outlook用户,它会显示在outlook中(可能是因为gmail重建了邮件)。在

这是我们用来发送电子邮件的代码:def send_email(headers={}, attachments=[], body={}):

ADDRESS_HEADERS = set(['from', 'to', 'cc', 'bcc', 'reply-to'])

msg = MIMEMultipart('alternative')

msg.preamble = "You need a MIME-aware email client to read this email.\n"

def add_headers():

def encode_address(v):

(name, address) = parseaddr(v)

name = str(Header(unicode(name), 'utf-8'))

address = address.encode('ascii')

return formataddr((name, address))

for key, value in headers.iteritems():

if not isinstance(value, list):

value = [value]

if key.lower() in ADDRESS_HEADERS:

value = map(encode_address, value)

msg[key.title()] = u';'.join(value)

def set_body():

msg.attach(MIMEText(body.get('text', ''), 'plain', _charset='utf-8'))

if 'html' in body:

msg.attach(MIMEText(body['html'], 'html', _charset='utf-8'))

def attach_file(attachment):

maintype, subtype = attachment['mimetype'].split("/", 1)

part = MIMEBase(maintype, subtype)

filename = attachment['filename']

name = attachment.get('name', os.path.basename(filename))

with open(filename, 'rb') as f:

part.set_payload(f.read())

encoders.encode_base64(part)

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

msg.attach(part)

add_headers()

map(attach_file, attachments)

set_body()

composed = msg.as_string()

p = subprocess.Popen("sendmail -t", shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

(stdout, stderr) = p.communicate(composed)

if p.returncode != 0:

raise IOError(u'{}\n\n{}'.format(stdout, stderr).strip())

由于电子邮件实现的碎片化,很难找到任何相关信息。在

我们附加的文件是mime类型为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet的excel文件

我正在尝试查找有关正在使用的outlook版本的详细信息。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值