Skip to content

SendPulse API SMTP SendEMail Python 3 example

Steve Wasiura edited this page Jan 4, 2022 · 1 revision

This is a simple example, using python 3 to post to SendPulse's API

first, create a python file to hold your client credentials


#SendPulse_API_Client_Credentials.py

# create account at SendPulse.com
# wait a few days for approval to make sure you are not a spammer
# get your client_id & secret from https://login.sendpulse.com/settings/#api

# SendPulse API Client ID and Secret go inside the dbl quotes

# STATUS ######
# 2022-01-04, values updated by [yourNameHere]

def ID() :
    # paste your Client ID inside the dbl quotes
    return "0000000000000000000000000000000"

def Secret() :
    # paste your Client Secret inside the dbl quotes
    return "0000000000000000000000000000000"

next, create a python file to run to send the test email


#SendPulse_API_SMTP_Email_test.py

import requests  # this is python 3 module which can post to url, for api endpoints

# you need an access token from SendPulse for all API endpoints
# create account at SendPulse.com, wait a few days for approval
# get your client_id & secret from https://login.sendpulse.com/settings/#api
# edit file SendPulse_API_Client_Credentials.py to hold those credentials
# import that file into this script
import SendPulse_API_Client_Credentials

# set URL to get access token
SendPulse_OAuth_URL = "https://api.sendpulse.com/oauth/access_token"
# set properties of object to pass to API
# these values come from the imported credentials file 
SendPulse_OAuth_Creds = {
   "grant_type":"client_credentials", # must be as shown!
   "client_id": SendPulse_API_Client_Credentials.ID(),  
   "client_secret": SendPulse_API_Client_Credentials.Secret()
    }
# send request, get response
# NOTE: key json will set Content-Type of Request to app/json
SendPulse_OAuth_Response = requests.post(SendPulse_OAuth_URL, json = SendPulse_OAuth_Creds)

# debug response
#print(SendPulse_OAuth_Response.text)

# set local variabe to hold the access token
# use json() to convert it to a json object, then get the element by it's key
SendPulse_myBearerAccessToken = SendPulse_OAuth_Response.json()["access_token"]
# debug variable value
#print(SendPulse_myBearerAccessToken)

# the resulting BearerAccessToken is valid for 1 hour
# you must send that key in the Header of all API requests


#######################################
# next you can try to send an email thru the API

# create a JSON object to hold the detail of the email
# NOTE: the FROM : EMAIL value must match a preset email configured at
# https://login.sendpulse.com/smtp/index/settings/  Tab=General, Section=Sender Email
# or else you will get error "unauthorized domain"

# html : HTML version of an email, encoded in Base64. If blank/empty, no html part will be generated
SendPulseEmailObject = {
  "email": {
    "html": "",
    "text": "SMTP API Test thru SendPulse using Python 3 requests",
    "subject": "SendPulse API test ",
    "from": {
      "name": "Authorized Sender",
      "email": "[email protected]"
    },
    "to": [
      {
        "name": "Recipient Name",
        "email": "[email protected]"
      }
    ]
  }
}

# debug email object
#print(SendPulseEmailObject)


# now attempt to send it
# set url for API endpoint
SendPulse_SMTP_Emails_URL = "https://api.sendpulse.com/smtp/emails"

# contruct the request method call using json as the payload, so it sets the content type correctly.
# set the Headers to include the BearerAccessToekn from above
SendPulse_Response = requests.post(
    SendPulse_SMTP_Emails_URL,
    json = SendPulseEmailObject,
    headers = {"Authorization": "Bearer "+ str(SendPulse_myBearerAccessToken) +"" }
    )

# debug the response
print(SendPulse_Response.text)

# a valid response will return:
#### {"result":true,"id":"r000xv-0o000z-0w"}


Clone this wiki locally