Django ブログサイト mails.py メール処理
2020/07/04 (更新:2020/11/19)
メール処理です。
メールの本文はメール毎テンプレートで定義します。
コメント投稿通知
コメント登録時のメールを送信します。
comment_notification
def comment_notification(request, comment):
if not comment.post.author.user.email:
return
subject = _('COMMENT_NOTIFICATION_SUBJECT').format(comment.post.author.title_text)
context = {
'comment': comment,
'url': request.build_absolute_uri(reverse('blog:detail', args=(comment.post.author.user.username, comment.post.id))),
}
message = render_to_string('blog/mails/comment_notification.txt', context, request)
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [comment.post.author.user.email]
send_mail(subject, message, from_email, recipient_list)
templates/comment_notification.txt
{% load i18n %}
{% trans 'COMMENT_NOTIFICATION_MESSAGE' %}
{{ comment.post.author.title_text }}
{{ comment.post.content.title_text }}
{{ url }}
---
{% trans 'Name' %}: {{ comment.name_text }}
{% trans 'Timestamp' %}: {{ comment.created_date|date:"Y/m/d H:i:s" }}
{{ comment.comment_text }}