【问题标题】:Emailing csv attachments from Google App Engine without downloading the attachments从 Google App Engine 通过电子邮件发送 csv 附件而不下载附件
【发布时间】:2014-04-23 04:31:50
【问题描述】:

我找到了这个帖子:GAE Python - How to attach the results of csv.writer to an email?

这有助于让我能够将 csv 附加到来自 Google App Engine 的电子邮件。我的代码如下:

self.response.headers[str('Content-Type')] = str('text/csv')
self.response.headers[str('Content-Disposition')] = str('attachment; filename="data.csv"')
writer = csv.writer(self.response.out)
[write csv here]

message=mail.EmailMessage(sender='test@example.com',subject='Subject', attachments=[("data.csv",self.response.body)])
message.to=[to email here]
message.html='Message Body'
message.send()

问题在于,当此代码运行时,不仅会发送电子邮件,还会下载文件。有没有办法阻止文件被下载?

【问题讨论】:

    标签: python google-app-engine csv attachment email-attachments


    【解决方案1】:

    它正在下载,因为您正在将其写入响应输出。您应该将附件内容写入临时文件/内存并附加。

    也不需要设置这些标题。

    您引用的SO问题的公认答案不正确,其他答案最好https://stackoverflow.com/a/15453323/2018227

    【讨论】:

      猜你喜欢
      • 2012-03-24
      • 2015-11-08
      • 1970-01-01
      • 2010-10-27
      • 2016-10-27
      • 2019-10-05
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      相关资源
      最近更新 更多