【问题标题】:Sending an email w/ attachment on AppEngine (BlobInfo)在 App Engine (BlobInfo) 上发送带有/附件的电子邮件
【发布时间】:2012-09-19 16:14:40
【问题描述】:

我刚刚在 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()])

【问题讨论】:

    标签: python google-app-engine blobstore


    【解决方案1】:

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

    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()  
    

    【讨论】:

    • 嘿 Voscausa,我收到以下错误:引发 ERROR_MAP[e.application_error](e.error_detail) InvalidAttachmentTypeError: Invalid attach type
    • 我没有收到此错误。我使用此代码发送 CSV。我知道并非所有附件类型都是允许的。见此链接:developers.google.com/appengine/docs/python/mail/attachments
    • 我也在使用 CSV - 正如您在上面代码的第 1 行中看到的那样。该文件工作正常,当我使用 print blob_reader.read() 的内容时,它会按预期显示文本。还有其他想法吗?
    • 我使用以下文件创建了 CSV 文件:files.blobstore.create(mime_type='text/comma-separated-values', _blobinfo_uploaded_filename='test')
    • 我做了同样的事情 - 我将在上面的问题中发布新代码,包括文件的输入。
    猜你喜欢
    • 2012-03-24
    • 2015-11-08
    • 2015-09-09
    • 2010-10-27
    • 2015-08-28
    相关资源
    最近更新 更多