from flask import Flask
from flask.ext.mail import Mail, Message
import os
app = Flask(__name__)
app.config.update(
DEBUG = True,
MAIL_SERVER = 'smtp.126.com',
MAIL_PROT = 25,
MAIL_USE_TLS = True,
MAIL_USE_SSL = False,
MAIL_USERNAME = "luxxxx@126.com",
MAIL_PASSWORD = "8xxxxlgn",
MAIL_DEBUG = True
)
mail = Mail(app)
@app.route('/')
def index():
# sender 发送方,recipients 邮件接收方列表,可以多个
msg = Message("This is a test email ",sender='luxxxx@126.com', recipients=['luxxxx@pugongying.com.cn'])
# msg.body 邮件正文
msg.body = "This is a test email"
# msg.attach 邮件附件
# msg.attach("文件名", "文件类型", 读取文件)
with app.open_resource("D:\in.jpg") as fp:
msg.attach("image.jpg", "image/jpg", fp.read())
mail.send(msg)
print ("Mail sent")
return "Sent sucess"
if __name__ == "__main__":
app.run()
flask-mail 发邮件测试程序
最新推荐文章于 2024-04-20 17:10:53 发布