当前位置:   article > 正文

[python3.6 flask web学习]使用Flask-Mail发送邮件_set mail_username

set mail_username

首先进入虚拟环境安装flask-mail扩展模块

pip install flask-mail

发送邮件需要配置发送邮件的服务器。flask-mail固定的配置变量名如下



在macos或者linux操作系统中使用export命令导入环境变量

export MAIL_USERNAME = username
export MAIL_PASSWORD = passowrd
windows操作系统

set MAIL_USERNAME = username
set MAIL_PASSWORD = passoword

同步发送电子邮件

  1. import os
  2. # ...
  3. app.config['MAIL_SERVER'] = 'smtp.googlemail.com' //stmp邮箱服务器
  4. app.config['MAIL_PORT'] = 587 //端口
  5. from flask.ext.mail import Message
  6. from flask.ext.mail import Mail
  7. #...
  8. app.config['MAIL_SUBJECT_PREFIX'] = '[prefix]' //邮件主题前缀
  9. app.config['MAIL_SENDER'] = 'Admin <test@test.test>'
  10. mail = Mail(app)
  11. def send_email(to, subject, template, **kwargs):
  12. msg = Message(app.config['MAIL_SUBJECT_PREFIX'] + subject, sender = app.config['MAIL_SENDER'], recipients=[to])
  13. msg.body = render_template(template + '.txt', **kwargs)
  14. msg.html = render_template(template + '.html', **kwargs)
  15. mail.send(msg) //调用mail实例的send函数发送邮件
  16. app.config['MAIL_USE_TLS'] = True //是否采用安全协议app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME') //从环境变量中获取用户名
  17. app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD') //从环境变量中获取密码,一般这种数据配置在环境变量中,防止隐私信息不消息被公布

使用这种方式发送邮件当然是可以的,但是如果网络或者邮件服务器有问题,导致发送邮件出问题或者缓慢,将会导致整个程序运行阻塞。因此最好的方式新开线程异步发送
新增异步发送方法

  1. from threading import Thread
  2. def send_async_email(app, msg):
  3. with app.app_context():
  4. mail.send(msg)
  5. def send_mail(to, subject, template, **kwargs):
  6. msg = Message(app.config['MAIL_SUBJECT_PREFIX'] + subject, sender = app.config['MAIL_SENDER'], recipient=[to])
  7. msg.body = render_template(template + '.txt', **kwargs)
  8. msg.html = render_template(template + '.html', **kwargs)
  9. th = Thread(target = send_async_email, args=[app, msg])
  10. th.start()
  11. return th

当然生产环境中如果有很多邮件发送会创建大量大线程,一般是发送到队列中执行。

  1. import os
  2. # ...
  3. app.config['MAIL_SERVER'] = 'smtp.googlemail.com' //stmp邮箱服务器
  4. app.config['MAIL_PORT'] = 587 //端口
  1. from flask.ext.mail import Message
  2. from flask.ext.mail import Mail
  3. #...
  4. app.config['MAIL_SUBJECT_PREFIX'] = '[prefix]' //邮件主题前缀
  5. app.config['MAIL_SENDER'] = 'Admin <test@test.test>'
  6. mail = Mail(app)
  7. def send_email(to, subject, template, **kwargs):
  8. msg = Message(app.config['MAIL_SUBJECT_PREFIX'] + subject, sender = app.config['MAIL_SENDER'], recipients=[to])
  9. msg.body = render_template(template + '.txt', **kwargs)
  10. msg.html = render_template(template + '.html', **kwargs)
  11. mail.send(msg) //调用mail实例的send函数发送邮件

app.config['MAIL_USE_TLS'] = True //是否采用安全协议app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME') //从环境变量中获取用户名app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD') //从环境变量中获取密码,一般这种数据配置在环境变量中,防止隐私信息不消息被公布


  
  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/499316
推荐阅读
相关标签
  

闽ICP备14008679号