Skip to content

Commit 7bbd1c7

Browse files
committed
SendGrid: support username/password auth
Closes #9
1 parent 9462d03 commit 7bbd1c7

File tree

4 files changed

+113
-13
lines changed

4 files changed

+113
-13
lines changed

anymail/backends/sendgrid.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.core.exceptions import ImproperlyConfigured
12
from django.core.mail import make_msgid
23

34
from ..exceptions import AnymailImproperlyInstalled, AnymailRequestsAPIError
@@ -20,7 +21,16 @@ class SendGridBackend(AnymailRequestsBackend):
2021

2122
def __init__(self, **kwargs):
2223
"""Init options from Django settings"""
23-
self.api_key = get_anymail_setting('SENDGRID_API_KEY', allow_bare=True)
24+
# Auth requires *either* SENDGRID_API_KEY or SENDGRID_USERNAME+SENDGRID_PASSWORD
25+
self.api_key = get_anymail_setting('SENDGRID_API_KEY', default=None, allow_bare=True)
26+
self.username = get_anymail_setting('SENDGRID_USERNAME', default=None, allow_bare=True)
27+
self.password = get_anymail_setting('SENDGRID_PASSWORD', default=None, allow_bare=True)
28+
if self.api_key is None and self.username is None and self.password is None:
29+
raise ImproperlyConfigured(
30+
"You must set either SENDGRID_API_KEY or both SENDGRID_USERNAME and "
31+
"SENDGRID_PASSWORD in your Django ANYMAIL settings."
32+
)
33+
2434
# This is SendGrid's Web API v2 (because the Web API v3 doesn't support sending)
2535
api_url = get_anymail_setting("SENDGRID_API_URL", "https://api.sendgrid.com/api/")
2636
if not api_url.endswith("/"):
@@ -53,9 +63,16 @@ def __init__(self, message, defaults, backend, *args, **kwargs):
5363
self.message_id = None # Message-ID -- assigned in serialize_data unless provided in headers
5464
self.smtpapi = {} # SendGrid x-smtpapi field
5565

56-
auth_headers = {'Authorization': 'Bearer ' + backend.api_key}
66+
http_headers = kwargs.pop('headers', {})
67+
query_params = kwargs.pop('params', {})
68+
if backend.api_key is not None:
69+
http_headers['Authorization'] = 'Bearer %s' % backend.api_key
70+
else:
71+
query_params['api_user'] = backend.username
72+
query_params['api_key'] = backend.password
5773
super(SendGridPayload, self).__init__(message, defaults, backend,
58-
headers=auth_headers, *args, **kwargs)
74+
params=query_params, headers=http_headers,
75+
*args, **kwargs)
5976

6077
def get_api_endpoint(self):
6178
return "mail.send.json"

anymail/tests/test_sendgrid_backend.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,35 @@ def test_send_mail(self):
4848
mail.send_mail('Subject here', 'Here is the message.',
4949
'[email protected]', ['[email protected]'], fail_silently=False)
5050
self.assert_esp_called('/api/mail.send.json')
51-
headers = self.get_api_call_headers()
52-
self.assertEqual(headers["Authorization"], "Bearer test_api_key")
51+
http_headers = self.get_api_call_headers()
52+
self.assertEqual(http_headers["Authorization"], "Bearer test_api_key")
53+
54+
query = self.get_api_call_params(required=False)
55+
if query:
56+
self.assertNotIn('api_user', query)
57+
self.assertNotIn('api_key', query)
58+
5359
data = self.get_api_call_data()
5460
self.assertEqual(data['subject'], "Subject here")
5561
self.assertEqual(data['text'], "Here is the message.")
5662
self.assertEqual(data['from'], "[email protected]")
5763
self.assertEqual(data['to'], ["[email protected]"])
5864
# make sure backend assigned a Message-ID for event tracking
59-
headers = json.loads(data['headers'])
60-
self.assertRegex(headers['Message-ID'], r'\<.+@sender\.example\.com\>') # id uses from_email's domain
65+
email_headers = json.loads(data['headers'])
66+
self.assertRegex(email_headers['Message-ID'], r'\<.+@sender\.example\.com\>') # id uses from_email's domain
67+
68+
@override_settings(ANYMAIL={'SENDGRID_USERNAME': 'sg_username', 'SENDGRID_PASSWORD': 'sg_password'})
69+
def test_user_pass_auth(self):
70+
"""Make sure alternative USERNAME/PASSWORD auth works"""
71+
mail.send_mail('Subject here', 'Here is the message.',
72+
'[email protected]', ['[email protected]'], fail_silently=False)
73+
self.assert_esp_called('/api/mail.send.json')
74+
query = self.get_api_call_params()
75+
self.assertEqual(query['api_user'], 'sg_username')
76+
self.assertEqual(query['api_key'], 'sg_password')
77+
http_headers = self.get_api_call_headers(required=False)
78+
if http_headers:
79+
self.assertNotIn('Authorization', http_headers)
6180

6281
def test_name_addr(self):
6382
"""Make sure RFC2822 name-addr format (with display-name) is allowed
@@ -534,9 +553,11 @@ def test_esp_send_defaults(self):
534553
class SendGridBackendImproperlyConfiguredTests(SimpleTestCase, AnymailTestMixin):
535554
"""Test ESP backend without required settings in place"""
536555

537-
def test_missing_api_key(self):
556+
def test_missing_auth(self):
538557
with self.assertRaises(ImproperlyConfigured) as cm:
539558
mail.send_mail('Subject', 'Message', '[email protected]', ['[email protected]'])
540559
errmsg = str(cm.exception)
560+
# Make sure the exception mentions all the auth keys:
541561
self.assertRegex(errmsg, r'\bSENDGRID_API_KEY\b')
542-
self.assertRegex(errmsg, r'\bANYMAIL_SENDGRID_API_KEY\b')
562+
self.assertRegex(errmsg, r'\bSENDGRID_USERNAME\b')
563+
self.assertRegex(errmsg, r'\bSENDGRID_PASSWORD\b')

anymail/tests/test_sendgrid_integration.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
from datetime import datetime, timedelta
66

7+
from django.core.mail import send_mail
78
from django.test import SimpleTestCase
89
from django.test.utils import override_settings
910

@@ -12,8 +13,13 @@
1213

1314
from .utils import AnymailTestMixin, sample_image_path
1415

16+
# For API_KEY auth tests:
1517
SENDGRID_TEST_API_KEY = os.getenv('SENDGRID_TEST_API_KEY')
1618

19+
# For USERNAME/PASSWORD auth tests:
20+
SENDGRID_TEST_USERNAME = os.getenv('SENDGRID_TEST_USERNAME')
21+
SENDGRID_TEST_PASSWORD = os.getenv('SENDGRID_TEST_PASSWORD')
22+
1723

1824
@unittest.skipUnless(SENDGRID_TEST_API_KEY,
1925
"Set SENDGRID_TEST_API_KEY environment variable "
@@ -91,3 +97,31 @@ def test_invalid_api_key(self):
9197
self.assertEqual(err.status_code, 400)
9298
# Make sure the exception message includes SendGrid's response:
9399
self.assertIn("authorization grant is invalid", str(err))
100+
101+
102+
@unittest.skipUnless(SENDGRID_TEST_USERNAME and SENDGRID_TEST_PASSWORD,
103+
"Set SENDGRID_TEST_USERNAME and SENDGRID_TEST_PASSWORD"
104+
"environment variables to run SendGrid integration tests")
105+
@override_settings(ANYMAIL_SENDGRID_USERNAME=SENDGRID_TEST_USERNAME,
106+
ANYMAIL_SENDGRID_PASSWORD=SENDGRID_TEST_PASSWORD,
107+
EMAIL_BACKEND="anymail.backends.sendgrid.SendGridBackend")
108+
class SendGridBackendUserPassIntegrationTests(SimpleTestCase, AnymailTestMixin):
109+
"""SendGrid username/password API integration tests
110+
111+
(See notes above for the API-key tests)
112+
"""
113+
114+
def test_valid_auth(self):
115+
sent_count = send_mail('Anymail SendGrid username/password integration test',
116+
'Text content', '[email protected]', ['[email protected]'])
117+
self.assertEqual(sent_count, 1)
118+
119+
@override_settings(ANYMAIL_SENDGRID_PASSWORD="Hey, this isn't the password!")
120+
def test_invalid_auth(self):
121+
with self.assertRaises(AnymailAPIError) as cm:
122+
send_mail('Anymail SendGrid username/password integration test',
123+
'Text content', '[email protected]', ['[email protected]'])
124+
err = cm.exception
125+
self.assertEqual(err.status_code, 400)
126+
# Make sure the exception message includes SendGrid's response:
127+
self.assertIn("Bad username / password", str(err))

docs/esps/sendgrid.rst

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ their name with an uppercase "G", so Anymail does too.)
3131

3232
.. rubric:: SENDGRID_API_KEY
3333

34-
Required. A SendGrid API key with "Mail Send" permission.
35-
(Manage API keys in your `SendGrid API key settings`_.
36-
Anymail does not support SendGrid's earlier username/password
37-
authentication.)
34+
A SendGrid API key with "Mail Send" permission.
35+
(Manage API keys in your `SendGrid API key settings`_.)
36+
Either an API key or both :setting:`SENDGRID_USERNAME <ANYMAIL_SENDGRID_USERNAME>`
37+
and :setting:`SENDGRID_PASSWORD <ANYMAIL_SENDGRID_PASSWORD>` are required.
3838

3939
.. code-block:: python
4040
@@ -50,6 +50,34 @@ nor ``ANYMAIL_SENDGRID_API_KEY`` is set.
5050
.. _SendGrid API key settings: https://app.sendgrid.com/settings/api_keys
5151

5252

53+
.. setting:: ANYMAIL_SENDGRID_USERNAME
54+
.. setting:: ANYMAIL_SENDGRID_PASSWORD
55+
56+
.. rubric:: SENDGRID_USERNAME and SENDGRID_PASSWORD
57+
58+
SendGrid credentials with the "Mail" permission. You should **not**
59+
use the username/password that you use to log into SendGrid's
60+
dashboard. Create credentials specifically for sending mail in the
61+
`SendGrid credentials settings`_.
62+
63+
.. code-block:: python
64+
65+
ANYMAIL = {
66+
...
67+
"SENDGRID_USERNAME": "<sendgrid credential with Mail permission>",
68+
"SENDGRID_PASSWORD": "<password for that credential>",
69+
}
70+
71+
Either username/password or :setting:`SENDGRID_API_KEY <ANYMAIL_SENDGRID_API_KEY>`
72+
are required (but not both).
73+
74+
Anymail will also look for ``SENDGRID_USERNAME`` and ``SENDGRID_PASSWORD`` at the
75+
root of the settings file if neither ``ANYMAIL["SENDGRID_USERNAME"]``
76+
nor ``ANYMAIL_SENDGRID_USERNAME`` is set.
77+
78+
.. _SendGrid credentials settings: https://app.sendgrid.com/settings/credentials
79+
80+
5381
.. setting:: ANYMAIL_SENDGRID_API_URL
5482

5583
.. rubric:: SENDGRID_API_URL

0 commit comments

Comments
 (0)