Skip to content

Latest commit

 

History

History
157 lines (123 loc) · 4.86 KB

File metadata and controls

157 lines (123 loc) · 4.86 KB

Update Campaign

This guide will walk you through steps to update an existing campaign on your account using the Python library.

Required Access Level: ModifyCampaigns

What's a campaign?

When using Elastic Email, when you send an email to any group of contacts we call that a "campaign".

To send a campaign you need a template (which becomes the email body itself) and you need contacts (the recipients who receive the email).

Preparation

Install Python 3.

Install ElasticEmail library.

Eg. run in terminal pip install ElasticEmail to install from PyPi repository.

Create a new Python file snippet.py and open it in editor of your preference eg. PyCharm (https://www.jetbrains.com/pycharm/download/)

Let's dig into the code

Put the below code to your file.

Load libraries using below code:

import ElasticEmail
from ElasticEmail.models.campaign import Campaign
from ElasticEmail.models.campaign_recipient import CampaignRecipient
from ElasticEmail.models.campaign_status import CampaignStatus
from ElasticEmail.models.campaign_template import CampaignTemplate
from pprint import pprint

Generate and use your API key (remember to check a required access level).

Defining the host is optional and defaults to https://api.elasticemail.com/v4

configuration = ElasticEmail.Configuration()
configuration.api_key['apikey'] = 'YOUR_API_KEY'

Pass configuration to an api client and make it instance available under api_client name:

with ElasticEmail.ApiClient(configuration) as api_client:

Create an instance of CampaignsApi that will be used to update a campaign.

    api_instance = ElasticEmail.CampaignsApi(api_client)
    name = "hello campaign"

Create an example campaign object:

  • Name: defines campaign name by which you can identify it later
  • Recipients: define your audience
  • Conent: define your message details
  • Status: define status in which campaign should be created

Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/campaignsByNamePut

Send will be triggered immediately or postponed, depending on given options. Because we define Status as Draft, so in this case it will be postponed and campaign will be added to drafts.

    campaign = Campaign(
        Content=[
            CampaignTemplate(
                From="test@email.test",
                ReplyTo="test@email.test",
                Subject="Hello",
                TemplateName="hello_template",
            ),
        ],
        Name="hello campaign update",
        Status=CampaignStatus("Draft"),
        Recipients=CampaignRecipient(
            list_names=[
                "my list name",
            ],
        ),
    )

Use try & except block to call campaigns_by_name_put method from the API to update a campaign:

    try:
        # Add Campaign
        api_response = api_instance.campaigns_by_name_put(name=name, campaign=campaign)
        pprint(api_response)
    except ElasticEmail.ApiException as e:
        print("Exception when calling CampaignsApi->campaigns_by_name_put: %s\n" % e)

The whole code to copy and paste:

import ElasticEmail
from ElasticEmail.models.campaign import Campaign
from ElasticEmail.models.campaign_recipient import CampaignRecipient
from ElasticEmail.models.campaign_status import CampaignStatus
from ElasticEmail.models.campaign_template import CampaignTemplate
from pprint import pprint

# Defining the host is optional and defaults to https://api.elasticemail.com/v4
configuration = ElasticEmail.Configuration()

# Configure API key authorization: apikey
configuration.api_key['apikey'] = 'YOUR_API_KEY'

"""
Update Campaign
Example api call that updates a campaign.
Send will be triggered immediately or postponed, depending on given options.
"""
with ElasticEmail.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = ElasticEmail.CampaignsApi(api_client)
    name = "hello campaign"

    campaign = Campaign(
        Content=[
            CampaignTemplate(
                From="test@email.test",
                ReplyTo="test@email.test",
                Subject="Hello",
                TemplateName="hello_template",
            ),
        ],
        Name="hello campaign update",
        Status=CampaignStatus("Draft"),
        Recipients=CampaignRecipient(
            list_names=[
                "my list name",
            ],
        ),
    ) # Campaign | JSON representation of a campaign

    # example passing only required values which don't have defaults set
    try:
        # Add Campaign
        api_response = api_instance.campaigns_by_name_put(name=name, campaign=campaign)
        pprint(api_response)
    except ElasticEmail.ApiException as e:
        print("Exception when calling CampaignsApi->campaigns_by_name_put: %s\n" % e)

Run the code

python3 snippet.py