機能補完---メール送信
6337 ワード
必要:
複数のメールアドレスに同時にメールを送信します.
app.py
send_mail.html
base.html
wtf.html
複数のメールアドレスに同時にメールを送信します.
app.py
from flask import Flask, render_template, request, redirect, url_for, flash, session
from flask_bootstrap import Bootstrap
from flask_mail import Message, Mail
from forms import RegisterForm, MailForm
users = [
{
'username': 'root',
'password': 'root'
},
]
# , , ,
app = Flask(__name__)
app.config['SECRET_KEY'] = 'westos'
bootstrap = Bootstrap(app)
#
# IP
app.config['MAIL_SERVER'] = 'smtp.qq.com'
# , 25, qq 465 587
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = '731231642'
# qq POP3/SMTP IMAP/SMTP ,
# POP3
app.config['MAIL_PASSWORD'] = "zqzjbjvkjzvpbcgd"
def send_mail(to, subject, template, **kwargs):
mail = Mail(app)
# app.app_context() : Flask app AppContext
# app
with app.app_context():
msg = Message(subject=subject,
sender='[email protected]',
recipients=to,
# body=render_template(template + '.txt', **kwargs),
html=render_template(template + '.html', **kwargs)
# , , html
)
mail.send(msg)
@app.route('/mail/', methods=['GET', 'POST'])
def mail():
#
form = MailForm()
if form.validate_on_submit():
#
toEmails = form.toEmails.data.split(',')
toFilename = form.toFilename.data
try:
send_mail(toEmails, " ", toFilename)
except Exception as e:
flash(" , : %s!" % (e))
else:
flash(" !")
return redirect(url_for('mail'))
return render_template('send_mail.html',form=form)
send_mail.html
{% extends 'base.html' %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %} {% endblock %}
{% block content %}
{{ wtf.quick_form(form) }}
{% endblock %}
base.html
{% extends 'bootstrap/base.html' %}
{% block styles %}
{# css #}
{{ super() }}
{% endblock %}
{% block navbar %}
{# #}
{% for item in get_flashed_messages() %}
{{ item }}
{% endfor %}
{% endblock %}
wtf.html
{% extends 'base.html' %}
{# , wtf#}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %} wtf {% endblock %}
{% block content %}
-wtf
{# form html #}
{{ wtf.quick_form(form) }}
{% endblock %}