Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ Example of valid input::
{'email': '[email protected]', 'name': 'Name2'}]
['[email protected]', {'email': '[email protected]'},
{'email': '[email protected]', 'name': 'Name3'}]

Development
-----------
``pip install -r requirements.dev.txt``

15 changes: 12 additions & 3 deletions aiohttp_sendgrid/sendgrid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import aiohttp
import os
import sendgrid
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because it wasn't referenced in setup.py and didn't seem to be used.


SENDGRID_API_URL = 'https://api.sendgrid.com/v3'
SEND_URN = '/mail/send'
Expand Down Expand Up @@ -28,7 +27,7 @@ def __init__(self, api_key=None):
self.headers = {'authorization': auth}
self.send_url = SENDGRID_API_URL + SEND_URN

async def send(self, to, sender, subject, content, body_type='text/html'):
async def send(self, to, sender, subject, content, body_type='text/html', sandbox=False):
"""This coroutine performs ``/mail/send`` POST requests via ``aiohttp``
Example::

Expand Down Expand Up @@ -59,6 +58,9 @@ async def send(self, to, sender, subject, content, body_type='text/html'):
of the content.
By default it equals to ``text/html``.
Might be ``text/html`` or ``text/plain``
:param sandbox: (optional) whether the mail should be sent in
sandbox mode or not. See
https://sendgrid.com/docs/for-developers/sending-email/sandbox-mode/

# TODO: add cc, bcc, reply_to support
"""
Expand All @@ -73,14 +75,21 @@ def generate_payload():
payload['from'] = send_from
payload['subject'] = subject
payload['content'] = body

if sandbox:
payload["mail_settings"] = {
"sandbox_mode": {
"enable": True
}
}
return payload
payload = generate_payload()
async with aiohttp.ClientSession() as session:
response = await self._post(session, self.send_url, payload)
return response

async def send_sgmail(self, sgmail):
"""Convinient way to send existing `sendgrid.helpers.mail.Mail` object
"""Convenient way to send existing `sendgrid.helpers.mail.Mail` object

:param sgmail: A `sendgrid.helpers.mail.Mail` object
"""
Expand Down
3 changes: 3 additions & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-e .
pytest==4.5.0
pytest-asyncio==0.10.0
31 changes: 31 additions & 0 deletions tests/test_sandbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import Mock

from aiohttp_sendgrid import Sendgrid
import pytest

pytestmark = pytest.mark.asyncio


@pytest.fixture
def sendgrid():
sg = Sendgrid(api_key="blah")

async def mock_post(_, __, payload):
return payload

sg._post = mock_post
return sg


async def test_sandbox(sendgrid):

payload = await sendgrid.send("[email protected]", "[email protected]", "mysubject", "mycontent", sandbox=True)

assert payload["mail_settings"]["sandbox_mode"]["enable"] is True


async def test_no_sandbox(sendgrid):

payload = await sendgrid.send("[email protected]", "[email protected]", "mysubject", "mycontent")

assert "mail_settings" not in payload