Python | Elastic E mail

0
9

[ad_1]

Authentication

To supply legitimate authentication and begin utilizing our API, you’ll need an API key. To generate your API key, enter settings in your Elastic E mail account and go to Settings -> Handle API Keys -> Create or you can even click on on the hyperlink: 

https://elasticemail.com/account#/settings/new/create-api

Ceater API key

At this level, you may set customized permissions and non-obligatory entry in your API key. 

Safety tip: The restriction forces the API Key to work solely with an IP or IP vary that will likely be specified on this discipline.

Create API key 2

When you create your API key, hold it secure as it is going to be used for each API name you make to be able to determine you and ensure your account’s credentials. You’ll be able to create both as much as 15 or a vast quantity of API keys primarily based in your pricing plan. 

Your API key must be despatched contained in the header with the parameter title ‘x-elasticemail-apikey’ and your API key as a price.

Set up and Utilization

Necessities

The required Python model is 3.6 or later.

Set up

Our official downloadable Python library is accessible on GitHub.

We’ve ready for you the steps to take to put in and implement our Python library:
If the python bundle is hosted on a repository, you may set up it immediately utilizing:

Copied!

pip set up git+https://github.com/elasticemail/elasticemail-python.git

(it’s possible you’ll have to run pip with root permission: sudo pip set up

git+https://github.com/elasticemail/elasticemail-python.git)

Then import the bundle:

Copied!

import ElasticEmail

After the set up process, run the next:

Please comply with the set up process after which run the next:

Copied!

import time
import ElasticEmail
from pprint import pprint
from ElasticEmail.api import campaigns_api
from ElasticEmail.mannequin.marketing campaign import Marketing campaign
# Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4
# See configuration.py for an inventory of all supported configuration parameters.
configuration = ElasticEmail.Configuration(
    host = "https://api.elasticemail.com/v4"
)
# The shopper should configure the authentication and authorization parameters
# in accordance with the API server safety coverage.
# Examples for every auth technique are offered under, use the instance that
# satisfies your auth use case.
# Configure API key authorization: apikey
configuration.api_key['apikey'] = 'YOUR_API_KEY'
# Uncomment under to setup prefix (e.g. Bearer) for API key, if wanted
# configuration.api_key_prefix['apikey'] = 'Bearer'
# Enter a context with an occasion of the API shopper
with ElasticEmail.ApiClient(configuration) as api_client:
    # Create an occasion of the API class
    api_instance = campaigns_api.CampaignsApi(api_client)
    title = "name_example" # str | Title of Marketing campaign to delete
    attempt:
        # Delete Marketing campaign
        api_instance.campaigns_by_name_delete(title)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling CampaignsApi->campaigns_by_name_delete: %sn" % e)

Fast begin information

On this part, we are going to inform you the steps to start out sending emails with our e mail API.

1. Register a free account

Register and activate your Elastic E mail account to get entry to our product.

2. Confirm your area

Comply with the directions on our settings web page to confirm your area and begin sending with us.

3. Create an API Key

Go to your setting to generate an API Key so as to add to your code later.

4. Set up our libraries

Set up our Python library as defined within the Set up and utilization part.

5. Ship your first e mail with API

Now it’s time to ship your first take a look at e mail with API to guarantee that all the pieces is about up appropriately after downloading the library and the authentication course of.

Copied!

configuration = ElasticEmail.Configuration()
configuration.api_key['apikey'] = '895A382DF3DCC13A97E72EXAMPLEKEY'
 
with ElasticEmail.ApiClient(configuration) as api_client:
    api_instance = emails_api.EmailsApi(api_client)
    email_message_data = EmailMessageData(
        recipients=[
            EmailRecipient(
                email="MeowWow "
            ),
        ],
        content material={
	    "Physique": [
		{
		    "ContentType":"HTML",
		    "Content":"My test email content ;)"
		}
	    ],
	    "Topic": "Python EE lib take a look at",
	    "From": "MyEmail "
	}
    )
 
    attempt:
        api_response = api_instance.emails_post(email_message_data)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling EmailsApi->emails_post: %sn" % e)

Code Samples

Upon getting despatched the take a look at e mail with our API, you can begin sending transactional emails and carry out varied operations immediately by your app. Now we have ready for you some code samples for many important actions, together with managing contacts and lists, creating templates, and sending and monitoring emails. Because of them, your integration with our API will likely be a good simpler and extra nice expertise.
In case any of the next snippets are modified within the meantime, the latest model of this API Library is accessible on GitHub.

Managing Contacts

On this part, we are going to stroll you thru managing the contacts in your account utilizing the Python library.

Add Contacts

So as to add contacts to your account, you’ll need Entry Degree: ModifyContacts.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import contacts_api
from ElasticEmail.mannequin.contact_status import ContactStatus
from ElasticEmail.mannequin.contact_payload import ContactPayload
from pprint import pprint

Generate and use your API key (bear in mind to examine the required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an API shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of ContactsApi that will likely be used so as to add contacts.

Copied!

   api_instance = contacts_api.ContactsApi(api_client)

Create an array with new contacts.

You’ll be able to cross an array with as much as 1000 contacts.

The E mail discipline is obligatory, the remainder is non-obligatory.

Discover out extra by checking our API’s documentation.

Copied!

   contact_payload = [
        ContactPayload(
            email="johnsmith@domain.com",
            status=ContactStatus("Active"),
            first_name="John",
            last_name="Smith",
        ),
    ]

Specify an current listing title in choices, in any other case, contacts will likely be added to all contacts.

Copied!

  list_names = [
        "New list",
    ]

Use attempt & besides block to name contacts_post technique from the API so as to add contacts:

   attempt:

Copied!

api_response = api_instance.contacts_post(contact_payload, listnames=list_names)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling ContactsApi->contacts_post: %sn" % e)

Delete contacts

If you wish to delete a contact, you’ll need Entry Degree: ModifyContacts.

To delete a contact, put the next code into your file.

Load library utilizing the next line:

Copied!
import ElasticEmail
from ElasticEmail.api import contacts_api
from ElasticEmail.mannequin.emails_payload import EmailsPayload

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an API shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of ContactsApi that will likely be used to delete contacts.

Copied!

   api_instance = contacts_api.ContactsApi(api_client)

Create an object with an array of contacts to delete.

Discover out extra by checking our API’s documentation.

Copied!

   emails_payload = EmailsPayload(
        emails=["johnsmith@domain.com"],
    )

Use attempt & besides block to name contacts_delete_post technique from the API to delete contacts:

   attempt:

Copied!

        api_instance.contacts_delete_post(emails_payload)
        print("Contacts deleted.")
    besides ElasticEmail.ApiException as e:
        print("Exception when calling ContactsApi->contacts_delete_post: %sn" % e)

Add Contacts

If you wish to import contacts to your account utilizing the Python library, you’ll need Entry Degree: ModifyContacts.

To import contacts, put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import contacts_api

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an API shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of ContactsApi that will likely be used to add contacts.

Copied!

   api_instance = contacts_api.ContactsApi(api_client)

Create choices

  • file encoding
  • optionally, an inventory title to which contacts must be added, in any other case contacts will likely be added to all contacts.
Copied!

   list_name = "Finest contacts"
    encoding_name = "utf-8"

The best CSV file requires just one column E mail, eg.:

Copied!

E mail
john@johnsmith.com

Discover out extra by checking our API’s documentation.

Load file

Copied!

file = open('./recordsdata/contacts.csv', 'rb')

Use attempt & besides block to name contacts_import_post technique from the API to add contacts:

   attempt:

Copied!

        api_instance.contacts_import_post(list_name=list_name, encoding_name=encoding_name, file=file)
        print("Contacts uploaded.")
    besides ElasticEmail.ApiException as e:
        print("Exception when calling ContactsApi->contacts_import_post: %sn" % e)

Export Contacts

If you wish to export the chosen contact to a downloadable file, you’ll need Entry Degree: Export.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import contacts_api
from ElasticEmail.mannequin.export_file_formats import ExportFileFormats
from ElasticEmail.mannequin.compression_format import CompressionFormat
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an API shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of ContactsApi that will likely be used to create a file with exported contacts.

Copied!

   api_instance = contacts_api.ContactsApi(api_client)

Create choices variables:

  • file_format – specify format by which file must be created, choices are: “Csv” “Xml” “Json”.
  • emails – choose contacts to export by offering array of emails
  • file_name – you may specify file title of your alternative

Different choices:

  • rule – eg. rule=Standingpercent20=%20Engaged – Question used for filtering
  • compression_format – “None” or “Zip”

Discover out extra by checking our API’s documentation.

Copied!

   file_format = ExportFileFormats("Csv")
    emails = [
        "johnsmith@domain.com",
    ]
    compression_format = CompressionFormat("None")
    file_name = "exported.csv"

Use attempt & besides block to name contacts_export_post technique from the API to export contacts:

   attempt:

Copied!

        api_response = api_instance.contacts_export_post(file_format=file_format, emails=emails, compression_format=compression_format, file_name=file_name)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling ContactsApi->contacts_export_post: %sn" % e)

Managing Lists

On this part, we are going to stroll you thru managing your contact listing in your account utilizing the Python library.

Add Record

So as to add an inventory, you’ll need Entry Degree: ModifyContacts.

Put the next code into your file. 

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import lists_api
from ElasticEmail.mannequin.list_payload import ListPayload
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an API shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of ListsApi that will likely be used to create a brand new listing.

Copied!

   api_instance = lists_api.ListsApi(api_client)

Create an object with particulars in regards to the new listing. Solely ListName is required.

You can even outline if to permit unsubscription from the listing and cross an e mail array of current contacts in your account so as to add them to the listing throughout listing creation.

Discover out extra by checking our API’s documentation.

Copied!

   list_payload = ListPayload(
        list_name="Finest contacts",
        allow_unsubscribe=True,
        emails=[
            "johnsmith@domain.com",
        ],
    )

Use attempt & besides block to name lists_post technique from the API to create an inventory:

   attempt:

Copied!

        api_response = api_instance.lists_post(list_payload)
        pprint(api_response)

    besides ElasticEmail.ApiException as e:

Copied!

        print("Exception when calling ListsApi->lists_post: %sn" % e)

Load Record

To load an inventory, you’ll need Entry Degree: ViewContacts.

Put the next code into your file. 

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import lists_api
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an API shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of ListsApi that will likely be used to load the listing.

Copied!

   api_instance = lists_api.ListsApi(api_client)

The one factor wanted is an inventory title.

Discover out extra by checking our API’s documentation.

Copied!

   title = "Finest contacts"

Use attempt & besides block to name lists_by_name_get technique from the API to fetch an inventory:

   attempt:

Copied!

        api_response = api_instance.lists_by_name_get(title)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling ListsApi->lists_by_name_get: %sn" % e)

Delete Record

To take away a contact listing out of your account, you’ll need Entry Degree: ModifyContacts.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import lists_api

Generate and use your API key (bear in mind to examine the required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an API shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of ListsApi that will likely be used to delete the listing.

Copied!

   api_instance = lists_api.ListsApi(api_client)

The one factor wanted is an inventory title.

Discover out extra by checking our API’s documentation.

Copied!

   title = "Finest contacts"

Use attempt & besides block to name lists_by_name_delete technique from the API to delete an inventory:

   attempt:

Copied!

        api_instance.lists_by_name_delete(title)
        print("Record deleted.")
    besides ElasticEmail.ApiException as e:
        print("Exception when calling ListsApi->lists_by_name_delete: %sn" % e)

Creating Templates

An e mail template is a physique of e mail ready and saved below a given title. On this part, you’ll get to know how one can add and cargo e mail templates.

Add Template

So as to add a template, you’ll need Entry Degree: ModifyTemplates.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import templates_api
from ElasticEmail.mannequin.body_part import BodyPart
from ElasticEmail.mannequin.body_content_type import BodyContentType
from ElasticEmail.mannequin.template_payload import TemplatePayload
from ElasticEmail.mannequin.template_scope import TemplateScope
from pprint import pprint

Generate and use your API key (bear in mind to examine the required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of TemplatesApi that will likely be used to create a brand new template.

Copied!

    api_instance = templates_api.TemplatesApi(api_client)

Create an object with particulars in regards to the new template:

  • Title – the title of your template by which it may be recognized and used
  • Topic – specify the default topic for this template
  • Physique – specify precise physique content material eg. in HTML, PlainText or each
  • TemplateScope – specify scope, “Private” template will not be shared, “International” template could be shared together with your sub accounts.

Discover out extra by checking our API’s documentation.

Copied!

    template_payload = TemplatePayload(
        title="My new template",
        topic="Default topic",
        physique=[
            BodyPart(
                content_type=BodyContentType("HTML"),
                content="My template",
                charset="utf-8",
            ),
        ],
        template_scope=TemplateScope("Private"),
    )

Use attempt & besides block to name templates_post technique from the API to create a template:

Copied!

    attempt:
        api_response = api_instance.templates_post(template_payload)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling TemplatesApi->templates_post: %sn" % e)

Load Template

To load current template particulars, you’ll need Entry Degree: ViewTemplates.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import templates_api
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of TemplatesApi that will likely be used to load current template in your account.

Copied!

    api_instance = templates_api.TemplatesApi(api_client)

To load a template, you’ll want to specify its title:

Discover out extra by checking our API’s documentation.

Copied!

    template_payload = TemplatePayload(
        title="My new template",
        topic="Default topic",
        physique=[
            BodyPart(
                content_type=BodyContentType("HTML"),
                content="My template",
                charset="utf-8",
            ),
        ],
        template_scope=TemplateScope("Private"),
    )

Use attempt & besides block to name templates_by_name_get technique from the API to load a template:

Copied!

    attempt:
        api_response = api_instance.templates_by_name_get(title)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling TemplatesApi->templates_by_name_get: %sn" % e)

Delete Template

To delete a template, you’ll need Entry Degree: ModifyTemplates.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import templates_api

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of TemplatesApi that will likely be used to delete current template out of your account.

Copied!

    api_instance = templates_api.TemplatesApi(api_client)

To load a template, you’ll want to specify its title:

Discover out extra by checking our API’s documentation.

Copied!

    title = "My template"

Use attempt & besides block to name templates_by_name_delete technique from the API to delete a template:

Copied!

    attempt:
        api_instance.templates_by_name_delete(title)
        print("Template deleted.")
    besides ElasticEmail.ApiException as e:
        print("Exception when calling TemplatesApi->templates_by_name_delete: %sn" % e)

Sending Emails

On this part, we are going to inform you how one can begin sending emails utilizing the Python library. You’ll get to know how one can ship bulk and transactional emails.

Ship Transactional Emails

To ship transactional emails, you’ll need Entry Degree: SendHttp.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import emails_api
from ElasticEmail.mannequin.email_content import EmailContent
from ElasticEmail.mannequin.body_part import BodyPart
from ElasticEmail.mannequin.body_content_type import BodyContentType
from ElasticEmail.mannequin.transactional_recipient import TransactionalRecipient
from ElasticEmail.mannequin.email_transactional_message_data import EmailTransactionalMessageData
from pprint import pprint

Generate and use your API key (bear in mind to examine the required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of EmailsApi that will likely be used to ship a transactional e mail.

Copied!

    api_instance = emails_api.EmailsApi(api_client)

First, you’ll want to specify e mail particulars:

e mail recipients:

this instance demonstrates merge fields utilization, for every recipient {title} will likely be modified to the recipient’s title

e mail content material:

physique elements – in HTML, PlainText or in each from e mail – it must be your validated e mail tackle e mail topic

Discover out extra by checking our API’s documentation.

Copied!

    email_transactional_message_data = EmailTransactionalMessageData(
        recipients=TransactionalRecipient(
            to=[
                "johnsmith@domain.com",
            ],
        ),
        content material=EmailContent(
            physique=[
                BodyPart(
                    content_type=BodyContentType("HTML"),
                    content="Mail content.",
                    charset="utf-8",
                ),
                BodyPart(
                    content_type=BodyContentType("PlainText"),
                    content="Mail content.",
                    charset="utf-8",
                ),
            ],
            _from="myemail@area.com",
            reply_to="myemail@area.com",
            topic="Instance transactional e mail",
        ),
    ) 

Use attempt & besides block to name emails_transactional_post technique from the API to ship an e mail:

Copied!

    attempt:
        api_response = api_instance.emails_transactional_post(email_transactional_message_data)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling EmailsApi->emails_transactional_post: %sn" % e)

Ship Bulk Emails

Put the next code to your file to ship a bulk e mail, which means a single e mail despatched to a big group without delay.

You’ll need Entry Degree: SendHttp.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import emails_api
from ElasticEmail.mannequin.email_content import EmailContent
from ElasticEmail.mannequin.body_part import BodyPart
from ElasticEmail.mannequin.body_content_type import BodyContentType
from ElasticEmail.mannequin.email_recipient import EmailRecipient
from ElasticEmail.mannequin.email_message_data import EmailMessageData
from pprint import pprint

Generate and use your API key (bear in mind to examine the required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of EmailsApi that will likely be used to ship a bulk e mail.

Copied!

    api_instance = emails_api.EmailsApi(api_client)

First, you’ll want to specify e mail particulars:

e mail recipients:

this instance demonstrates merge fields utilization, for every recipient {title} will likely be modified to the recipient’s title

e mail content material:

physique elements – in HTML, PlainText or in each from e mail – it must be your validated e mail tackle e mail topic

Discover out extra by checking our API’s documentation.

Copied!

    email_message_data = EmailMessageData(
        recipients=[
            EmailRecipient(
                email="johnsmith@domain.com",
                fields={
                    "name": "John",
                },
            ),
        ],
        content material=EmailContent(
            physique=[
                BodyPart(
                    content_type=BodyContentType("HTML"),
                    content="Hi {name}!",
                    charset="utf-8",
                ),
                BodyPart(
                    content_type=BodyContentType("PlainText"),
                    content="Hi {name}!",
                    charset="utf-8",
                ),
            ],
            _from="myemail@area.com",
            reply_to="myemail@area.com",
            topic="Instance e mail",
        ),
    )

Use attempt & besides block to name emails_post technique from the API to ship an e mail:

Copied!

    attempt:
        api_response = api_instance.emails_post(email_message_data)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling EmailsApi->emails_post: %sn" % e)

Managing Campaigns

Add Marketing campaign

So as to add your first marketing campaign utilizing the Python library, you’ll need Entry Degree: ModifyCampaigns. Thoughts that when utilizing Elastic E mail, while you ship an e mail to any group of contacts, we name it a “marketing campaign”.

So as to add a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import campaigns_api
from ElasticEmail.mannequin.marketing campaign import Marketing campaign
from ElasticEmail.mannequin.campaign_recipient import CampaignRecipient
from ElasticEmail.mannequin.campaign_status import CampaignStatus
from ElasticEmail.mannequin.campaign_template import CampaignTemplate
from pprint import pprint

Generate and use your API key (bear in mind to examine the required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of CampaignsApi that will likely be used to create a marketing campaign.

Copied!

    api_instance = campaigns_api.CampaignsApi(api_client)

Title: defines the marketing campaign title by which you’ll be able to determine it later

Recipients: outline your viewers

Content material: outline your message particulars

Standing: outline the standing by which the marketing campaign must be created

Discover out extra by checking our API’s documentation.

Ship will likely be triggered instantly or postponed, relying on given choices. As a result of we outline Standing as Draft, so on this case it is going to be postponed and marketing campaign will likely be added to drafts.

Copied!

    marketing campaign = Marketing campaign(
        content material=[
            CampaignTemplate(
                _from="karol.szczycinski@elasticemail.com",
                reply_to="karol.szczycinski@elasticemail.com",
                subject="Hello",
                template_name="hello_template",
            ),
        ],
        title="hiya marketing campaign",
        standing=CampaignStatus("Draft"),
        recipients=CampaignRecipient(
            list_names=[
                "my list name",
            ],
        ),
    )

Use attempt & besides block to name campaigns_post technique from the API to create a marketing campaign:

Copied!

    attempt:
        api_response = api_instance.campaigns_post(marketing campaign)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling CampaignsApi->campaigns_post: %sn" % e)

Delete Marketing campaign

To delete an current marketing campaign, you’ll need Entry Degree: ModifyCampaigns. Thoughts that when utilizing Elastic E mail, while you ship an e mail to any group of contacts, we name it a “marketing campaign”.

To delete a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import campaigns_api

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of CampaignsApi that will likely be used to delete a marketing campaign.

Copied!

    api_instance = campaigns_api.CampaignsApi(api_client)

The one factor you’ll want to specify is a marketing campaign title

Discover out extra by checking our API’s documentation.

Copied!

    title = "hiya marketing campaign"

Use attempt & besides block to name campaigns_by_name_delete technique from the API to delete a marketing campaign:

Copied!

    attempt:
        api_instance.campaigns_by_name_delete(title)
        print("Marketing campaign deleted.")
    besides ElasticEmail.ApiException as e:
        print("Exception when calling CampaignsApi->campaigns_by_name_delete: %sn" % e)

Load Marketing campaign

To load particulars about an current marketing campaign in your account utilizing the py library, you’ll need Entry Degree: ViewCampaigns. Thoughts that when utilizing Elastic E mail, while you ship an e mail to any group of contacts, we name it a “marketing campaign”.

To load a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import campaigns_api
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of CampaignsApi that will likely be used to load a marketing campaign.

Copied!

   api_instance = campaigns_api.CampaignsApi(api_client)

The one factor you’ll want to specify is a marketing campaign title

Discover out extra by checking our API’s documentation. 

Copied!

    title = "hiya marketing campaign"

Use attempt & besides block to name campaigns_by_name_get technique from the API to fetch marketing campaign particulars:

Copied!

    attempt:
        api_response = api_instance.campaigns_by_name_get(title)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling CampaignsApi->campaigns_by_name_get: %sn" % e)

Replace Marketing campaign

To replace current campaigns in your account utilizing the Python library, you’ll need Entry Degree: ModifyCampaigns. Thoughts that when utilizing Elastic E mail, while you ship an e mail to any group of contacts, we name it a “marketing campaign”.

To replace a marketing campaign, put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import campaigns_api
from ElasticEmail.mannequin.marketing campaign import Marketing campaign
from ElasticEmail.mannequin.campaign_recipient import CampaignRecipient
from ElasticEmail.mannequin.campaign_status import CampaignStatus
from ElasticEmail.mannequin.campaign_template import CampaignTemplate
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of CampaignsApi that will likely be used to replace a marketing campaign.

Copied!

    api_instance = campaigns_api.CampaignsApi(api_client)

Create an instance marketing campaign object:

  • Title: defines marketing campaign title by which you’ll be able to determine it later
  • Recipients: outline your viewers
  • Conent: outline your message particulars
  • Standing: outline standing by which marketing campaign must be created

Discover out extra by checking our API’s documentation. 

Ship will likely be triggered instantly or postponed, relying on given choices. As a result of we outline Standing as Draft, so on this case it is going to be postponed and marketing campaign will likely be added to drafts.

Copied!

    marketing campaign = Marketing campaign(
        content material=[
            CampaignTemplate(
                _from="karol.szczycinski@elasticemail.com",
                reply_to="karol.szczycinski@elasticemail.com",
                subject="Hello",
                template_name="hello_template",
            ),
        ],
        title="hiya marketing campaign replace",
        standing=CampaignStatus("Draft"),
        recipients=CampaignRecipient(
            list_names=[
                "my list name",
            ],
        ),
    )

Use attempt & besides block to name campaigns_by_name_put technique from the API to replace a marketing campaign:

Copied!

    attempt:
        api_response = api_instance.campaigns_by_name_put(title, marketing campaign)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling CampaignsApi->campaigns_by_name_put: %sn" % e)

Monitoring

On this part, we are going to stroll you thru the steps of managing actions associated to e mail monitoring. You’ll get to know how one can load supply and marketing campaign statistics out of your account utilizing the Python library.

Load Statistics

To load supply statistics out of your account, you’ll need Entry Degree: ViewReports.

Put the next code into your file.

Load library utilizing the road:

Copied!

from datetime import datetime
import ElasticEmail
from ElasticEmail.api import statistics_api
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of StatisticsApi that will likely be used to get primary ship statistics.

Copied!

    api_instance = statistics_api.StatisticsApi(api_client)

First, you’ll want to specify a date vary:

  • from date
  • so far – non-obligatory

Discover out extra by checking our API’s documentation. 

Copied!

    _from = datetime(2022,1,1,00,00,00)
    to = datetime(2022,1,30,00,00,00)

Use attempt & besides block to name statistics_get technique from the API to load a statistics with solely from date given:

Copied!

    attempt:
        api_response = api_instance.statistics_get(_from)
        print("From %s" % _from)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling StatisticsApi->statistics_get: %sn" % e)
Copied!

    attempt:
        api_response = api_instance.statistics_get(_from, to=to)
        print(f"nFrom {_from} To {to}")
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling StatisticsApi->statistics_get: %sn" % e)

Load Channels Statistics

To load statistics for every channel out of your account, you’ll need Entry Degree: ViewChannels.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import statistics_api
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of StatisticsApi that will likely be used to get primary ship statistics.

Copied!

    api_instance = statistics_api.StatisticsApi(api_client)

Channels statistics reponse is paginated you’ll want to specfiy pagination choices:

  • restrict – most returned gadgets, restrict = 0 means to return all the pieces until the top of the listing
  • offset – what number of gadgets must be skipped from begging

Eg. to return second web page of parts paginated 20 parts per web page specify pagination choices as follows

Copied!

    restrict = 20
    offset = 20

Discover out extra by checking our API’s documentation.

Let’s fetch first 100 channels:

Copied!

    restrict = 100
    offset = 0 

Use attempt & besides block to name statistics_channels_get technique from the API to fetch statistics:

Copied!

    attempt:
        api_response = api_instance.statistics_channels_get(restrict=restrict, offset=offset)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling StatisticsApi->statistics_campaigns_get: %sn" % e)

Load Campaigns Stats

To load statistics for every e mail marketing campaign out of your account, you’ll need Entry Degree: ViewChannels.

Put the next code into your file.

Load library utilizing the next line:

Copied!

import ElasticEmail
from ElasticEmail.api import statistics_api
from pprint import pprint

Generate and use your API key (bear in mind to examine a required entry degree).

Defining the host is non-obligatory and defaults to https://api.elasticemail.com/v4

Copied!

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

Cross configuration to an api shopper and make it occasion accessible below api_client title:

Copied!

with ElasticEmail.ApiClient(configuration) as api_client:

Create an occasion of StatisticsApi that will likely be used to get primary ship statistics.

Copied!

    api_instance = statistics_api.StatisticsApi(api_client)

Campaigns statistics reponse is paginated you’ll want to specfiy pagination choices:

  • restrict – most returned gadgets, restrict = 0 means to return all the pieces until the top of the listing
  • offset – what number of gadgets must be skipped from begging

Eg. to return second web page of parts paginated 20 parts per web page specify pagination choices as follows

Copied!

    restrict = 20
    offset = 20

Discover out extra by checking our API’s documentation.

Let’s fetch first 100 campaigns:

Copied!

    restrict = 100
    offset = 0 

Use attempt & besides block to name statistics_campaigns_get technique from the API to fetch statistics:

Copied!

    attempt:
        api_response = api_instance.statistics_campaigns_get(restrict=restrict, offset=offset)
        pprint(api_response)
    besides ElasticEmail.ApiException as e:
        print("Exception when calling StatisticsApi->statistics_campaigns_get: %sn" % e)

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here