Flask-Mail
说明
安装
文档
使用
from flask_mail import Mail, Message
import os
app.config['MAIL_SERVER'] = 'smtp.1000phone.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'lijie@1000phone.com'
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD', '123456')
mail = Mail(app)
@app.route('/send/')
def send():
msg = Message(
subject='账户激活',
recipients=['1415532462@qq.com'],
html='<h2>恭喜你,中奖了!</h2>',
sender=app.config['MAIL_USERNAME']
)
mail.send(msg)
return '邮件已发送'
def send_mail(subject, to, template, *args, **kwargs):
if isinstance(to, list):
recipients = to
elif isinstance(to, str):
recipients = to.split(',')
else:
raise Exception('邮件接收者格式有误')
html = render_template(template, *args, **kwargs)
msg = Message(
subject=subject,
recipients=recipients,
html=html,
sender=app.config['MAIL_USERNAME']
)
mail.send(msg)
def async_send_mail(app, msg):
with app.app_context():
mail.send(msg)
def send_mail(subject, to, template, *args, **kwargs):
if isinstance(to, list):
recipients = to
elif isinstance(to, str):
recipients = to.split(',')
else:
raise Exception('邮件接收者格式有误')
html = render_template(template, *args, **kwargs)
msg = Message(
subject=subject,
recipients=recipients,
html=html,
sender=app.config['MAIL_USERNAME']
)
thr = Thread(target=async_send_mail, args=(app, msg))
thr.start()
return thr
推荐