|
|
|
|
@ -1,28 +1,45 @@
|
|
|
|
|
import logging
|
|
|
|
|
import logging # 导入日志模块,用于记录程序运行中的日志信息
|
|
|
|
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
from django.utils.translation import gettext_lazy as _ # 导入翻译函数,支持国际化文本
|
|
|
|
|
|
|
|
|
|
from djangoblog.utils import get_current_site
|
|
|
|
|
from djangoblog.utils import send_email
|
|
|
|
|
from djangoblog.utils import get_current_site # 从自定义工具模块导入获取当前站点域名的函数
|
|
|
|
|
from djangoblog.utils import send_email # 从自定义工具模块导入发送邮件的函数
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
logger = logging.getLogger(__name__) # 创建当前模块的日志记录器,用于记录该模块的日志
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_comment_email(comment):
|
|
|
|
|
"""
|
|
|
|
|
发送评论相关邮件:
|
|
|
|
|
1. 向评论作者发送评论成功的感谢邮件
|
|
|
|
|
2. 若当前评论是回复(有父评论),向父评论作者发送回复通知邮件
|
|
|
|
|
"""
|
|
|
|
|
# 获取当前网站的域名(用于拼接文章链接)
|
|
|
|
|
site = get_current_site().domain
|
|
|
|
|
# 邮件主题:评论感谢(支持国际化)
|
|
|
|
|
subject = _('Thanks for your comment')
|
|
|
|
|
# 拼接评论对应的文章访问链接(HTTPS协议)
|
|
|
|
|
article_url = f"https://{site}{comment.article.get_absolute_url()}"
|
|
|
|
|
# 构建给评论作者的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}
|
|
|
|
|
%(article_url)s""") % {
|
|
|
|
|
'article_url': article_url, # 文章访问链接
|
|
|
|
|
'article_title': comment.article.title # 文章标题
|
|
|
|
|
}
|
|
|
|
|
# 评论作者的邮箱(收件人)
|
|
|
|
|
tomail = comment.author.email
|
|
|
|
|
# 调用发送邮件函数,向评论作者发送感谢邮件
|
|
|
|
|
send_email([tomail], subject, html_content)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# 判断当前评论是否有父评论(即是否是回复评论)
|
|
|
|
|
if comment.parent_comment:
|
|
|
|
|
# 构建给父评论作者的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/>
|
|
|
|
|
@ -30,9 +47,16 @@ def send_comment_email(comment):
|
|
|
|
|
<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}
|
|
|
|
|
""") % {
|
|
|
|
|
'article_url': article_url, # 文章访问链接
|
|
|
|
|
'article_title': comment.article.title, # 文章标题
|
|
|
|
|
'comment_body': comment.parent_comment.body # 父评论的内容(供作者识别)
|
|
|
|
|
}
|
|
|
|
|
# 父评论作者的邮箱(收件人)
|
|
|
|
|
tomail = comment.parent_comment.author.email
|
|
|
|
|
# 调用发送邮件函数,向父评论作者发送回复通知邮件
|
|
|
|
|
send_email([tomail], subject, html_content)
|
|
|
|
|
# 捕获发送回复邮件过程中的异常(避免单个邮件发送失败影响整体流程)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(e)
|
|
|
|
|
# 记录异常日志(便于问题排查)
|
|
|
|
|
logger.error(e)
|