### Install Django-Templated-Email Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This command installs the `django-templated-email` library using pip, making it available for use in your Django project. ```Shell pip install django-templated-email ``` -------------------------------- ### Send Templated Email using send_templated_mail Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This example shows how to send an email using the `send_templated_mail` shortcut. It requires a template name, sender email, recipient list, and a context dictionary for template rendering. Optional parameters for CC, BCC, headers, and template prefixes/suffixes are also shown. ```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={ 'username':request.user.username, 'full_name':request.user.get_full_name(), 'signup_date':request.user.date_joined }, # Optional: # cc=['cc@example.com'], # bcc=['bcc@example.com'], # headers={'My-Custom-Header':'Custom Value'}, # template_prefix="my_emails/", # template_suffix="email", ) ``` -------------------------------- ### Example: Add Context Data for Django Templated Email Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This Python snippet illustrates how to extend the context data used for rendering email templates by overriding the templated_email_get_context_data method. It demonstrates calling the super method to inherit existing context and then adding custom data. ```python def templated_email_get_context_data(self, **kwargs): context = super(ThisClassView, self).templated_email_get_context_data(**kwargs) # add things to context return context ``` -------------------------------- ### Example: Get Email Recipients in Django Templated Email Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This Python snippet demonstrates how to implement the templated_email_get_recipients method within a Django view. It shows how to dynamically retrieve the email address from form data to determine the recipients for the email. ```python def templated_email_get_recipients(self, form): return [form.data['email']] ``` -------------------------------- ### Define a Basic Content Block in Django Template Source: https://github.com/vintasoftware/django-templated-email/blob/develop/tests/template_fixtures/templated_email/legacy.html This snippet demonstrates the fundamental structure of a 'block' tag in Django templates. Blocks are used to define sections of content that can be overridden by child templates, facilitating content reuse and inheritance. This pattern is particularly useful for structuring email layouts or other templated content where specific sections need to be customizable. ```Django Template Language {% block test %} test {% endblock %} ``` -------------------------------- ### Example: Customize Send Email Keyword Arguments in Django Templated Email Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This Python snippet shows how to modify the keyword arguments passed to the email sending function by implementing templated_email_get_send_email_kwargs. It demonstrates how to retrieve default arguments from the super method and then add or override specific parameters like BCC recipients. ```python def templated_email_get_send_email_kwargs(valid, form): kwargs = super(ThisClassView, self).templated_email_get_send_email_kwargs(valid, form) kwargs['bcc'] = ['admin@example.com'] return kwargs ``` -------------------------------- ### Disable Automatic Plain Text Generation from HTML Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This setting allows you to disable the automatic conversion of the HTML email part to a plain text part. By default, if `html2text` is installed and the plain block is not specified, the plain part is generated from HTML. ```Python TEMPLATED_EMAIL_AUTO_PLAIN = False ``` -------------------------------- ### Release New Version of Django Templated Email Package Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst These shell commands detail the process for releasing a new version of the `django-templated-email` package. It involves updating the `CHANGELOG`, incrementing the version using `bumpversion`, publishing to PyPI, and pushing Git tags. ```Shell bumpversion [major,minor,patch] python setup.py publish git push origin --tags ``` -------------------------------- ### Configure Templated Email Backend in Django Settings Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This snippet demonstrates how to configure the `TEMPLATED_EMAIL_BACKEND` in your Django `settings.py`. You can specify the backend using its full path, a shortcut path, or by directly importing and assigning the class. ```Python TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.vanilla_django.TemplateBackend' # You can use a shortcut version TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.vanilla_django' # You can also use a class directly from templated_email.backends.vanilla_django import TemplateBackend TEMPLATED_EMAIL_BACKEND = TemplateBackend ``` -------------------------------- ### Configure Global Settings for Django Templated Email Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This Python code block provides a comprehensive list of global settings available for configuring Django-Templated-Email. It includes options for defining the sender email, email backend, template directory, file extension, plain text conversion, and integration with Anymail. ```python TEMPLATED_EMAIL_FROM_EMAIL = None # String containing the email to send the email from - fallback to DEFAULT_FROM_EMAIL TEMPLATED_EMAIL_BACKEND = TemplateBackend # The backend class that will send the email, as a string like 'foo.bar.TemplateBackend' or the class reference itself TEMPLATED_EMAIL_TEMPLATE_DIR = 'templated_email/' # The directory containing the templates, use '' if using the top level TEMPLATED_EMAIL_FILE_EXTENSION = 'email' # The file extension of the template files TEMPLATED_EMAIL_AUTO_PLAIN = True # Set to false to disable the behavior of calculating the plain part from the html part of the email when `html2text ` is installed TEMPLATED_EMAIL_PLAIN_FUNCTION = None # Specify a custom function that converts from HTML to the plain part # Specific for anymail integration: TEMPLATED_EMAIL_EMAIL_MESSAGE_CLASS = 'django.core.mail.EmailMessage' # Replaces django.core.mail.EmailMessage TEMPLATED_EMAIL_EMAIL_MULTIALTERNATIVES_CLASS = 'django.core.mail.EmailMultiAlternatives' # Replaces django.core.mail.EmailMultiAlternatives ``` -------------------------------- ### Generate Web View Links for Django Templated Emails Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst Details how to enable a web view link for sent emails, allowing recipients to view the email in a browser. This involves adding `templated_email` to `INSTALLED_APPS`, including its URLs, setting `create_link=True` when sending the email, and finally adding the link to your email template. ```Python # Add templated email to INSTALLED_APPS INSTALLED_APPS = [ ... 'templated_email' ] ``` ```Python # and this to your url patterns url(r'^', include('templated_email.urls', namespace='templated_email')), ``` ```Python # when sending the email use the *create_link* parameter. send_templated_mail( template_name='welcome', from_email='from@example.com', recipient_list=['to@example.com'], context={}, create_link=True) ``` ```HTML {% if email_uuid %} You can view this e-mail on the web here: here {% endif %} ``` -------------------------------- ### Integrate Django Templated Email with Django Anymail Message Classes Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst Demonstrates how to configure django-templated-email to use Anymail's custom `EmailMessage` and `EmailMultiAlternatives` classes, enabling advanced features from transactional email service providers like Mailgun or SendGrid. ```Python # This replaces django.core.mail.EmailMessage TEMPLATED_EMAIL_EMAIL_MESSAGE_CLASS='anymail.message.AnymailMessage' # This replaces django.core.mail.EmailMultiAlternatives TEMPLATED_EMAIL_EMAIL_MULTIALTERNATIVES_CLASS='anymail.message.AnymailMessage' ``` -------------------------------- ### Configure Global Template Directory and File Extension for Django Templated Email Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst Shows how to globally override the default template directory and file extension for email templates using `TEMPLATED_EMAIL_TEMPLATE_DIR` and `TEMPLATED_EMAIL_FILE_EXTENSION` variables in your Django settings.py. ```Python TEMPLATED_EMAIL_TEMPLATE_DIR = 'templated_email/' #use '' for top level template dir, ensure there is a trailing slash TEMPLATED_EMAIL_FILE_EXTENSION = 'email' ``` -------------------------------- ### Configure Django Templated Email Template Directory and File Extension Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This Python snippet shows the default settings for `django-templated-email` regarding the template directory and file extension. These settings can be overridden to customize where email templates are located and their expected file type. ```Python TEMPLATED_EMAIL_TEMPLATE_DIR = 'templated_email/' #Use '' for top level template dir TEMPLATED_EMAIL_FILE_EXTENSION = 'email' ``` -------------------------------- ### Parameters for send_templated_mail Function Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This section outlines the optional parameters that can be passed to the `send_templated_mail` function to customize email sending behavior. Parameters include template path overrides, CC/BCC recipients, Django mail backend connection, and authentication details. ```APIDOC send_templated_mail( template_prefix: str = 'your_template_dir/' # Override where the method looks for email templates (alternatively, use template_dir) template_suffix: str = 'email' # Override the file extension of the email templates (alternatively, use file_extension) cc: list[str] = ['fubar@example.com'] # Set a CC on the mail bcc: list[str] = ['fubar@example.com'] # Set a BCC on the mail template_dir: str = 'your_template_dir/' # Override where the method looks for email templates connection: django.core.mail.backends.BaseEmailBackend # Takes a django mail backend connection, created using django.core.mail.get_connection auth_user: str = 'username' # Override the user that the django mail backend uses, per django.core.mail.send_mail auth_password: str = 'password' # Override the password that the django mail backend uses, per django.core.mail.send_mail ) ``` -------------------------------- ### Send Templated Emails from Django Class-Based Views Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst Shows how to integrate email sending with Django's class-based views, particularly `FormMixin` descendants, by using the `TemplatedEmailFormViewMixin`. This simplifies sending emails after form submissions, automatically providing form data or errors to the email context. ```Python from templated_email.generic_views import TemplatedEmailFormViewMixin class AuthorCreateView(TemplatedEmailFormViewMixin, CreateView): model = Author fields = ['name', 'email'] success_url = '/create_author/' template_name = 'authors/create_author.html' ``` -------------------------------- ### Retrieve Templated Email as Django EmailMessage Object Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst For more granular control, `get_templated_mail` returns a Django `EmailMessage` object, prepared with the specified template and context. This allows for further modification before sending. Parameters are similar to `send_templated_mail`. ```Python from templated_email import get_templated_mail get_templated_mail( template_name='welcome', from_email='from@example.com', to=['to@example.com'], context={ 'username':request.user.username, 'full_name':request.user.get_full_name(), 'signup_date':request.user.date_joined }, # Optional: # cc=['cc@example.com'], # bcc=['bcc@example.com'], # headers={'My-Custom-Header':'Custom Value'}, # template_prefix="my_emails/", # template_suffix="email", ) ``` -------------------------------- ### Define Email Subject and Plain Text Body in Django Template Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This template snippet shows how to define the email's subject and its plain text content using Django template blocks. The `subject` block sets the email subject, and the `plain` block defines the multi-line plain text body, both utilizing context variables. ```Django Template Language {% block subject %}My subject for {{username}}{% endblock %} {% block plain %} Hi {{full_name}}, You just signed up for my website, using: username: {{username}} join date: {{signup_date}} Thanks, you rock! {% endblock %} ``` -------------------------------- ### Define Django Templated Email Subjects in Settings Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This Python snippet demonstrates how to define email subjects directly within the Django settings file. While this method is supported for legacy purposes, the preferred approach is to use a `{% block subject %}` within the email template itself. ```Python TEMPLATED_EMAIL_DJANGO_SUBJECTS = { 'welcome':'Welcome to my website', } ``` -------------------------------- ### Define HTML Email Body in Django Template Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This template snippet demonstrates how to include an HTML part in your emails. The `html` block contains standard HTML markup, allowing for rich email formatting while still leveraging context variables for dynamic content. ```Django Template Language {% block html %}

Hi {{full_name}},

You just signed up for my website, using:

username
{{username}}
join date
{{signup_date}}

Thanks, you rock!

{% endblock %} ``` -------------------------------- ### Embed Inline Images in Django Templated Emails Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst Explains how to add inline images to emails using the `InlineImage` class. It covers reading image content from files or Django `ImageField`s, creating `InlineImage` instances, passing them in the email context, and displaying them in HTML templates. ```Python # From a file with open('pikachu.png', 'rb') as pikachu: image = pikachu.read() # From an ImageField # Suppose we have this model class Company(models.Model): logo = models.ImageField() image = company.logo.read() ``` ```Python from templated_email import InlineImage inline_image = InlineImage(filename="pikachu.png", content=image) ``` ```Python send_templated_mail(template_name='welcome', from_email='from@example.com', recipient_list=['to@example.com'], context={'pikachu_image': inline_image}) ``` ```HTML ``` -------------------------------- ### Customize Django Templated Email with View Methods Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This section outlines the methods available for overriding default email sending logic in Django class-based views. These methods allow for dynamic template selection, custom recipient lists, extended context data, and modification of email sending arguments. ```APIDOC Methods: templated_email_get_template_names(self, valid) (mandatory if you don't set templated_email_template_name): If the method returns a string it will use it as the template to render the email. If it returns a list it will send the email only with the first existing template. templated_email_get_recipients(self, form) (mandatory): Return the recipient list to whom the email will be sent to. templated_email_get_context_data(**kwargs) (optional): Use this method to add extra data to the context used for rendering the template. You should get the parent class's context from calling super. templated_email_get_send_email_kwargs(self, valid, form) (optional): Add or change the kwargs that will be used to send the e-mail. You should call super to get the default kwargs. templated_email_send_templated_mail(*args, **kwargs) (optional): This method calls django-templated-email's send_templated_mail method. You could change this method to use a celery's task for example or to handle errors. ``` -------------------------------- ### Customize Django Templated Email with View Attributes Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst This section details the configurable attributes available for Django class-based views to control email sending behavior, including template selection, success/failure conditions, and sender email address. These attributes provide quick customization without method overrides. ```APIDOC Attributes: templated_email_template_name (mandatory if you don't implement templated_email_get_template_names()): String naming the template you want to use for the email. ie: templated_email_template_name = 'welcome'. templated_email_send_on_success (default: True): This attribute tells django-templated-email to send an email if the form is valid. templated_email_send_on_failure (default: False): This attribute tells django-templated-email to send an email if the form is invalid. templated_email_from_email (default: settings.TEMPLATED_EMAIL_FROM_EMAIL): String containing the email to send the email from. ``` -------------------------------- ### Define Custom HTML to Text Conversion Function in Django Templated Email Source: https://github.com/vintasoftware/django-templated-email/blob/develop/README.rst Illustrates how to override the default HTML to plain text conversion function used by django-templated-email by assigning a custom function to `TEMPLATED_EMAIL_PLAIN_FUNCTION` in your Django settings. ```Python def convert_html_to_text(html): ... TEMPLATED_EMAIL_PLAIN_FUNCTION = convert_html_to_text ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.