python - 在 App Engine (BlobInfo) 上发送带有附件的电子邮件

标签 python google-app-engine blobstore

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

我什至可以将 BlobInfo 作为附件发送(=字节字符串)吗?如果没有的话我该如何翻译呢?

提前致谢:)

    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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="493d2c3a3d092e26262e252c24282025672a2624" rel="noreferrer noopener nofollow">[email protected]</a>"
sender_address = "Test <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c78697f784c6b63636b6069616d6560226f6361" rel="noreferrer noopener nofollow">[email protected]</a>>"
subject = "Test"
body = "Test"
mail.send_mail(sender_address, user_address, subject, body, attachments=[blob_info.filename, blob_reader.read()])

最佳答案

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

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 = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec86838482ac89948d819c8089c28f8381" rel="noreferrer noopener nofollow">[email protected]</a>'
    message.body = 'Download attached CSV'
    message.attachments = [blob_info.filename,blob_reader.read()]
    message.send()  

关于python - 在 App Engine (BlobInfo) 上发送带有附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12498759/

相关文章:

Python 附加列标题并将列值从列表附加到 csv

python - `exit` 关键字在 Python3 中用 Jupyter Notebook 做什么?

PHP GAE 无法在本地运行 SDK

python - 将视频上传到 google app engine blobstore

python、Django 与使用 urllib2 发送 pdf 相关

java - 给定 BlobstoreService 提供的 HttpServletResponse,如何获取提供的 blob?

python - 在 App Engine 上使用 Python 上传大文件时如何防止 Blob 存储上传 URL 过期

python - 如何缩进python记录器打印的多行消息?

python - 如何让 Google App Engine 在下载过程中进行gunzip?

python - 在 Python 中合并两个文件并排序