python flask发送邮件
来源:互联网 发布:淘宝直播申请入口关闭 编辑:程序博客网 时间:2024/06/06 04:39
python flask发送邮件
* SMTPAuthenticationError: (535, ‘Error: authentication failed’)
解决办法:调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授权码替代上面代码部分的passwd即可成功发送邮件
安装flask-mail
pip3 install flask-mail
总体结构图
hello.py
import osfrom flask import Flask, render_template, session, redirect, url_forfrom flask_script import Manager, Shellfrom flask_bootstrap import Bootstrapfrom flask_moment import Momentfrom flask_wtf import Formfrom wtforms import StringField, SubmitFieldfrom wtforms.validators import Requiredfrom flask_mail import Mail, Messageapp = Flask(__name__)app.config['SECRET_KEY'] = 'hard to guess string'app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = Trueapp.config['MAIL_SERVER'] = 'smtp.163.com' # 163smtp服务器app.config['MAIL_PORT'] = 25 # 端口号app.config['MAIL_USERNAME'] = "aaa@163.com"app.config['MAIL_PASSWORD'] = "abcabc" #填写客户端授权码app.config['FLASKY_MAIL_SUBJECT_PREFIX'] = '[Flasky]'app.config['FLASKY_MAIL_SENDER'] = 'aaa@163.com'app.config['FLASKY_ADMIN'] = "aaa@163.com"manager = Manager(app)bootstrap = Bootstrap(app)moment = Moment(app)mail = Mail(app)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)class NameForm(Form): name = StringField('What is your name?', validators=[Required()]) submit = SubmitField('Submit')def make_shell_context(): return dict(app=app)manager.add_command("shell", Shell(make_context=make_shell_context))@app.errorhandler(404)def page_not_found(e): return render_template('404.html'), 404@app.errorhandler(500)def internal_server_error(e): return render_template('500.html'), 500@app.route('/', methods=['GET', 'POST'])def index(): form = NameForm() if form.validate_on_submit(): user = form.name.data session['known'] = True if app.config['FLASKY_ADMIN']: send_email([app.config['FLASKY_ADMIN'], 'abc@qq.com'], 'New User', 'mail/new_user', user=user) session['name'] = form.name.data return redirect(url_for('index')) return render_template('index.html', form=form, name=session.get('name'), known=session.get('known', False))if __name__ == '__main__': manager.run()
404.html
{% extends "base.html" %}{% block title %}Flasky - Page Not Found{% endblock %}{% block page_content %}<div class="page-header"> <h1>Not Found</h1></div>{% endblock %}
500.html
{% extends "base.html" %}{% block title %}Flasky - Internal Server Error{% endblock %}{% block page_content %}<div class="page-header"> <h1>Internal Server Error</h1></div>{% endblock %}
index.html
{% extends "base.html" %}{% import "bootstrap/wtf.html" as wtf %}{% block title %}Flasky{% endblock %}{% block page_content %}<div class="page-header"> <h1>Hello, {% if name %}{{ name }}{% else %}Stranger{% endif %}!</h1> {% if not known %} <p>Pleased to meet you!</p> {% else %} <p>Happy to see you again!</p> {% endif %}</div>{{ wtf.quick_form(form) }}{% endblock %}
base.html
{% extends "bootstrap/base.html" %}{% block title %}Flasky{% endblock %}{% block head %}{{ super() }}<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon"><link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">{% endblock %}{% block navbar %}<div class="navbar navbar-inverse" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="/">Flasky</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a href="/">Home</a></li> </ul> </div> </div></div>{% endblock %}{% block content %}<div class="container"> {% for message in get_flashed_messages() %} <div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert">×</button> {{ message }} </div> {% endfor %} {% block page_content %}{% endblock %}</div>{% endblock %}{% block scripts %}{{ super() }}{{ moment.include_moment() }}{% endblock %}
异步发送电子邮件
from threading import Threaddef send_async_email(app,msg): with app.app_context(): mail.send(msg)def send_email(to,subject,template, **kwargs): msg = Message(app.config['FLASK_MAIL_SUBJECT_PREFIX']+subject, sender=app.config['FLASK_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
注意:很多Flask扩展都假设已经存在激活的程序上下文和请求上下文。Flask-Mail中的send()函数使用current_app,因此必须激活程序上下文。不过,在不同线程中执行mail.send()函数时,程序上下文要使用app.app_context()人工创建。
0 0
- python Flask发送邮件
- python flask发送邮件
- python flask发送邮件嵌入图片 Outlook
- 使用Python Flask-mail发送邮件
- flask 异步发送邮件
- Python学习之Flask-Mail异步邮件发送
- Flask-Mail邮件发送问题
- 使用Flask-Mail发送邮件
- 使用 flask-mail 扩展发送邮件
- flask-mai发送邮件503,553错误
- Flask Web 开发 发送异步邮件
- 用Flask-mail发送邮件失败解决方案
- 使用Flask-Mail扩展发送邮件
- 使用Flask-Mail发送QQ邮件
- [python3.6 flask web学习]使用Flask-Mail发送邮件
- flask:使用flask-mail客户端访问触发发送邮件
- 用Python发送邮件
- Python 发送邮件
- 怎么写存储过程
- js ==与===区别(两个等号与三个等号)
- ElasticSearch性能优化
- 欢迎使用CSDN-markdown编辑器
- (HR2016.2.21 101 Hack 34) Minimum Penalty Path (状压dp)
- python flask发送邮件
- turtlebot刷机
- redis的5种数据结构的使用场景
- UML---序列图和协作图
- caffe安装+Ubuntu16.04+三显卡gpu加速
- C++ pair
- 常用Linux命令
- shell编程之grep命令
- 这样的孩子,送台三星吧