Django的SendGrid后端
sendgrid-django的Python项目详细描述
使用SendGrid的Web API发送电子邮件的简单Django后端。
安装
从pypi安装后端:
pip install sendgrid-django
在项目的settings.py中添加以下内容:
EMAIL_BACKEND="sgbackend.SendGridBackend"SENDGRID_API_KEY="Your SendGrid API Key"
完成!
示例
fromdjango.core.mailimportsend_mailfromdjango.core.mailimportEmailMultiAlternativessend_mail("Your Subject","This is a simple text email body.","Yamil Asusta <hello@yamilasusta.com>",["yamil@sendgrid.com"])# ormail=EmailMultiAlternatives(subject="Your Subject",body="This is a simple text email body.",from_email="Yamil Asusta <hello@yamilasusta.com>",to=["yamil@sendgrid.com"],headers={"Reply-To":"support@sendgrid.com"})# Add templatemail.template_id='YOUR TEMPLATE ID FROM SENDGRID ADMIN'# Replace substitutions in sendgrid templatemail.substitutions={'%username%':'elbuo8'}# Attach filewithopen('somefilename.pdf','rb')asfile:mail.attachments=[('somefilename.pdf',file.read(),'application/pdf')]# Add categoriesmail.categories=['work','urgent',]mail.attach_alternative("<p>This is a simple HTML email body</p>","text/html")mail.send()
若要使用“设置”中提供的API键以外的API键创建SendGridBackend的实例,请将API键传递给构造函数
许可证
麻省理工学院
享受:)
开发
- 安装依赖项:
- pip安装-r requirements-dev.txt
- 运行覆盖率为: 的测试
- pytest–cov=sgbackend
- 如果看到错误“no module named sgbackend”,请运行:
- pip安装-e.