使用HTML文件在Appengine[PYTHON]中发送HTML电子邮件

使用HTML文件在Appengine[PYTHON]中发送HTML电子邮件,python,html,google-app-engine,Python,Html,Google App Engine,我想在用户注册网站后向他们发送一封HTML电子邮件。早些时候我写了这个脚本来发送 from google.appengine.api import mail message = mail.EmailMessage(sender="Example.com Support <support@example.com>", subject="Your account has been approved") message.to =

我想在用户注册网站后向他们发送一封HTML电子邮件。早些时候我写了这个脚本来发送

from google.appengine.api import mail

message = mail.EmailMessage(sender="Example.com Support <support@example.com>",
                            subject="Your account has been approved")

message.to = "Albert Johnson <Albert.Johnson@example.com>"

message.body = """
Dear Albert:

Your example.com account has been approved.  You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.

Please let us know if you have any questions.

The example.com Team
"""

message.html = """
<html><head></head><body>
Dear Albert:

Your example.com account has been approved.  You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.

Please let us know if you have any questions.

The example.com Team
</body></html>
"""
message.send()
但这是徒劳的。如何使用HTML文件代替HTML代码?

您可以设置

message.html = open('emailHTML.html').read()
获得与你现在所做的完全相同的效果;或者,您可以将HTML作为附件(因此电子邮件的正文仅为纯文本,但收件人可以将HTML作为附件下载),包括:


我不太确定在这两种情况下你希望实现什么,但这几乎是我能想到的仅有的两种可能性。如果两者都不令人满意,请编辑您的Q,向用户准确解释您希望此电子邮件的外观(正文应该是普通的还是html,是否应该有附件…?)。

最好的方法可能是从html文件中加载并生成html作为字符串。例如,如果您使用该软件包,您可以按照以下方式进行操作:

from webapp2_extras import jinja2 as webapp_extras_jinja2
# ...

def get_message_html():
  jinja2 = webapp_extras_jinja2.get_jinja2()
  return jinja2.render_template('relative/path/to/template.html')

# ...
def send_email():
   # ...
   message.html = get_message_html()
   # ...
请注意,要使其正常工作,您需要将jinja2添加到app.yaml的库部分,如下所示:

libraries:
- name: webapp2
  version: 2.5.2
- name: jinja2
  version: 2.6
。。。此外,您还需要在应用程序配置中包含适当的“webapp2_extras.jinja2”。例:

config = {
   'webapp2_extras.jinja2': {
     'template_path': 'path/containing/my/templates',
     'environment_args': {
       # Keep generated HTML short
       'trim_blocks': True,
       'extensions': [
         # Support auto-escaping for security
         'jinja2.ext.autoescape',
         # Handy but might not be needed for you
         'jinja2.ext.with_'
         # ... other extensions? ...
       ],
       # Auto-escape by default for security
       'autoescape': True
     },
     # .. other configuration options for jinja2 ...
   },
   # ... other configuration for the app ...
},
# ...
app = webapp2.WSGIApplication(routes, is_debug_enabled, config)
虽然您可以自己轻松地打开HTML文件,但使用jinja2等模板引擎的好处是,它将鼓励您以更合理的方式编写和重用HTML(而简单地加载HTML文件可能最终导致您手动应用替换)。另外,只是一个快速的安全提醒:如果您在电子邮件中包含的任何数据来自不受信任的来源(如用户或其他用户),请确保正确验证和检查内容的完整性(并在模板引擎中启用自动转义)


显然,您可以选择除jinja2之外的模板,但我特别选择了该模板作为我的答案,因为它为App Engine提供了良好的支持和文档记录。

您是否尝试过使用
open()
.readlines()或.read()
命令?@WasimThabraze,如果您没有首先编写文件
'emailHTML.html'
,当然,所讨论的文件将不存在,尝试打开并读取它将导致错误。在读取文件之前,它必须存在--这是给定的!-)您如何选择创建它是一个完全不同的问题(在应用程序引擎中,您无法写入文件,因此它需要是作为应用程序的一部分上载的只读数据文件)。@WasimThabraze,您需要确保文件包含在推送到服务器的源中,并且您使用的是正确的相对路径。“app.yaml”文件配置上传文件列表中包括或不包括的文件(默认情况下跳过某些文件)。
libraries:
- name: webapp2
  version: 2.5.2
- name: jinja2
  version: 2.6
config = {
   'webapp2_extras.jinja2': {
     'template_path': 'path/containing/my/templates',
     'environment_args': {
       # Keep generated HTML short
       'trim_blocks': True,
       'extensions': [
         # Support auto-escaping for security
         'jinja2.ext.autoescape',
         # Handy but might not be needed for you
         'jinja2.ext.with_'
         # ... other extensions? ...
       ],
       # Auto-escape by default for security
       'autoescape': True
     },
     # .. other configuration options for jinja2 ...
   },
   # ... other configuration for the app ...
},
# ...
app = webapp2.WSGIApplication(routes, is_debug_enabled, config)