在App Engine上发送带有/ attachment的电子邮件(BlobInfo)

时间:2012-09-19 16:14:40

标签: python google-app-engine blobstore

我刚刚在AppEngine上传了这个 - 尝试发送带附件的电子邮件(blobinfo)。这将返回一个空白页面 - 没有错误消息。当我遗漏附件时,电子邮件会被发送,当我包含电子邮件时,电子邮件永远不会到达,但是再次:没有错误消息。

我甚至可以发送BlobInfo作为附件(= Bytestring)吗?如果没有,我该如何翻译呢?

提前致谢:)

    dataset = ""
for i in range(len(newer_table)):
  for j in range(len(newer_table[i])):
    dataset = dataset + str(newer_table[i][j]) + ','
  dataset += '\n'

file_name = files.blobstore.create(mime_type='text/comma-separated-values', _blobinfo_uploaded_filename='test')
with files.open(file_name, 'a') as f:
  f.write(dataset)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
blob_info = blobstore.BlobInfo.get(blob_key)
blob_reader = blobstore.BlobReader(blob_key)

#self.response.out.write(blob_reader.read())

user_address = "test@googlemail.com"
sender_address = "Test <test@googlemail.com>"
subject = "Test"
body = "Test"
mail.send_mail(sender_address, user_address, subject, body, attachments=[blob_info.filename, blob_reader.read()])

1 个答案:

答案 0 :(得分:2)

您必须阅读附件才能将其作为附件发送:

blob_reader = blobstore.BlobReader(blob_key)
...
mail.send_mail(sender_address, user_address, subject, body, attachments=[blob_info.filename,blob_reader.read()])
顺便说一下。我更喜欢使用Amazon SES从GAE发送邮件,因为GAE邮件API不提供有关邮件传递的任何信息。

以下是我用来发送邮件的代码:

    message = mail.EmailMessage(sender = 'noreply@....', subject = 'CSV')
    message.to = 'john@example.com'
    message.body = 'Download attached CSV'
    message.attachments = [blob_info.filename,blob_reader.read()]
    message.send()