I want to send a HTML email to the users after they signup to the website. Earlier I wrote this script to send
from google.appengine.api import mail
message = mail.EmailMessage(sender="Example.com Support <[email protected]>",
subject="Your account has been approved")
message.to = "Albert Johnson <[email protected]>"
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 placing the HTML directly in the main code, I want to have a separate HTML file which would be used as a body. I tried to do it as follows:
message.html = 'emailHTML.html'
but in vain. How can I use a HTML file in the place of HTML code?
You can set
message.html = open('emailHTML.html').read()
to get exactly the same effect as what you're doing now; or, you could have the HTML as an attachment (so the email's body is just the plain text one, but the recipient can download the HTML as an attachment) with:
message.attachments = [('emailHTML.html', open('emailHTML.html').read())]
I'm not quite sure what you'd hope to accomplish in either case, but these are pretty much the only two possibilities I can think of. If neither is satisfactory, please edit your Q to explain exactly what you want this email to look like to the user (is the body supposed to be plain or html, is there supposed to be an attachment...?).
Probably the best way to do this would be to use a templating engine to load and generate the HTML as a string from the HTML file. For example, if you use the webapp2 jinja2 extras package, you could do something along the lines of:
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()
# ...
Note that to get this working, you need to add jinja2 to the libraries section of app.yaml as in:
libraries:
- name: webapp2
version: 2.5.2
- name: jinja2
version: 2.6
... and you also need to include an appropriate 'webapp2_extras.jinja2' to the app config. Ex:
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 benefit of using a templating engine such as jinja2 is that it will encourage you to compose and reuse the HTML in a more sane way (whereas simply loading the HTML file might result in you eventually applying substitutions by hand). Also, just a quick security reminder: if any of the data you include in the email comes from untrusted sources (like the user or another user), make sure to properly validate and sanity-check the content (and also enable auto-escaping in the templating engine).
You can obviously choose a templating other than jinja2, but I specifically chose that one for my answer since it is well supported and well documented for App Engine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With