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()

原文地址:https://www.cnblogs.com/longyunfeigu/p/9284777.html

时间: 2024-10-16 12:01:26

flask邮件发送的相关文章

Flask 邮件发送

欢迎关注小婷儿的博客: csdn:https://blog.csdn.net/u010986753 博客园:http://www.cnblogs.com/xxtalhr/ 有问题请在博客下留言或加QQ群:483766429 或联系作者本人 QQ :87605025 OCP培训说明连接:https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16HkiuA OCM培训说明连接:https://mp.weixin.qq.com/s/7-R6Cz8RcJKduVv6YlAx

关于Flask 邮件发送,支持多人发送

#!/usr/bin/python# -*- coding: UTF-8 -*-import smtplibfrom email.mime.text import MIMETextfrom email.header import Header class SendEmail(object):    def __init__(self, mailserver, username_send, password, port, server_type):        self.mailserver =

文件上传与邮件发送

# 文件上传与邮件发送 ### 原生实现 - 模板文件 ```html <form method="post" enctype="multipart/form-data"> <input type="file" name="photo" /><br /> <input type="submit" value="上传" /> </for

学习笔记之邮件发送篇

用脚本语言发送邮件是系统管理员必备技能 对系统定期检查或者当服务器受到攻击时生成文档和报表. 发布这些文档最快速有效的方法就是发送邮件. python中email模块使得处理邮件变得比较简单 发送邮件主要用到了smtplib和email两个模块,这里首先就两个模块进行一下简单的介绍: 本段摘录于    http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html 1.smtplib模块 smtplib.SMTP([host[, p

java-基于JavaMail的Java邮件发送

1.基于JavaMail的Java邮件发送:简单邮件发送 2.基于JavaMail的Java邮件发送:复杂邮件发送

更改邮件发送语言为英语,解决编码为UTF8邮箱注册账号,邮件内容乱码问题

Change email English language, code for UTF8 mailbox registered account, email content garbled. 1. code analysis 乱码分析 通过对中文编码的邮件服务器使用原来的系统(GB2312) The original system used by the mail server encoding for the Chinese code (GB2312) 我使用outlook.com的邮箱(UT

redmine邮件发送功能配置详解

redmine的邮件发送功能还是很有用的. 像项目有更新啦,任务分配啦,都能邮件发送的相关责任人. 我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了. 查了网上的资料,都是讲修改下配置文件就可以了,他们没错,只是没有讲全. 下面是我整理的一个redmine邮件发送功能设置的一个完整流程. 1. sendmail安装与检查 linux机器上安装的redmine要能发送邮件,先得是本机的sendmail功能是正常的. 查看sendmail进程是否已正常启动: $ ps au

自动化邮件报告平台-邮件发送highchart图表

前段时间参与开发这样的一个系统,负责前端设计开发,使用人员提出需要在邮件发送的时候自动获取这些highchart图表数据,并显示在平台页面上,当发送邮件的时候也把图表附带在邮件中. highchart是一个比较强大的图表组件,这个图表组件以svg方式渲染在网页上,渲染完毕后会在网页中添加了svg元素,可以通过dom 或者jQuery 把svg内容单独抽取出来,此svg元素也能够在网页上直接显示,如下图所示.  但是,在邮箱环境下,这些svg元素不一定能展示在邮件里面,各种邮箱环境不同,在手机端邮

JavaMail 邮件发送之使用qq邮箱

所需jar包:comment-email.jar     mail.jaractivation.jar 一. 配置QQ邮箱的IMAP 进入qq电子邮件点击 设置->账户里开启 SMTP 服务(开启IMAP/SMTP服务)   注意:在启用QQ邮箱的14天之后才能开启此服务 开启之后会得到授权码,此授权码要记住或者保存到文本文件当中发送邮件的时候需要作为验证密码使用. 二.使用JavaMail发送一封简单邮件 的示例代码: public static void main(String[] args