Python script examples covering these Email API functions:
(1) Get Email Sent: retrieve information about all emails sent during a given period of time.
(2) Send Mail: send an email to a given address and print the approved senders on your screen.
(3) Create An Approved Sender: create a new approved sender for your Email account.
Get Email Sent Example
{ /* ========================================================== * Dyn's Email Delivery RESTful API * Revised: January 31, 2014 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Dyn, Inc. provides this sample merely as an example of * how to use the Email Delivery REST API via Python. * While we will endeavor to keep the sample updated as the * API is enhanced, it is provided "as-is," without express * or implied warranty. In no event shall Dyn, Inc. be held * liable for any damages arising from the use of this code. * * ======================================================= */ #This script was built using Python version 2.7.2. #This script relies on using the Python library called 'requests'. #You can find this library at docs.python-requests.org #Install the requests library prior to using this script import requests #Your api key is found or generated on the integration tab in the Email Delivery portal api_key = "replace these words between the quotation marks with your api key" # my api key def get_emailssent(): my_params = { "apikey" : api_key, "starttime" : "replace with your start date and time based on ISO 8601 format", "endtime" : "replace with your end date and time based on ISO 8601 format", } #By printing r.text, you will get all of the information available for all emails sent #during the identified time period. You can also add a direction to save the text #to a file. r = requests.get('http://email.api.dynect.net/rest/json/reports/sent', params=my_params) print r.text print get_emailssent() }
NOTE: Reference the date formatting page for more information about the ISO 8601 format.
Send Mail Example
{ /* ========================================================== * Dyn's Email Delivery RESTful API * Revised: February 28, 2014 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Dyn, Inc. provides this sample merely as an example of * how to use the Email Delivery REST API via Python. * While we will endeavor to keep the sample updated as the * API is enhanced, it is provided "as-is," without express * or implied warranty. In no event shall Dyn, Inc. be held * liable for any damages arising from the use of this code. * * ======================================================= */ #This script was built using Python version 2.7.2. #This script relies on using the Python library called 'requests'. #You can find this library at docs.python-requests.org #Install the requests library prior to using this script import requests #Your api key is found or generated on the integration tab in the Email Delivery portal api_key = "replace these words between the quotation marks with your api key" # my api key def print_senders(): my_params = { "apikey" : api_key, } r = requests.get('http://email.api.dynect.net/rest/json/senders', params=my_params) response_obj = r.json() for sender in response_obj["response"]["data"]["senders"]: print sender["email"] def send_email(to_address): my_headers = { "Content-Type": "application/x-www-form-urlencoded" } my_params = { "apikey" : api_key, "from" : "replace these words with your from address", "to" : to_address, "subject" : "Hello from the API", "bodytext" : "This is a test message from the Dyn API.", "replyto" : "replace these words with your replyto address" } r = requests.post('http://email.api.dynect.net/rest/json/send', data=my_params, headers=my_headers) if (r.status_code == 200): print "email sent" else: print "some problem sending an email" print "sending an email" #runs the send_email() function and sends email to the address you enter send_email("replace these words with a single email address you want to send email to") print "printing all senders" #prints a list of the approved senders on your account print_senders() }
Create An Approved Sender
{ /* ========================================================== * Dyn's Email Delivery RESTful API * Revised: March 31, 2014 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Dyn, Inc. provides this sample merely as an example of * how to use the Email Delivery REST API via Python. * While we will endeavor to keep the sample updated as the * API is enhanced, it is provided "as-is," without express * or implied warranty. In no event shall Dyn, Inc. be held * liable for any damages arising from the use of this code. * * ======================================================= */ #This script was built using Python version 2.7.2. #This script relies on using the Python library called 'requests'. #You can find this library at docs.python-requests.org #Install the requests library prior to using this script import requests #Your api key is found or generated on the integration tab in the Email Delivery portal api_key = "replace these words between the quotation marks with your api key" # my api key def create_sender(): my_params = {"apikey" : api_key,"emailaddress" : email_address,"seeding" : "1",} my_headers = {"Content-Type": "application/x-www-form-urlencoded"} print my_params r = requests.post('http://email.api.dynect.net/rest/json/senders', data=my_params, headers=my_headers) response_obj = r.json() if (r.status_code == 200): print "Approved Sender " + email_address + " created." else: print r.status_code print email_address print "Approved Sender request not created. " }