GAE Python - 如何将csv.writer的结果附加到电子邮件? [英] GAE Python - How to attach the results of csv.writer to an email?

查看:214
本文介绍了GAE Python - 如何将csv.writer的结果附加到电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GAE Python我从我的数据存储区写一个CSV,并希望通过电子邮件发送,而不存储在Blobstore。这意味着每天一次作为CRON作业运行。



导出工作正常,但我不知道如何将CSV添加到电子邮件的附件。您的输入将非常感谢。

  class ShopExport(webapp2.RequestHandler):
def get
shops = Shop.all()。filter('active =',True).order('name')
self.response.headers [str('Content-Type')] = str 'application / csv')
self.response.headers [str('Content-Disposition')] = str('attachment; filename =shops.csv')
writer = unicodecsv.writer self.response.out,encoding ='utf-8')
writer.writerow([id,name,domain,category,deeplink,region])
writer.writerow([shop.keyname,shop.name,shop.url,shop.category,shop.url_aff,region])

message = mail.EmailMessage(sender =Me< me @ gmail.com>,
subject =Shop Export,
attachments = [(shops.csv,self.response.out)])
message.to = Someone< someone@gmail.com>,
message.html ='请找到附加的商店出口'
message.send()
pre>

解决方案



 code> message = mail.EmailMessage(sender =Me< me@gmail.com>,
subject =Shop Export,
attachments = [(shops.csv, self.response.body)])


On GAE Python I am writing a CSV from my datastore and would like to send it by email without storing it in the blobstore. This is intended to run as a CRON job once per day.

The export works fine, however I don't know how to add the CSV to the attachment of the email. Your input would be very much appreciated.

class ShopExport(webapp2.RequestHandler):
  def get(self):
    shops = Shop.all().filter('active =', True).order('name')
    self.response.headers[str('Content-Type')] = str('application/csv')
    self.response.headers[str('Content-Disposition')] = str('attachment; filename="shops.csv"')
    writer = unicodecsv.writer(self.response.out, encoding='utf-8')
    writer.writerow(["id", "name", "domain", "category", "deeplink", "region"])
    writer.writerow([shop.keyname, shop.name, shop.url, shop.category, shop.url_aff, region])

    message = mail.EmailMessage(sender="Me <me@gmail.com>",
            subject="Shop Export",
            attachments=[("shops.csv", self.response.out)])
    message.to="Someone <someone@gmail.com>",
    message.html= 'Please find attached the shop export'
    message.send()

解决方案

try to modify this line.

message = mail.EmailMessage(sender="Me <me@gmail.com>",
        subject="Shop Export",
        attachments=[("shops.csv", self.response.body)])

这篇关于GAE Python - 如何将csv.writer的结果附加到电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆