Send HTML email address to Appengine [PYTHON] using HTML file

I want to send an HTML email to users after registering on the website. I previously wrote this script to send

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

But instead of putting the HTML directly in the main code, I want to have a separate HTML file that will be used as the body. I tried to do it as follows:

 message.html = 'emailHTML.html' 

but in vain. How can I use an HTML file instead of HTML?

+6
source share
2 answers

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

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.

+2
source

You can set

 message.html = open('emailHTML.html').read() 

to get the same effect as you now; or you could have HTML as an attachment (so the body of the message is just text, but the recipient can load the HTML as an attachment) with

 message.attachments = [('emailHTML.html', open('emailHTML.html').read())] 

I'm not quite sure what you would like to achieve anyway, but these are pretty much the only two possibilities that I can think of. If none of them is satisfactory, please edit your Q to explain exactly how you want this letter to look like a user (it is assumed that the body should be simple or html, there should be an attachment ...?).

+4
source

Source: https://habr.com/ru/post/982767/


All Articles