该库允许您通过 Python 快速轻松地使用 SendGrid Web API v3。
该库的 3.X.X+ 版本为所有 SendGrid Web API v3 端点提供全面支持,包括新的 v3 /mail/send。
该库代表了 SendGrid 新路径的开始。我们希望这个库由社区驱动并由 SendGrid 主导。我们需要您的帮助来实现这一目标。为了帮助确保我们以正确的顺序构建正确的内容,我们要求您创建问题和拉取请求,或者只是对现有问题或拉取请求进行投票或评论。
如果您需要使用 SendGrid 的帮助,请查看 Twilio SendGrid 支持帮助中心。
请浏览本自述文件的其余部分以获取更多详细信息。
使用您的 SENDGRID_API_KEY 更新开发环境(更多信息请参见此处),例如:
echo " export SENDGRID_API_KEY='YOUR_API_KEY' " > sendgrid.env
echo " sendgrid.env " >> .gitignore
source ./sendgrid.env
SendGrid 还支持本地环境文件.env
。将.env_sample
复制或重命名为.env
并使用您的密钥更新 SENDGRID_API_KEY。
临时设置环境变量(仅在当前 cli 会话期间可访问):
set SENDGRID_API_KEY=YOUR_API_KEY
永久设置环境变量(可在所有后续 cli 会话中访问):
setx SENDGRID_API_KEY " YOUR_API_KEY "
pip install sendgrid
以下是使用 /mail/send Helper 发送电子邮件所需的最少代码(这是一个完整的示例):
import sendgrid
import os
from sendgrid . helpers . mail import *
sg = sendgrid . SendGridAPIClient ( api_key = os . environ . get ( 'SENDGRID_API_KEY' ))
from_email = Email ( "[email protected]" )
to_email = To ( "[email protected]" )
subject = "Sending with SendGrid is Fun"
content = Content ( "text/plain" , "and easy to do anywhere, even with Python" )
mail = Mail ( from_email , to_email , subject , content )
response = sg . client . mail . send . post ( request_body = mail . get ())
print ( response . status_code )
print ( response . body )
print ( response . headers )
Mail
构造函数为您创建一个个性化对象。这是如何添加它的示例。
以下是在没有 /mail/send Helper 的情况下发送电子邮件所需的最少代码(这是一个完整的示例):
import sendgrid
import os
sg = sendgrid . SendGridAPIClient ( api_key = os . environ . get ( 'SENDGRID_API_KEY' ))
data = {
"personalizations" : [
{
"to" : [
{
"email" : "[email protected]"
}
],
"subject" : "Sending with SendGrid is Fun"
}
],
"from" : {
"email" : "[email protected]"
},
"content" : [
{
"type" : "text/plain" ,
"value" : "and easy to do anywhere, even with Python"
}
]
}
response = sg . client . mail . send . post ( request_body = data )
print ( response . status_code )
print ( response . body )
print ( response . headers )
import sendgrid
import os
sg = sendgrid . SendGridAPIClient ( api_key = os . environ . get ( 'SENDGRID_API_KEY' ))
response = sg . client . suppression . bounces . get ()
print ( response . status_code )
print ( response . body )
print ( response . headers )
import sendgrid
import os
sg = sendgrid . SendGridAPIClient ( api_key = os . environ . get ( 'SENDGRID_API_KEY' ))
response = sg . client . _ ( "suppression/bounces" ). get ()
print ( response . status_code )
print ( response . body )
print ( response . headers )
请参阅我们的帮助程序以了解如何使用我们的入站解析 webhook。
常见 API 用例的示例,例如如何使用事务模板发送电子邮件。
该库的所有更新都记录在我们的变更日志和版本中。
我们鼓励为我们的图书馆做出贡献(您甚至可能会获得一些漂亮的礼物),请参阅我们的贡献指南了解详细信息。
快速链接:
请参阅我们的常见库问题故障排除指南。
sendgrid-python 由 Twilio SendGrid, Inc. 维护和资助。sendgrid-python 的名称和徽标是 Twilio SendGrid, Inc. 的商标。
如果您需要支持,请查看 Twilio SendGrid 支持帮助中心。
麻省理工学院许可证 (MIT)