【问题标题】:How can I send attachments in an email reply?如何在电子邮件回复中发送附件?
【发布时间】:2018-10-08 18:15:49
【问题描述】:

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

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

【问题讨论】:

    标签: python email attachment email-attachments exchangelib


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2021-03-06
    • 1970-01-01
    相关资源
    最近更新 更多