# Usage Guide ## Installation To install `django-templated-email-md`, run the following command: ```bash pip install django-templated-email-md ``` ## Configuration ### 1. Add to `INSTALLED_APPS` Add `templated_email_md` to your `INSTALLED_APPS` in `settings.py`: ```python INSTALLED_APPS = [ # ... 'templated_email_md', # ... ] ``` ### 2. Update Settings Assuming you have already installed and configured [django-templated-email](https://github.com/vintasoftware/django-templated-email/), update your Django settings as follows: ```python # settings.py # Configure the templated email backend TEMPLATED_EMAIL_BACKEND = 'templated_email_md.backend.MarkdownTemplateBackend' # Specify the base HTML template for wrapping your content TEMPLATED_EMAIL_BASE_HTML_TEMPLATE = 'templated_email/markdown_base.html' # Set the directory where your email templates are stored TEMPLATED_EMAIL_TEMPLATE_DIR = 'templated_email/' # Ensure there's a trailing slash # Define the file extension for your Markdown templates TEMPLATED_EMAIL_FILE_EXTENSION = 'md' # Optional: Specify Markdown extensions if needed TEMPLATED_EMAIL_MARKDOWN_EXTENSIONS = [ 'markdown.extensions.extra', 'markdown.extensions.meta', 'markdown.extensions.tables', ] ``` ### 3. Ensure Template Loaders Include `APP_DIRS` Make sure that your `TEMPLATES` setting includes `APP_DIRS` set to `True` or includes the `django.template.loaders.app_directories.Loader`: ```python # settings.py TEMPLATES = [ { # ... 'APP_DIRS': True, # ... }, ] ``` ## Creating Markdown Templates Place your Markdown email templates in the `templated_email/` directory within your project's templates directory or within an app's `templates` directory. ### Example Template: `templated_email/welcome.md` *Note: The subject and preheader can be provided as template blocks or as context arguments when sending email.* ```markdown {% load i18n %} {% block subject %}{% trans "Welcome to Our Service" %}{% endblock %} {% block preheader %}{% trans "Thanks for signing up!" %}{% endblock %} {% block content %} # {% trans "Welcome" %}, {{ user.first_name }}! {% trans "We're thrilled to have you join our service. Here are a few things you can do to get started:" %} 1. **{% trans "Complete your profile" %}** 2. **{% trans "Explore our features" %}** 3. **{% trans "Connect with other users" %}** {% trans "If you have any questions, don't hesitate to reach out to our support team." %} {% trans "Best regards," %} {% trans "The Team" %} {% endblock %} ``` ### Template Blocks - **subject**: Defines the email subject line. - **preheader** *(optional)*: A short summary text that follows the subject line when viewing the email in an inbox. - **content**: The main content of your email. ### Using Django Template Syntax You can use Django's template language within your Markdown templates to make each email dynamic. ## Sending Emails Use the [`send_templated_mail`](https://github.com/vintasoftware/django-templated-email/?tab=readme-ov-file#sending-templated-emails) function from `django-templated-email` to send emails using your Markdown templates: ```python from templated_email import send_templated_mail send_templated_mail( template_name='welcome', from_email='from@example.com', recipient_list=['to@example.com'], context={ 'user': user_instance, # Add other context variables as needed }, ) ``` *Remember to provide all necessary context variables when sending the email.* ### Important Notes - **Context Variables**: Ensure that all variables used in your templates are provided in the `context` dictionary. - **Template Name**: Do not include the file extension when specifying the `template_name`. ## Advanced Usage ### Custom Base Template While we recommend you stick with the provided base template, you can create a custom base HTML template to wrap your Markdown content. This allows you to define the overall structure and style of your emails. There are two ways to achieve this: 1. **Template Overrides**: Within the `templates/` directory in your project's base directory, create a `templated_email/` directory. Then add a `markdown_base.html` file to serve as the base template. This will override the default base template from the package. 2. **Custom Base Template**: Place your custom base HTML template elsewhere and update the `TEMPLATED_EMAIL_BASE_HTML_TEMPLATE` setting to point to it. #### Example: `templated_email/markdown_base.html` ```html {{ subject }} {% spaceless %} {% block content %}{{ markdown_content|safe }}{% endblock %} {% endspaceless %} ``` Update the `TEMPLATED_EMAIL_BASE_HTML_TEMPLATE` setting if you use a custom template in a different location: ```python TEMPLATED_EMAIL_BASE_HTML_TEMPLATE = 'templated_email/markdown_base.html' ``` ### Inline Styles The `MarkdownTemplateBackend` uses Premailer to inline CSS styles for better email client compatibility. - **Include `