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

How can I send attachments in an email reply?

我正在使用 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']
)

这行得通,但我现在不知道如何将附件添加到回复中。我试图设置属性 "attachments" 和 "attach" 但对象没有它们。

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

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