Skip to content

kurlov/aiohttp-sendgrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aiohttp-sendgrid

https://travis-ci.org/Kurlov/aiohttp-sendgrid.svg?branch=master

SendGrid mail API wrapper

Installation

pip install aiohttp_sendgrid

Usage

Create an instance of API client:

import asyncio
from aiohttp_sendgrid import Sendgrid
api_key = '<your_sendgrid_api_key>'
mailer = Sendgrid(api_key=api_key)

Important to note that if api_key is not provided then it will try to read SENDGRID_API_KEY environment variable

Send email to single recipient

to = '[email protected]'
sender = '[email protected]'
subject = 'greetings'
content = '<h1>Hello</h1>'
send_mail = mailer.send(to, sender, subject, content)
loop = asyncio.get_event_loop()
loop.run_until_complete(send_mail)

Both to and sender might be also a dictionary with email key, if you want to specify name for sender or recipient then add name key to the dictionary. Thus, to = {'email': '[email protected]', 'name': 'Recipient'} is also a correct value.

Send single email to multiple recipients

to = ['[email protected]', 'another@example']
sender = '[email protected]'
subject = 'greetings'
content = '<h1>Hello</h1>'
send_mail = mailer.send(to, sender, subject, content)
loop = asyncio.get_event_loop()
loop.run_until_complete(send_mail)

to might be tuple or list of strings or dictionaries. Example of valid input:

'[email protected]'
{'email': '[email protected]'}
{'email': '[email protected]', 'name': 'Name'}
['[email protected]']
['[email protected]', '[email protected]']
[{'email': '[email protected]'}]
[{'email': '[email protected]'}, {'email': '[email protected]'}]
[{'email': '[email protected]', 'name': 'Name'}]
[{'email': '[email protected]', 'name': 'Name'},
 {'email': '[email protected]', 'name': 'Name2'}]
['[email protected]', {'email': '[email protected]'},
 {'email': '[email protected]', 'name': 'Name3'}]