Skip to content

Commit

Permalink
Add support for multi-part e-mail messages
Browse files Browse the repository at this point in the history
  • Loading branch information
grahammcculloch committed Sep 29, 2018
1 parent 98dc23b commit fe09200
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/reference/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The default settings are:
FLUENT_COMMENTS_MODERATE_BAD_WORDS = ()
FLUENT_COMMENTS_MODERATE_AFTER_DAYS = None
FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION = True
FLUENT_COMMENTS_MULTIPART_EMAILS = False
# Form layouts
FLUENT_COMMENTS_FIELD_ORDER = ()
Expand Down
9 changes: 8 additions & 1 deletion docs/topics/email.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ This feature can be enabled or disabled using:
FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION = False
The template ``comments/comment_notification_email.txt`` is used to generate the e-mail message.
By default, plain-text e-mail messages are generated using the template ``comments/comment_notification_email.txt``.

Multi-part (HTML) e-mails are supported using the template ``comments/comment_notification_email.html``. To enabled
multi-part e-mails, set:

.. code-block:: python
FLUENT_COMMENTS_MULTIPART_EMAILS = True
In addition to the standard django-comments_ package, the ``request`` and ``site`` fields
are available in the template context data. This allows generating absolute URLs to the site.
Expand Down
1 change: 1 addition & 0 deletions fluent_comments/appsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FLUENT_CONTENTS_USE_AKISMET = getattr(settings, 'FLUENT_CONTENTS_USE_AKISMET', bool(AKISMET_API_KEY)) # enable when an API key is set.
FLUENT_COMMENTS_DEFAULT_MODERATOR = getattr(settings, 'FLUENT_COMMENTS_DEFAULT_MODERATOR', 'default')
FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION = getattr(settings, 'FLUENT_COMMENTS_USE_EMAIL_NOTIFICATION', True) # enable by default
FLUENT_COMMENTS_MULTIPART_EMAILS = getattr(settings, 'FLUENT_COMMENTS_MULTIPART_EMAILS', False) # disable by default
FLUENT_COMMENTS_CLOSE_AFTER_DAYS = getattr(settings, 'FLUENT_COMMENTS_CLOSE_AFTER_DAYS', None)
FLUENT_COMMENTS_MODERATE_BAD_WORDS = getattr(settings, 'FLUENT_COMMENTS_MODERATE_BAD_WORDS', ())
FLUENT_COMMENTS_MODERATE_AFTER_DAYS = getattr(settings, 'FLUENT_COMMENTS_MODERATE_AFTER_DAYS', None)
Expand Down
11 changes: 9 additions & 2 deletions fluent_comments/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.encoding import force_text
from fluent_comments import appsettings


def send_comment_posted(comment, request):
Expand Down Expand Up @@ -30,6 +31,12 @@ def send_comment_posted(comment, request):
'comment': comment,
'content_object': content_object
}

def render_message(template): return render_to_string(template, context, request=request)

message = render_to_string("comments/comment_notification_email.txt", context, request=request)
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipient_list, fail_silently=True)
message = render_message("comments/comment_notification_email.txt")
html_message = render_message(
"comments/comment_notification_email.html") if appsettings.FLUENT_COMMENTS_MULTIPART_EMAILS else None

send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
recipient_list, fail_silently=True, html_message=html_message)
51 changes: 51 additions & 0 deletions fluent_comments/templates/comments/comment_notification_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{% autoescape off %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">

<title>{{ site }}: New comment</title>

<style type="text/css">
span.preheader { display: none !important; }
</style>
</head>

<body>
<span class="preheader">Posted by: {{ comment.user_name|default:comment.user }}</span>

<div>
A new comment has been posted on your site "{{ site }}", to the page entitled
<a target="_blank" href="{{ request.scheme }}://{{ site.domain }}{{ content_object.get_absolute_url }}">{{ content_object }}</a>.
</div>

<br>

{% if comment.title %}
<div>Title: <strong>{{ comment.title }}</strong></div>
{% endif %}
<div>Name: <strong>{{ comment.user_name|default:comment.user }}</strong></div>
<div>Email: <strong>{{ comment.user_email }}</strong></div>
<div>Homepage: <strong>{{ comment.user_url }}</strong></div>
<div>Moderated: <strong>{{ comment.is_public|yesno:'no,yes' }}</strong></div>

<div>
Comment:<br>
<strong>{{ comment.comment }}</strong>
</div>

<hr>

<div>You have the following options available:</div>
<ul>
<li><a target="_blank" href="{{ request.scheme }}://{{ site.domain }}{{ comment.get_absolute_url }}">View comment</a></li>
<li><a target="_blank" href="{{ request.scheme }}://{{ site.domain }}{% url 'comments-flag' comment.pk %}">Flag comment</a></li>
<li><a target="_blank" href="{{ request.scheme }}://{{ site.domain }}{% url 'comments-delete' comment.pk %}">Delete comment</a></li>
</ul>

</body>
</html>
{% endautoescape %}

0 comments on commit fe09200

Please sign in to comment.