首先 此功能 是基于 flask框架实现邮件发送
本人实现的QQ邮件发送。
一、qq邮箱设置
1.先进入qq邮件中进行配置
二、flask 进行设置
1.按照 flask-mail这个扩展包
执行命令 pip install flask-mail
导入
from flask_mail import Mail,Message
import config 导入配置文件
实例化配置
mail = Mail() app = Flask(__name__) #引入加载配置 app.config.from_object(config) mail.init_app(app)
MAIL_DEBUG = True # 开启debug,便于调试看信息 MAIL_SUPPRESS_SEND = False # 发送邮件,为True则不发送 MAIL_SERVER = 'smtp.qq.com' # 邮箱服务器 MAIL_PORT = 465 # 端口 MAIL_USE_SSL = True # 重要,qq邮箱需要使用SSL MAIL_USE_TLS = False # 不需要使用TLS MAIL_USERNAME = '75......@qq.com' # 填邮箱 MAIL_PASSWORD = '.....' # 填授权码 FLASK_MAIL_SENDER = '皮皮虾!我们走!<752885032@qq.com>' #邮件发送方 FLASK_MAIL_SUBJECT_PREFIX = '[皮皮虾!我们走]' #邮件标题 MAIL_DEFAULT_SENDER = '752885032@qq.com' # 填邮箱,默认发送者**加粗样式**
发送内容路由
@app.route('/sendmail')
def sendmail():
message = Message(subject='hello flask-mail', recipients=['752885032@qq.com'], body='flask-mail测试代码')
try:
mail.send(message)
return '发送成功,请注意查收~'
except Exception as e:
print(e)
return '发送失败'
#发送附带附件的邮件 --压缩包图片都可以 亲测
@app.route('/email_send_attach/')
def email_send_attach():
message = Message(subject='hello flask-mail',recipients=['752885032@qq.com'],body='我是一个附件邮件')
try:
# with open(filename,'rb') as fp:
# message.attach("test.jpg", "image/jpg", fp.read())
with app.open_resource('./test.zip') as fp:
# attach("文件名", "类型", 读取文件)
message.attach("test.zip", 'application/octet-stream', fp.read())
mail.send(message)
return '发送成功,请注意查收~'
except Exception as e:
print(e)
return '发送失败'
看看邮箱效果:
文件
内容文本
图片



