美文网首页
使用SendGrid发送邮件

使用SendGrid发送邮件

作者: singed | 来源:发表于2018-08-24 16:53 被阅读0次

安装SendGrid

https://sendgrid.com申请帐号后,Settings - API Keys申请API KEY。

  1. 安装模块
pip install sendgrid
  1. 设置环境变量
setx SENDGRID_API_KEY "YOUR_API_KEY"

代码示例

import sendgrid
import os
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = Email("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

相关文章

网友评论

      本文标题:使用SendGrid发送邮件

      本文链接:https://www.haomeiwen.com/subject/mdhiiftx.html