Flask-mail包装了python标准库中smtplib的扩展能更好的和Flask集成。
-
#安装flask_mail
-
pip install flask_mail
-
export MAIL_USERNAME='mail username'
-
export MAIL_PASSWORD='mail password'
-
#!/usr/bin/env python
-
#-*- coding:utf-8 -*-
-
from flask_mail import Mail, Message
-
from flask import Flask
-
import os
-
#
-
app = Flask(__name__)
-
#定义邮箱地址文件
-
mail_file = file('/root/mail.txt')
-
#读取文件中所有的邮箱地址
-
mail_add = mail_file.read()
-
#将邮箱地址去掉换行符添加的列表当中
-
mail_list = mail_add.split('\n')
-
#添加Flask-mail smtp的配置
-
#这里使用126的smtp服务器
-
app.config['MAIL_SERVER'] = 'smtp.126.com'
-
app.config['MAIL_PORT'] = 25
-
app.config['MAIL_USER_TLS'] = True
-
#通过export把环境变量MAIL_USERNAME添加到env中,这里os.environ.get获取环境变量中的变量。
-
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
-
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
-
app.config['MAIL_DEBUG'] = True
-
-
mail = Mail(app)
-
-
msg = Message("hello,perfect world!",sender=''MAIL_USERNAME'',recipients=mail_list)
-
msg.body = 'hello perfect world'
-
msg.html = 'HTML body'
-
-
with app.app_context():
-
mail.send(msg)
-
阅读(1092) | 评论(0) | 转发(0) |