Templates
Author: Shaurya Sharma
The Email Service uses HTML and CSS templates stored in email_templates/. Each template consists of:
- An
.htmlfile (e.g.,button_template.html) - A corresponding
_styles.cssfile (e.g.,button_template_styles.css)
These templates are uploaded into MongoDB via upload_templates.py. The script:
- Reads each
.htmlfile. - Checks if a corresponding
_styles.cssfile exists. - Generates and stores a hash to detect changes.
How Templates Work
- Storage: Templates are inserted into the
templatescollection, alongside fields such astemplate_name,content, andcss. - Retrieval: When sending an email, the service fetches the template from the database using
template_id. - Rendering: The HTML is rendered with Jinja2, then CSS is optionally inlined for better email client support using a library such as
premailer.
Updating or Adding Templates
Note: Please ensure the each template name(Template file name) is unique.
To update existing templates or add new ones:
- Place the
.htmland_styles.cssfiles (with matching base filenames) into theemail_templates/directory. - Run the
upload_templates.pyscript.- If the hashes differ, the content is updated.
- Otherwise, a new template document is created if it doesn’t exist.
Example Use Case
- An admin wants a new layout for newsletters. They create
newsletter_template.htmlandnewsletter_template_styles.css. - They run
python upload_templates.py. - The new template is now stored in MongoDB and can be referenced when generating email content for the new newsletter.