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 = _("""
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}
# 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 %(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}
# jrx: 被回复者(父评论作者)邮箱
tomail = comment.parent_comment.author.email
# jrx: 发送回复通知邮件
send_email([tomail], subject, html_content)
except Exception as e:
# jrx: 记录邮件发送失败的错误日志
logger.error(e)