flask扩展 flask-mail

2022-09-14 18:54:24 浏览数 (1)

一、邮件发送 flask-mail说明

是一个邮件发送的扩展库,使用非常方便

二、安装

pip install flask-mail

三、配置

一定要写在创建Mail对象之前,否则将不起作用

代码语言:javascript复制
<span class="hljs-comment"># 导入类库</span>
<span class="hljs-keyword">from</span> flask_mail <span class="hljs-keyword">import</span> Mail, Message

<span class="hljs-comment"># 邮箱服务器</span>
app.config[<span class="hljs-string">'MAIL_SERVER'</span>] = os.environ.get(<span class="hljs-string">'MAIL_SERVER'</span>, <span class="hljs-string">'smtp.163.com'</span>)
<span class="hljs-comment"># 用户名</span>
app.config[<span class="hljs-string">'MAIL_USERNAME'</span>] = os.environ.get(<span class="hljs-string">'MAIL_USERNAME'</span>, <span class="hljs-string">'15611833906@163.com'</span>)
<span class="hljs-comment"># 密码,密码有时是授权码</span>
app.config[<span class="hljs-string">'MAIL_PASSWORD'</span>] = os.environ.get(<span class="hljs-string">'MAIL_PASSWORD'</span>, <span class="hljs-string">'***'</span>)

<span class="hljs-comment"># 创建对象</span>
mail = Mail(app)

四、发送邮件

代码语言:javascript复制
<span class="hljs-meta">@app.route('/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># 创建邮件对象</span>
    msg = Message(subject=<span class="hljs-string">'账户激活'</span>, recipients=[<span class="hljs-string">'793390457@qq.com'</span>], 
                  sender=app.config[<span class="hljs-string">'MAIL_USERNAME'</span>])
    <span class="hljs-comment"># 浏览器打开邮件显示内容</span>
    msg.html = <span class="hljs-string">'&lt;h1&gt;你好,Mr Lucky,激活请点击右边链接&lt;/h1&gt;'</span>
    <span class="hljs-comment"># 终端接受邮件显示内容</span>
    msg.body = <span class="hljs-string">'你好,Mr Lucky,激活请点击右边链接'</span>
    
    <span class="hljs-comment"># 发送邮件</span>
    mail.send(message=msg)
    <span class="hljs-keyword">return</span> <span class="hljs-string">'邮件已发送'</span>

五、封装发送邮件函数

代码语言:javascript复制
<span class="hljs-comment"># 封装函数,发送邮件</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_mail</span><span class="hljs-params">(to, subject, template, **kwarg)</span>:</span>
    <span class="hljs-comment"># 创建邮件对象</span>
    msg = Message(subject=subject, recipients=[to], 
                  sender=app.config[<span class="hljs-string">'MAIL_USERNAME'</span>])
    <span class="hljs-comment"># 浏览器打开邮件显示内容</span>
    msg.html = render_template(template <span class="hljs-string">'.html'</span>, **kwargs)
    <span class="hljs-comment"># 终端接受邮件显示内容</span>
    msg.body = render_template(template <span class="hljs-string">'.txt'</span>, **kwargs)
    <span class="hljs-comment"># 发送邮件</span>
    mail.send(message=msg)

调用邮件发送函数:

代码语言:javascript复制
<span class="hljs-meta">@app.route('/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    send_mail(<span class="hljs-string">'793390457@qq.com'</span>, <span class="hljs-string">'找回密码'</span>, <span class="hljs-string">'activate'</span>, 
              username=<span class="hljs-string">'lucky'</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-string">'邮件已发送'</span>

六、异步发送邮件

代码语言:javascript复制
<span class="hljs-comment"># 异步发送邮件</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">async_send_mail</span><span class="hljs-params">(app, msg)</span>:</span>
    <span class="hljs-comment"># 发送邮件需要程序上下文,新的线程没有上下文,需要手动创建     </span>
    <span class="hljs-keyword">with</span> app.app_context():
        <span class="hljs-comment"># 发送邮件</span>
        mail.send(message=msg)

<span class="hljs-comment"># 封装函数,发送邮件</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_mail</span><span class="hljs-params">(to, subject, template, **kwargs)</span>:</span>
    <span class="hljs-comment"># 根据current_app获取当前的实例</span>
    app = current_app._get_current_object()
    <span class="hljs-comment"># 创建邮件对象</span>
    msg = Message(subject=subject, recipients=[to], sender=app.config[<span class="hljs-string">'MAIL_USERNAME'</span>])
    <span class="hljs-comment"># 浏览器打开邮件显示内容</span>
    msg.html = render_template(template <span class="hljs-string">'.html'</span>, **kwargs)
    <span class="hljs-comment"># 终端接受邮件显示内容</span>
    msg.body = render_template(template <span class="hljs-string">'.txt'</span>, **kwargs)
    <span class="hljs-comment"># 创建线程</span>
    thr = Thread(target=async_send_mail, args=[app, msg])
    <span class="hljs-comment"># 启动线程</span>
    thr.start()
    <span class="hljs-keyword">return</span> thr

七、扩展

环境变量:好处是可以避免隐私的信息公布于众

windows配置

代码语言:javascript复制
设置:set NAME=xiaoming
获取:set NAME

linux配置

代码语言:javascript复制
导出:export NAME=lucky
获取:echo $NAME

代码获取

代码语言:javascript复制
<span class="hljs-keyword">import</span> os
app.config[<span class="hljs-string">'SECRET_KEY'</span>] = os.environ.get(<span class="hljs-string">'SECRET_KEY'</span>) <span class="hljs-keyword">or</span> <span class="hljs-string">'123456'</span>
os.environ.get(<span class="hljs-string">'NAME'</span>, <span class="hljs-string">'default'</span>)

0 人点赞