【问题标题】:how to attach a pdf in google app engine python send_mail function?如何在谷歌应用引擎 python send_mail 函数中附加 pdf?
【发布时间】:2018-12-03 19:26:03
【问题描述】:

我找不到任何关于如何在 python(谷歌应用引擎)send_mail 函数中附加站点根文件夹中的文件(pdf)的示例。

 url_test = "https://mywebsite.com/pdf/test.pdf"
      test_file = urlfetch.fetch(url_test)

      if test_file.status_code == 200:
            test_document = test_file.content
      mail.send_mail(sender=EMAIL_SENDER,
                  to=['test@test.com'],
                  subject=subject,
                  body=theBody,
                  attachments=[("testing",test_document)])

决定用EmailMessage试试:

 message = mail.EmailMessage( sender=EMAIL_SENDER, 
 subject=subject,body=theBody,to=['myemail@gmail.com'],attachments= 
 [(attachname, blob.archivoBlob)])
 message.send()

上面的 blob 附件发送成功,但是附加相对路径的文件总是显示“无效附件”

 new_file = open(os.path.dirname(__file__) + 
 '/../pages/pdf/test.PDF').read()

 message = mail.EmailMessage( sender=EMAIL_SENDER, 
 subject=subject,body=theBody,to=['myemail@gmail.com'],attachments= 
 [('testing',new_file )])
 message.send()

在调试中,我还尝试通过这样做来查看文件是否正在被读取:

 logging.info(new_file)

它似乎正在读取文件,因为它输出了一些 unicode 字符

请帮助我为什么不能附加 PDF 而我可以附加 blob

【问题讨论】:

  • 您是在标准环境还是灵活环境中运行?
  • @LundinCast 运行标准环境
  • 由于记录 new_file 的输出是一些 unicode 字符而不是 PDF 的实际内容,这意味着问题不一定与 PDF 附件到 send_mail 函数()有关。在我看来,该文件没有从该位置正确读取。这个link 解释了预期的文件类型。
  • 另外,调用附件时,文件类型必须在文件标题上注明,例如附件= [('testing.pdf',new_file )])。查看此link
  • @oakinlaja 谢谢!这就是问题所在!我忘了提到“.pdf”扩展名。在那之后它工作了

标签: django python-2.7 google-app-engine


【解决方案1】:

调用附件时,文件类型必须在文件标题上注明,例如附件= [('testing.pdf',new_file )])。查看此link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-16
    • 2011-01-11
    • 2014-07-20
    • 2011-08-21
    • 2012-02-27
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    相关资源
    最近更新 更多