flask实战-个人博客-电子邮件支持

电子邮件支持

因为博客要支持评论,所以我们需要在文章有了新评论后发邮件通知管理员。而且,当管理员回复了读者的评论后,也需要发送邮件提醒读者。

为了方便读者使用示例程序,personalBlog中仍然使用Flask-Mail来发送邮件。读者在运行程序前需要在项目根目录内创建.env文件写入对应的环境变量,以便让发信功能正常工作。

因为邮件的内容很简单,我们将直接在发信函数中写出正文内容,这里只提供了HTML正文。我们有两个需要使用电子邮件的场景:

1、当文章有新评论时,发送邮件给管理员;

2、当某个评论被回复时,发送邮件给被回复用户。

为了方便使用,我们在emails.py中分别为这两个使用场景创建了特定的发信函数,可以直接在视图函数中调用 。这些函数内部则通过我们创建的通用发信函数send_email()来发送邮件,如下所示 :

personalBlog/emails.py: 提醒邮件函数

from flask import url_for

def send_mail(subject, to, html):
    pass

def send_new_comment_email(post):
# blog.show_post是blog蓝本下的show_post视图函数
    post_url = url_for('blog.show_post', post_id = post.id, _external = True) + '#comments'
    send_mail(subject = 'New comment', to = current_app.config['BLUEBLOG_ADMIN_EMAIL'],
              html = '<p>New comment in post <i>%s</i>, click the link below to check:</p>'
                     '<p><a href="%s">%s</a></p>'
                     '<p><small style = "color:#868e96">Do not reply this email.</small></p>'
                     % (post.title, post_url, post_url))

def send_new_reply_email(comment):
    post_url = url_for('blog.show_post', post_id = comment.post_id, _external = True) + '#comments'
    send_mail(subject = 'New reply', to = comment.email,
              html = '<p>New reply for the comment you left in post <i>%s</i>, click the link below to check: </p>'
                     '<p><a href="%s">%s</a></p>'
                     '<p><small style="color: #868e96">Do not reply this email.</small></p>'
                     % (comment.post.title, post_url, post_url))
 

send_new_comment_email()函数用来发送新评论提醒邮件。 我们通过将url_for()函数的_external参数设为True来构建外部链接。链接尾部的#comments是用来跳转到页面评论部分的URL片段(URL fragment),comments是评论部分div元素的id值。这个函数接收表示文章的post对象作为参数,从而生成文章正文的标题和链接。

URL片段又称片段标识符(fragment identifier),是URL中用来表示页面中资源位置的短字符,以#开头,对于HTML页面来说,一个典型的示例是文章页面的评论区。假设评论区的div元素id为comment,如果我们访问http://example.com/post/7#comment,页面加载完成后将会直接跳到评论部分。

send_new_reply_email()函数则用来发送新回复提醒邮件。这个发信函数接收comment对象作为参数,用来构建邮件正文,所属文章的主键值通过comment.post_id属性获取,标题则通过comment.post.titlle属性获取。

在personalBlog源码中,我们没有使用异步的方式发送邮件,如果你希望编写一个异步发送邮件的通用发信函数send_mail(),和之前介绍的基本相同,如下所示

from threading import Thread
from flask import current_app
from flask_mail import Message

from personalBlog.extensions import mail

def _send_async_mail(app, message):
    with app.app_context():
        mail.send(message)

def send_async_mail(subjecct, to, html):
    app = current_app._get_current_object() # 获取被代理的真实对象
    message = Message(subjecct, recipients=[to], html = html)
    thr = Thread(target = _send_async_mail, args = [app, message])
    thr.start()
    return thr

需要注意,因为我们的程序是通过工厂函数创建的,所以实例化Thread类时,我们使用代理对象current_app作为args参数列表中app的值。另外,因为在新建线程时需要真正的程序对象来创建上下文,所以不能直接传入current_app,而是传入current_app调用_get_current_app()方法获取的被代理的程序实例。

猜你喜欢

转载自www.cnblogs.com/xiaxiaoxu/p/10847070.html