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.
44 lines
2.0 KiB
44 lines
2.0 KiB
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):
|
|
"""发送评论相关邮件的主函数"""
|
|
site = get_current_site().domain# 获取当前站点域名
|
|
subject = _('Thanks for your comment')# 邮件标题(国际化)
|
|
article_url = f"https://{site}{comment.article.get_absolute_url()}" # 构建文章完整URL
|
|
|
|
# 1. 给评论作者发送感谢邮件
|
|
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}
|
|
tomail = comment.author.email# 收件人邮箱
|
|
send_email([tomail], subject, html_content)
|
|
|
|
# 2. 如果是回复评论,给被回复者发送通知邮件
|
|
try:
|
|
if comment.parent_comment:
|
|
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}
|
|
tomail = comment.parent_comment.author.email # 被回复者邮箱
|
|
send_email([tomail], subject, html_content)
|
|
except Exception as e:
|
|
logger.error(e)#记录邮件发送异常
|