如何在电子邮件回复中发送附件?

时间:2018-10-03 14:00:24

标签: python email attachment email-attachments exchangelib

我正在使用exchangelib程序包连接到Exchange。我需要在回复中发送附件。发送普通邮件时,我将附件添加到Message对象中,如下所示:

message = Message()
message.account = account
message.subject = 'subject'
message.body = 'text'
message.to_recipients = [Mailbox(email_address='example@gmail.com')]
message.cc_recipients = ['example2@gmail.com']

for attachment in attachments or []:
    with open(attachment['path'], 'rb') as f:
        file = FileAttachment(name=attachment['file_name'], content=f.read())
        message.attach(file)

并发送回复:

reply = message.reply(
    subject='Re: subject',
    body='texto',
    to_recipients=['example@gmail.com']
)

这有效,但是我现在不知道如何在回复中添加附件。我试图将属性设置为“附件”和“附件”,但是对象没有它们。

1 个答案:

答案 0 :(得分:1)

Message.reply()方法创建并发送一个不支持附件的ReplyToItem项目。参见https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/replytoitem

因此,如果您要发送包含附件的回复,只需创建一个普通的邮件项目,标题为'Re: some subject',包含附件,并在需要时引用原始邮件。