电子邮件

使用Flask-Mail提供电子邮件支持

pip install flask-mail

    Flask-Mail连接到简单邮件传输协议(Simple Mail Transfer Protocol,SMTP)服务器,并把邮件交给这个服务器发送。如果不进行配置,Flask-Mail会连接localhost上的端口25,无需验证即可发送电子邮件。

示例6-1 hello.py:配置Flask-Mail使用Gmail

import os
# ...
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')

示例6-2 hello.py:初始化Flask-Mail

from flask.ext.mail import Mail
mail = Mail(app)

保存电子邮件服务器用户名和密码的两个环境变量要在环境中定义。在linux或者Mac OS X中使用bash,可以按照下面的方式设定这两个变量。

(venv) $ export MAIL_USERNAME=<Gmail username>
(venv) $ export MAIL_PASSWORD=<Gmail password>

微软Windows用户可以安装下面的方式设定环境变量:

(venv) $ set MAIL_USERNAME=<Gmail username>
(venv) $ set MAIL_PASSWORD=<Gmail password>

在Python shell中发送电子邮件

(venv) $ python hello.py shell
>>> from flask.ext.mail import Message
>>> from hello import mail
>>> msg = Message('test subject', sender='you@example.com',
... recipients=['you@example.com'])
>>> msg.body = 'text body'
>>> msg.html = '<b>HTML</b> body'
>>> with app.app_context():
... mail.send(msg)
...

完整案例:使用126邮箱发送邮件:

#coding:utf-8
from flask import Flask
import os
from flask_script import Shell,Manager
from flask_mail import Mail
app=Flask(__name__)
manager=Manager(app)

app.config['MAIL_SERVER']='smtp.126.com'
app.config['MAIL_PORT']=25
app.config['MAIL_USE_TLS']=False
app.config['MAIL_USE_SSL']=False
app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD']=os.environ.get('MAIL_PASSWORD') # 邮箱客户端授权码,不是有限密码

mail=Mail(app)

def make_shell_context():
  return dict(app=app)
manager.add_command("shell",Shell(make_context=make_shell_context))

if __name__ == '__main__':
  manager.run()
环境变量也可直接编辑/etc/profile文件:
export MAIL_USERNAME=你的发送邮箱
export MAIL_PASSWORD=你的客户端授权码

编辑完成后:source /etc/profile

发送邮件:

(venv) [root@localhost ch6]# python hello.py shell
>>> from flask_mail import Message
>>> from hello import mail
>>> msg=Message('test subject',sender='xyshiyongchao@126.com',recipients=['15529343201@163.com'])
>>> msg.body='text body'
>>> msg.html='<b>HTML</b> body'
>>> with app.app_context():
...     mail.send(msg)

在程序中集成发送电子邮件功能

示例6-3 hello.py:电子邮件支持

from flask.ext.mail import Message
app.config['FLASKY_MAIL_SUBJECT_PREFIX'] = '[Flasky]'
app.config['FLASKY_MAIL_SENDER'] = 'Flasky Admin <flasky@example.com>'
def send_email(to, subject, template, **kwargs):
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    mail.send(msg)

示例6-4 hello.py:电子邮件示例


# ...
app.config['FLASKY_ADMIN'] = os.environ.get('FLASKY_ADMIN')
# ...
@app.route('/', methods=['GET', 'POST'])
def index():
  form = NameForm()
  if form.validate_on_submit():
    user = User.query.filter_by(username=form.name.data).first()
    if user is None:
      user = User(username=form.name.data)
      db.session.add(user)
      session['known'] = False
      if app.config['FLASKY_ADMIN']:
        send_email(app.config['FLASKY_ADMIN'], 'New User','mail/new_user', user=user)
    else:
      session['known'] = True
    session['name'] = form.name.data
    form.name.data = ''
    return redirect(url_for('index'))
  return render_template('index.html', form=form, name=session.get('name'),
known=session.get('known', False)) 


    电子邮件的收件人保存在环境变量FLASKY_ADMIN中,程序启动过程中,它会加载到一个同名配置变量中。我们要创建两个模板文件,分别用于渲染纯文本和HTML版本的邮件正文。这两个模板文件都保存在templates文件夹下的mail子文件夹中,以便和普通模板区分开来。电子邮件的模板中要有一个模板参数是用户,因此调用send_mail()函数时要以关键字参数的形式传入用户。

# For Linux and Mac OS X users, the command to start the application is:
(venv) $ export FLASKY_ADMIN=<your-email-address>
# For Microsoft Windows users, this is the equivalent command:
(venv) $ set FLASKY_ADMIN=<Gmail username>

异步发送电子邮件

示例6-5 hello.py:异步发送电子邮件

from threading import Thread
def send_async_email(app, msg):
  with app.app_context():
    mail.send(msg)
def send_email(to, subject, template, **kwargs):
  msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + subject,sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
  msg.body = render_template(template + '.txt', **kwargs)
  msg.html = render_template(template + '.html', **kwargs)
  thr = Thread(target=send_async_email, args=[app, msg])
  thr.start()
  return thr





















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值