Skip to content

Commit ea1e410

Browse files
author
Yamil Asusta
committed
Merge pull request elbuo8#10 from AMeng/use-api-key
Use SendGrid API key
2 parents f01f539 + 026d640 commit ea1e410

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ build
22
dist
33
sendgrid_django.egg-info
44
.pyc
5+
.eggs
56
venv

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ Install the backend from PyPI:
1414
1515
Add the following to your project’s **settings.py**:
1616

17+
.. code:: python
18+
19+
EMAIL_BACKEND = "sgbackend.SendGridBackend"
20+
SENDGRID_API_KEY = "Your SendGrid API Key"
21+
22+
Or, SendGrid username and password can be used instead of an API key:
23+
1724
.. code:: python
1825
1926
EMAIL_BACKEND = "sgbackend.SendGridBackend"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
license='MIT',
1515
description='SendGrid Backend for Django',
1616
long_description=open('./README.rst').read(),
17-
install_requires=["sendgrid==1.3.0"],
17+
install_requires=["sendgrid==1.4.0"],
1818
)

sgbackend/mail.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@ class SendGridBackend(BaseEmailBackend):
1818
def __init__(self, fail_silently=False, **kwargs):
1919
super(SendGridBackend, self).__init__(
2020
fail_silently=fail_silently, **kwargs)
21+
self.api_key = getattr(settings, "SENDGRID_API_KEY", None)
2122
self.api_user = getattr(settings, "SENDGRID_USER", None)
22-
self.api_key = getattr(settings, "SENDGRID_PASSWORD", None)
23-
24-
if self.api_user is None or self.api_key is None:
23+
self.api_password = getattr(settings, "SENDGRID_PASSWORD", None)
24+
25+
credentials = []
26+
if self.api_key:
27+
credentials.append(self.api_key)
28+
elif self.api_user and self.api_password:
29+
credentials.append(self.api_user)
30+
credentials.append(self.api_password)
31+
else:
2532
raise ImproperlyConfigured('''
26-
Either SENDGRID_USER or SENDGRID_PASSWORD
27-
was not declared in settings.py''')
33+
Either SENDGRID_API_KEY or both (SENDGRID_USER and
34+
SENDGRID_PASSWORD) must be declared in settings.py''')
2835
self.sg = sendgrid.SendGridClient(
29-
self.api_user, self.api_key,
36+
*credentials,
3037
raise_errors=not fail_silently)
3138

3239
def open(self):

0 commit comments

Comments
 (0)