Probably the best way to do this is to use the template engine to load and generate HTML as a string from an HTML file. For example, if you are using the webapp2 jinja2 extras package, you can do something according to:
from webapp2_extras import jinja2 as webapp_extras_jinja2
Please note that for this you need to add jinja2 to the app.yaml libraries section, as in:
libraries: - name: webapp2 version: 2.5.2 - name: jinja2 version: 2.6
... and you also need to include the appropriate "webapp2_extras.jinja2" in the application configuration. Example:
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)
While you can just as easily open the HTML file yourself, the advantage of using a template engine such as jinja2 is that it will encourage you to build and reuse the HTML in a more reasonable way (whereas just loading the HTML file may result you end up using manual replacements). Itβs also just a quick safety reminder: if any of the data that you include in the email comes from unreliable sources (such as a user or another user), make sure that you check and verify the content correctly (and also turn on automatic shielding in the template engine).
Obviously, you can choose a template other than jinja2, but I specifically chose this one for my answer, as it is well supported and well documented for the App Engine.
source share