danirus / django-comments-xtd

A pluggable Django comments application with thread support, follow-up notifications, mail confirmation, like/dislike flags, moderation, a ReactJS plugin and Bootstrap 5.3.
https://django-comments-xtd.readthedocs.io
BSD 2-Clause "Simplified" License
594 stars 158 forks source link

How to send a custom HTML notification email instead of default plain text? #200

Closed jdbit closed 4 years ago

jdbit commented 4 years ago

I've fount this template: comment_notification_email.txt in /django_comments_xtd/templates, but how can I create a custom HTML template for notification emails?

danirus commented 4 years ago

That template is used to send emails to the moderator. It's part of django-contrib-comments. It's not used to send a multipart email with both, HTML and text versions.

If you are looking for the email templates used by django-comments-xtd, they are in the templates/django_comments_xtd directory. All the email templates are sent in the two formats at once, plain text and HTML, so it's better to customize both files.

jdbit commented 4 years ago

I've seen templates in django-comments-xtd, just wanted to change the comment_notification_email.txt template. Thank you! I'll check django-contrib-comments docs if there is a way to use html email template.

manisar2 commented 3 years ago

If someone needs the code for implementing what has been asked in this thread, here it is.

It is a simple modification of the default email function present in the CommentModerator class. Be sure to have the comment_notification_email.html template ready in your comments app's template folder. Note that I'm not using the translation module which was used in the original email function (for the subject of the email).

from django_comments.moderation import CommentModerator
from django_comments_xtd.moderation import moderator
from django.core.mail import EmailMultiAlternatives
from django.template import loader
from django.contrib.sites.shortcuts import get_current_site

class EntryModerator(CommentModerator):
    email_notification = True
    enable_field = '<enable_comment_field_in_your_model>' # boolean

    def email(self, comment, content_object, request):
        """
        Send email notification of a new comment to site staff when email
        notifications have been requested.
        """
        if not self.email_notification:
            return
        recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
        t = loader.get_template('comments/comment_notification_email.txt')
        h = loader.get_template('comments/comment_notification_email.html')
        c = {
            'comment': comment,
            'content_object': content_object,
        }
        subject = '[%(site)s] New comment posted on "%(object)s"' % {
            'site': get_current_site(request).name,
            'object': content_object,
        }
        text_message = t.render(c)
        html_content = h.render(c)
        # send_mail(subject, text_message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True) # this was the default method used in the original method

        msg = EmailMultiAlternatives(subject, text_message, settings.DEFAULT_FROM_EMAIL, recipient_list)
        msg.attach_alternative(html_content, "text/html")
        msg.send(fail_silently=False)

moderator.register(<YourModel>, EntryModerator)