跳转到主要内容

Flask扩展用于sendgrid。它具有与Flask-Mail相同的接口。

项目描述

https://badge.fury.io/py/Flask-Mail-SendGrid.svg

Flask扩展用于sendgrid。它具有与Flask-Mail相同的接口。

Flask-Mail-SendGrid与其他扩展(如Flask-Security)兼容。

安装Flask-Mail-SendGrid

使用pypi安装

$ pip install flask-mail-sendgrid

或安装最新版本

$ git clone https://github.com/hamano/flask-mail-sendgrid.git
$ cd flask-mail-sendgrid
$ python setup.py install

配置Flask-Mail-SendGrid

  • MAIL_SENDGRID_API_KEY: SendGrid的API密钥

  • MAIL_DEFAULT_SENDER: 默认发送者

from flask import Flask
from flask_mail_sendgrid import MailSendGrid

app = Flask(__name__)
app.config['MAIL_SENDGRID_API_KEY'] = 'XXXXXXXX'
mail = MailSendGrid(app)

发送消息

要发送消息,首先创建一个Message实例

from flask_mail import Message

@app.route("/")
def index():
    msg = Message("Hello",
                  sender="from@example.com",
                  recipients=["to@example.com"])

消息可以包含正文和/或HTML

msg.body = "testing"
msg.html = "<b>testing</b>"

或者,如果您使用Sendgrid的模板,可以指定模板ID和数据

msg.template_id = 'my-template-id'
msg.dynamic_template_data = {'first_name': 'John', 'last_name': 'Doe'}

最后,使用配置了您的Flask应用程序的Mail实例发送消息

mail.send(msg)

其他选项

msg = Message("Hello",
              sender="from@example.com",
              mail_options={'from_name': 'John'},
              recipients=["to@example.com"],
              cc=["to@example.com"],
              bcc=["to@example.com"])

项目详情


下载文件

下载适合您的平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源分布

Flask-Mail-SendGrid-0.6.tar.gz (3.2 kB 查看哈希值)

上传时间 来源

支持者