#Simple Mailjet APIv3 wrapper
Every code examples can be find on the Mailjet Documentation
(Please refer to the Mailjet Documentation Repository to contribute to the documentation examples)
(sudo) pip install mailjet_rest
First, make sure you have an API key, and an API secret. Once you got them, save them in your environment:
export MJ_APIKEY_PUBLIC='your api key'
export MJ_APIKEY_PRIVATE='your api secret'
# import the mailjet wrapper
from mailjet_rest import Client
import os
# Get your environment Mailjet keys
API_KEY = os.environ['MJ_APIKEY_PUBLIC']
API_SECRET = os.environ['MJ_APIKEY_PRIVATE']
mailjet = Client(auth=(API_KEY, API_SECRET))
# get every contacts
result = mailjet.contact.get()
# get the 2 first contacts
result = mailjet.contact.get(filters={'limit': 2})
# Register a new sender email address
result = mailjet.sender.create(data={'email': 'test@mailjet.com'})
# Get the contact lists of contact #2
result = mailjet.contact_getcontactslists.get(id=2)
email = {
'FromName': 'Mr Smith',
'FromEmail': 'mr@smith.com',
'Subject': 'Test Email',
'Text-Part': 'Hey there !',
'Recipients': [{'Email': 'your email here'}]
}
mailjet.send.create(email)
# wrapping the call inside a function
def new_contact(email):
return mailjet.contact.create(data={'Email': email})
new_contact('mr@smith.com')