用于python的twilio sendgrid库
sendgrid的Python项目详细描述
此库允许您通过python快速方便地使用twilio sendgrid web api v3。
新建:
- 订阅电子邮件notifications以获取发布和中断更改。
- 版本6.x的发布是版本5.x的一个突破性的变化,请参见release notes了解详细信息。
- 快速开始使用Docker。
- 用Twilio发送短消息。
这个库为所有twilio sendgridWeb API v3端点提供完全支持,包括v3 /mail/send。
我们希望这个库是社区驱动和Twilio SendGrid LED。 我们需要你的帮助来实现这个目标。 为了确保我们按照正确的顺序建造正确的东西, 我们要求您创建issues和pull requests,或者简单地对现有问题或拉取请求进行投票或评论。
请浏览本自述文件的其余部分以了解更多详细信息。
感谢您的持续支持,谢谢!
目录
安装
先决条件
- python版本2.7和3.4+
- 对于电子邮件,您需要一个twilio sendGrid帐户,从free level
- 对于短信息,您需要一个免费的Twilio account
设置环境变量
mac
使用SENDGRID_API_KEY(更多信息here)更新开发环境,例如:
echo"export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env echo"sendgrid.env" >> .gitignore source ./sendgrid.env
twilio sendgrid还支持本地环境文件.env。 将.env_sample复制或重命名为.env,并使用密钥更新SENDGRID_API_KEY。
窗口
临时设置环境变量(仅在当前cli会话期间可访问):
setSENDGRID_API_KEY=YOUR_API_KEY
永久设置环境变量(可在所有后续的cli会话中访问):
setx SENDGRID_API_KEY "YOUR_API_KEY"
安装软件包
pip install sendgrid
快速启动
你好电子邮件
以下是发送带有/mail/send Helper的电子邮件所需的最少代码 (here是一个完整的示例):
使用邮件帮助程序类
importosfromsendgridimportSendGridAPIClientfromsendgrid.helpers.mailimportMailmessage=Mail(from_email='from_email@example.com',to_emails='to@example.com',subject='Sending with Twilio SendGrid is Fun',html_content='<strong>and easy to do anywhere, even with Python</strong>')try:sg=SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))response=sg.send(message)print(response.status_code)print(response.body)print(response.headers)exceptExceptionase:print(e.message)
Mail构造函数为您创建一个personalization object。 Here是一个如何添加的示例。
没有邮件助手类
以下是在不使用/mail/send助手的情况下发送电子邮件所需的最少代码 (here是一个完整的示例):
importosfromsendgridimportSendGridAPIClientmessage={'personalizations':[{'to':[{'email':'test@example.com'}],'subject':'Sending with Twilio SendGrid is Fun'}],'from':{'email':'test@example.com'},'content':[{'type':'text/plain','value':'and easy to do anywhere, even with Python'}]}try:sg=SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))response=sg.send(message)print(response.status_code)print(response.body)print(response.headers)exceptExceptionase:print(e.message)
v3 web api的一般用法(使用Fluent Interface)
importosfromsendgridimportSendGridAPIClientsg=SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))response=sg.client.suppression.bounces.get()print(response.status_code)print(response.body)print(response.headers)
一般v3 web api用法(不带Fluent Interface)
importosfromsendgridimportSendGridAPIClientsg=SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))response=sg.client._('suppression/bounces').get()print(response.status_code)print(response.body)print(response.headers)
处理入站电子邮件
请参阅our helper了解如何利用我们的入站解析webhook。
用法
用例
Examples of common API use cases,例如如何使用事务模板发送电子邮件、添加附件或发送短信。
公告
加入一个富有经验和激情的团队,专注于发挥影响力。 Opportunities abound来发展产品-并发展你的事业!
此库的所有更新都记录在CHANGELOG和releases中。 您还可以订阅电子邮件release notifications以获取发布和中断更改。
路线图
如果你对这个项目的未来方向感兴趣, 请看一下我们打开的issues和pull requests。 我们很想听听你的反馈。
故障排除
请参阅我们的troubleshooting guide以了解常见的库问题。
关于
sendgrid python由twilio开发者体验团队指导和支持。
如果有任何疑问,请发送电子邮件给开发人员体验团队here。
sendgrid python由twilio sendgrid,inc.维护和资助。 sendGrid python的名称和徽标是Twilio SendGrid,Inc.的商标。