# django-sendgrid-v5
[![Latest Release](https://img.shields.io/pypi/v/django-sendgrid-v5.svg)](https://pypi.python.org/pypi/django-sendgrid-v5/)
This package implements an email backend for Django that relies on sendgrid's REST API for message delivery.
It is under active development, and pull requests are more than welcome\!
To use the backend, simply install the package (using pip), set the `EMAIL_BACKEND` setting in Django, and add a `SENDGRID_API_KEY` key (set to the appropriate value) to your Django settings.
## How to Install
1. `pip install django-sendgrid-v5`
2. In your project's settings.py script:
1. Set `EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"`
2. Set the SENDGRID\_API\_KEY in settings.py to your api key that was provided to you by sendgrid. `SENDGRID_API_KEY = os.environ["SENDGRID_API_KEY"]`
### Other settings
1. To toggle sandbox mode (when django is running in DEBUG mode), set `SENDGRID_SANDBOX_MODE_IN_DEBUG = True/False`.
1. To err on the side of caution, this defaults to True, so emails sent in DEBUG mode will not be delivered, unless this setting is explicitly set to False.
2. `SENDGRID_ECHO_TO_STDOUT` will echo to stdout or any other file-like
object that is passed to the backend via the `stream` kwarg.
3. `SENDGRID_TRACK_EMAIL_OPENS` - defaults to true and tracks email open events via the Sendgrid service. These events are logged in the Statistics UI, Email Activity interface, and are reported by the Event Webhook.
4. `SENDGRID_TRACK_CLICKS_HTML` - defaults to true and, if enabled in your Sendgrid account, will tracks click events on links found in the HTML message sent.
5. `SENDGRID_TRACK_CLICKS_PLAIN` - defaults to true and, if enabled in your Sendgrid account, will tracks click events on links found in the plain text message sent.
## Usage
### Simple
```python
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
'from@example.com',
['to@example.com'],
fail_silently=False,
)
```
### Dynamic Template with JSON Data
First, create a [dynamic template](https://mc.sendgrid.com/dynamic-templates) and copy the ID.
```python
from django.core.mail import EmailMessage
msg = EmailMessage(
from_email='to@example.com',
to=['to@example.com'],
)
msg.template_id = "your-dynamic-template-id"
msg.dynamic_template_data = {
"title": foo
}
msg.send(fail_silently=False)
```
### The kitchen sink EmailMessage (all of the supported sendgrid-specific properties)
```python
from django.core.mail import EmailMessage
msg = EmailMessage(
from_email='to@example.com',
to=['to@example.com'],
cc=['cc@example.com'],
bcc=['bcc@example.com'],
)
# Personalization custom args
# https://sendgrid.com/docs/for-developers/sending-email/personalizations/
msg.custom_args = {'arg1': 'value1', 'arg2': 'value2'}
# Reply to email address (sendgrid only supports 1 reply-to email address)
msg.reply_to = 'reply-to@example.com'
# Send at (accepts an integer per the sendgrid docs)
# https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html#-Send-At
msg.send_at = 1600188812
# Transactional templates
# https://sendgrid.com/docs/ui/sending-email/how-to-send-an-email-with-dynamic-transactional-templates/
msg.template_id = "your-dynamic-template-id"
msg.dynamic_template_data = { # Sendgrid v6+ only
"title": foo
}
msg.substitutions = {
"title": bar
}
# Unsubscribe groups
# https://sendgrid.com/docs/ui/sending-email/unsubscribe-groups/
msg.asm = {'group_id': 123, 'groups_to_display': ['group1', 'group2']}
# Categories
# https://sendgrid.com/docs/glossary/categories/
msg.categories = ['category1', 'category2']
# IP Pools
# https://sendgrid.com/docs/ui/account-and-settings/ip-pools/
msg.ip_pool_name = 'my-ip-pool'
msg.send(fail_silently=False)
```
## Examples
- Marcelo Canina [(@marcanuy)](https://github.com/marcanuy) wrote a great article demonstrating how to integrate `django-sendgrid-v5` into your Django application on his site: [https://simpleit.rocks/python/django/adding-email-to-django-the-easiest-way/](https://simpleit.rocks/python/django/adding-email-to-django-the-easiest-way/)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源分类:Python库 所属语言:Python 资源全名:django-sendgrid-v5-1.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
django-sendgrid-v5-1.1.2.tar.gz (22个子文件)
django-sendgrid-v5-1.1.2
MANIFEST.in 59B
PKG-INFO 6KB
pyproject.toml 217B
test
test_sandbox_mode.py 2KB
test_post_to_sendgrid.py 1KB
test_mail.py 24KB
test_backend_init.py 707B
test_echo_to_stream.py 2KB
django_sendgrid_v5.egg-info
PKG-INFO 6KB
requires.txt 54B
SOURCES.txt 525B
top_level.txt 17B
dependency_links.txt 1B
sendgrid_backend
mail.py 18KB
util.py 1KB
__init__.py 83B
signals.py 71B
version.py 22B
LICENSE 1023B
setup.cfg 38B
setup.py 1KB
README.md 4KB
共 22 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- cad定制家具平面图工具-(FG)门板覆盖柜体
- asp.net 原生js代码及HTML实现多文件分片上传功能(自定义上传文件大小、文件上传类型)
- whl@pip install pyaudio ERROR: Failed building wheel for pyaudio
- Constantsfd密钥和权限集合.kt
- 基于Java的财务报销管理系统后端开发源码
- 基于Python核心技术的cola项目设计源码介绍
- 基于Python及多语言集成的TSDT软件过程改进设计源码
- 基于Java语言的歌唱比赛评分系统设计源码
- 基于JavaEE技术的课程项目答辩源码设计——杨晔萌、李知林、岳圣杰、张俊范小组作品
- 基于Java原生安卓开发的蔚蓝档案娱乐应用设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功