flask邮件发送

Posted longyunfeigu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask邮件发送相关的知识,希望对你有一定的参考价值。

安装flask-mail

pip3 install flask-mail

开启smtp服务

技术分享图片

发送邮件

from flask import Flask, render_template
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 = '[email protected]',
    MAIL_PASSWORD = 'xxxxxxxxxxx',
    MAIL_DEBUG = True
)

mail = Mail(app)

def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender, recipients)
    msg.body = text_body
    msg.html = html_body
    mail.send(msg)


@app.route('/')
def index():
# sender 发送方哈,recipients 邮件接收方列表
    msg = Message("Hi!This is a test ",sender='[email protected]', recipients=['[email protected]'])
# msg.body 邮件正文
    msg.body = 'wocaonima'
    msg.html = "<a href='http://www.baidu.com'>This is a first email1111</a>"
# msg.attach 邮件附件添加

    mail.send(msg)
    return "Sent"

if __name__ == "__main__":
    app.run()

发送附件

from flask import Flask, render_template
from flask.ext.mail import Mail, Message

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 = '[email protected]',
    MAIL_PASSWORD = 'sishen44448888',
    MAIL_DEBUG = True
)

mail = Mail(app)

def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender, recipients)
    msg.body = text_body
    msg.html = html_body
    mail.send(msg)


@app.route('/')
def index():
# sender 发送方哈,recipients 邮件接收方列表
    msg = Message("Hi!This is a test ",sender='[email protected]', recipients=['[email protected]'])
# msg.attach 邮件附件添加
# msg.attach("文件名", "类型", 读取文件)
    with app.open_resource("./stu.png") as fp:
        msg.attach("image.jpg", "image/jpg", fp.read())

    mail.send(msg)
    return "Sent"

if __name__ == "__main__":
    app.run()

异步发送邮件

from flask import Flask, render_template
from flask.ext.mail import Mail, Message
from threading import Thread

def async(f):
    def wrapper(*args, **kwargs):
        thr = Thread(target = f, args = args, kwargs = kwargs)
        thr.start()
    return wrapper

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 = '[email protected]',
    MAIL_PASSWORD = 'sishen44448888',
    MAIL_DEBUG = True
)
import time
mail = Mail(app)

@async
def send_async_email(msg):
    time.sleep(10)
    with app.app_context():
        mail.send(msg)

def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender = sender, recipients = recipients)
    msg.body = text_body
    msg.html = html_body
    send_async_email(msg)

@app.route('/')
def index():
    send_email("This is a test", sender='[email protected]', recipients=['[email protected]'],
               text_body='nihao', html_body="<a href='http://www.baidu.com'>This is a first email</a>")
    return "Sent"

if __name__ == "__main__":
    app.run()

以上是关于flask邮件发送的主要内容,如果未能解决你的问题,请参考以下文章

使用内嵌图像Flask-Mail发送电子邮件?

使用flask-mail通过gmail发送电子邮件

Flask 邮件发送

flask 邮件发送

Flask发送注册邮件

AttributeError:尝试在 Flask 中发送邮件时,“NoneType”对象没有属性“app”[重复]