Python

As an outcome of this guide, you will send an email to your inbox using Infobip Email API.


Prerequisites

Infobip account

• Working Python 3 environment

Difficulty level

This guide assumes very basic knowledge of Python and basic familiarity with APIs.

Summary of the steps

• Install the Infobip API Python SDK.
• Import the Email Channel and create an Email Channel instance.
• Add credentials and email payload to your instance.
• Send an email and print the response to track its progress.

Install the Infobip API Python SDK

pip install infobip-api-python-sdk

Create an Email Channel instance

You’ll use the instance to input your credentials and access its methods. In this case, we will use the send_email_message method to add the Email payload.

Step 1. Import the EmailChannel.

from infobip_channels.email.channel import EmailChannel

Step 2. Create an EmailChannel instance and add your base_url and api_key that you can access either from your Infobip account or from the Infobip API landing page, once logged in.

channel = EmailChannel.from_auth_params({
    "base_url": "<your_base_url>",
    "api_key": "<your_api_key>"
})

Step 3. Use the send_email_message method to add the email payload.

Key points:

• We recommend you put the send_email_message method within a variable, so that you can print out the response.

email_response = channel.send_email_message({
        "from": "[email protected]",
        "to": "[email protected]",
        "subject": "Hi there!",
        "text": "It is me, SDK!"
})

Create a batch sending request

Add multiple recipients by using the to, cc and bbc fields to input a maximum of 1000 email addresses in total. Each email address cannot exceed 256 characters. Since it’s a HTTP multi-part request, you’ll use multiple to, cc and bcc fields for each address you wish to pass in the request.

Key points:

• Use multiple cc and bcc fields when sending the same email to multiple recipients.
• Use multiple to fields when sending an email with placeholders, as each email is created separately.
• Use the bulkId and messageId fields if you want to pass your custom ID; otherwise, these will be autogenerated and returned in the response. Bulk ID is used for batch sending and identifies a group of emails each having its own message ID. You’ll use them for troubleshooting and delivery reports.

{
  "from": "[email protected]",
  "to": "[email protected]",
  "cc": "[email protected], ",
  "bcc": "[email protected]",
  "subject": "Hi there!",
  "text": "It is me, SDK!",
  "bulkId": "my-campaign"
}

Step 4. Print the response to see what happened to your email.

print(sms_response)

Once you run the code, you should get an email into your inbox and see the following 200 OK response.

One recipient or one recipient and multiple CC and BCC recipients

{
    "bulkId": "2h2541dqnvzkytr1q8nv",
    "messages": [
        {
            "to": "[email protected]",
            "messageId": "68odsozntppd74pvunh8",
            "status": {
                "groupId": 1,
                "groupName": "PENDING",
                "id": 26,
                "name": "PENDING_ACCEPTED",
                "description": "Message accepted, pending for delivery."
            }
        }
    ]
}

Multiple TO recipients

{
    "bulkId": "m8dkv481xevpv3ayyvag",
    "messages": [
        {
            "to": "[email protected]",
            "messageId": "gcg7v6zz65h96gvlagri",
            "status": {
                "groupId": 1,
                "groupName": "PENDING",
                "id": 26,
                "name": "PENDING_ACCEPTED",
                "description": "Message accepted, pending for delivery."
            }
        },
        {
            "to": "[email protected]",
            "messageId": "oi5ku55ecf3aialds1dz",
            "status": {
                "groupId": 1,
                "groupName": "PENDING",
                "id": 26,
                "name": "PENDING_ACCEPTED",
                "description": "Message accepted, pending for delivery."
            }
        }
    ]
}

For troubleshooting and analytics, Use the auto-generated bulkId to fetch the entire bulk of messages, or the messageId to fetch one specific message.