import logging
from django.utils.translation import gettext_lazy as _
from djangoblog.utils import get_current_site
from djangoblog.utils import send_email
# zr 获取当前模块的日志记录器
logger = logging.getLogger(__name__)
# zr 发送评论邮件功能
def send_comment_email(comment):
# zr 获取当前站点域名
site = get_current_site().domain
# zr 设置邮件主题
subject = _('Thanks for your comment')
# zr 构建文章完整URL
article_url = f"https://{site}{comment.article.get_absolute_url()}"
# zr 构建给评论作者的邮件内容
html_content = _("""
Thank you very much for your comments on this site
You can visit %(article_title)s
to review your comments,
Thank you again!
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}
# zr 获取评论作者邮箱并发送邮件
tomail = comment.author.email
send_email([tomail], subject, html_content)
# zr 如果是回复评论,同时发送邮件给被回复的评论作者
try:
if comment.parent_comment:
# zr 构建回复通知邮件内容
html_content = _("""Your comment on %(article_title)s
has
received a reply.
%(comment_body)s
go check it out!
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}
# zr 获取被回复评论作者的邮箱并发送通知
tomail = comment.parent_comment.author.email
send_email([tomail], subject, html_content)
except Exception as e:
# zr 记录邮件发送异常
logger.error(e)