Using the Python3 wrapper for Paubox Email API
In today’s post, we are going to use the Python3 wrapper for the Paubox email api. Python is one of the most popular languages of 2022 and has a thriving developer community. You can learn how to use Paubox’s secure email api with your Python3 application by following some simple steps as shown below.
- Creating a Python project
- Configuring the credentials
- Invoking the API
Creating a Python Project
In this example, we are using Visual Studio Code to set up the Python project and configure our Paubox credentials. We start by creating a simple folder to house the project, but you can create a virtual environment to keep it separate from other environments.
Create a folder named paubox-python3-wrapper and open it in Visual Studio Code. Make sure you have Python installed. Open a terminal and execute the following command: pip3 install paubox-python3. This installs the python3 SDK for Paubox. Create a new file and name it paubox-python-wrapper.py. This file will contain all the logic for calling the API. Create one more file named config.cfg. We will use this to store our Paubox credentials.
Configuring the credentials
To configure the credentials, you must have a Paubox account. You can sign up here. After signing up, follow this 5-minute guide to verify your domain and generate an API key. Copy the API key and keep it safe as it is shown only once. If you forget it or lose it, you must generate a new one.
Navigate to the config.cfg file and add these two lines. Enter the values from your account.
PAUBOX_HOST: ‘https://api.paubox.net/v1/ENTER_YOUR_USERNAME_HERE’
PAUBOX_API_KEY: ‘ENTER_YOUR_API_KEY_HERE’
Paste the API key that you generated earlier in the PAUBOX_API_KEY field and paste your username from the Paubox dashboard in the designated area of the PAUBOX_HOST field. Your username is present in the unique API endpoint for your domain. See the image below for reference. The blurred portion is your unique username.
Lastly, you must install the config package to use the configuration file in your application. To do this, run the following command: pip3 install config.
Invoking the API using the helper class
We now have the project and credentials configured. Let’s write some code to invoke the API. There are two ways in which you can invoke the API. The first method uses the Mail helper class and the second one is without it.
In the first two lines, we are importing the main paubox class and the Mail helper class. In the next section, we are importing the config class and opening the config.cfg file created earlier. Next, we create an instance of the PauboxApiClient by passing the API key and host from the config file. The next four lines contain the recipients, from address, subject and content. After this, we prepare an object of the Mail helper class by setting the from address, subject, recipients list and content. Lastly, we invoke the get() method from the mail object and pass it as an argument to the send method of the paubox_client. If everything went well, you should see something similar in your logs and an email in your inbox.
{“sourceTrackingId”:”x00x0x00-0x00-0x00-xx00-00000x0xxx00″, “data”:”Service OK”}
Invoking the API without the helper class
Without the helper class, you can invoke the API using a JSON payload. The first few steps remain the same as before. After creating an instance of the PauboxApiClient, you must create a json object that contains the recipients, subject, from_address and content. See below to understand the structure.
Instead of calling the get() method of the Mail helper class and passing it as an argument, we can just send the json object as a parameter to the send() method. You should see the same response as before if everything went well.
Configuring other options
Option for Helper Class | Option for JSON payload | Option for JSON payload Description |
optional_headers = { ‘allowNonTLS’: True } | ‘allowNonTLS’: True | Add this option to allow sending mails that need |
Add it to the Mail object as shown: mail = Mail(from_, subject, recipients, content, optional_headers) | Add this inside the message field in the JSON payload | not be HIPAA compliant |
optional_headers = { ‘forceSecureNotification’: True } Add it to the Mail object as shown: mail = Mail(from_, subject, recipients, content, optional_headers) | ‘forceSecureNotification’: ‘true’ Add this inside the message field in the JSON payload | Add this to use 2-factor authentication. Instead of an email, the recipient receives a notification about a new message in Paubox. |
You can also add other headers or options for different functionality. See the table below. See our Github page to view the complete list of available options and headers that you can use.
Send your first HIPAA compliant email in minutes.
Additional Paubox Email API resources
- Paubox Email API product overview
- GitHub repository for Paubox Email API SDKs
- The five-minute guide to Paubox Email API
- Paubox Email API documentation
- Using the NodeJS wrapper for Paubox Email API
- Using the Java wrapper for Paubox Email API
- HIPAA compliant email with Paubox Email API using the Python3 wrapper