You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Django/comments-jrx/utils.py

51 lines
2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import logging
from django.utils.translation import gettext_lazy as _
from djangoblog.utils import get_current_site
from djangoblog.utils import send_email
logger = logging.getLogger(__name__)
def send_comment_email(comment):
# jrx: 发送评论相关邮件(感谢评论和回复通知)
# jrx: 获取当前站点域名
site = get_current_site().domain
# jrx: 邮件主题(感谢评论)
subject = _('Thanks for your comment')
# jrx: 构建文章访问链接
article_url = f"https://{site}{comment.article.get_absolute_url()}"
# jrx: 构建感谢评论的邮件内容HTML格式
html_content = _("""<p>Thank you very much for your comments on this site</p>
You can visit <a href="%(article_url)s" rel="bookmark">%(article_title)s</a>
to review your comments,
Thank you again!
<br />
If the link above cannot be opened, please copy this link to your browser.
%(article_url)s""") % {'article_url': article_url, 'article_title': comment.article.title}
# jrx: 评论者邮箱
tomail = comment.author.email
# jrx: 发送感谢邮件给评论者
send_email([tomail], subject, html_content)
try:
# jrx: 如果是回复评论,给被回复者发送通知邮件
if comment.parent_comment:
# jrx: 构建回复通知的邮件内容HTML格式
html_content = _("""Your comment on <a href="%(article_url)s" rel="bookmark">%(article_title)s</a><br/> has
received a reply. <br/> %(comment_body)s
<br/>
go check it out!
<br/>
If the link above cannot be opened, please copy this link to your browser.
%(article_url)s
""") % {'article_url': article_url, 'article_title': comment.article.title,
'comment_body': comment.parent_comment.body}
# jrx: 被回复者(父评论作者)邮箱
tomail = comment.parent_comment.author.email
# jrx: 发送回复通知邮件
send_email([tomail], subject, html_content)
except Exception as e:
# jrx: 记录邮件发送失败的错误日志
logger.error(e)