From 3b0f0507aca7975d555e00f51b7b1133b6e9710e Mon Sep 17 00:00:00 2001 From: taimurkhan-1999 Date: Mon, 11 May 2026 15:45:04 +0500 Subject: [PATCH] updated python library examples --- examples/functions/addCampaign.md | 132 +++-------------- examples/functions/addCampaign.py | 13 +- examples/functions/addContacts.md | 123 +++------------ examples/functions/addContacts.py | 9 +- examples/functions/addList.md | 109 +++----------- examples/functions/addList.py | 12 +- examples/functions/addTemplate.md | 120 +++------------ examples/functions/addTemplate.py | 14 +- examples/functions/deleteCampaign.md | 90 ++--------- examples/functions/deleteCampaign.py | 5 +- examples/functions/deleteContacts.md | 94 +++--------- examples/functions/deleteContacts.py | 5 +- examples/functions/deleteList.md | 91 ++---------- examples/functions/deleteList.py | 5 +- examples/functions/deleteTemplate.md | 92 +++--------- examples/functions/deleteTemplate.py | 5 +- examples/functions/exportContacts.md | 124 ++++------------ examples/functions/exportContacts.py | 19 +-- examples/functions/loadCampaign.md | 93 +++--------- examples/functions/loadCampaign.py | 5 +- examples/functions/loadCampaignsStats.md | 111 ++------------ examples/functions/loadCampaignsStats.py | 10 +- examples/functions/loadChannelsStats.md | 112 ++------------ examples/functions/loadChannelsStats.py | 10 +- examples/functions/loadList.md | 108 ++++---------- examples/functions/loadList.py | 16 +- examples/functions/loadStatistics.md | 113 +++----------- examples/functions/loadStatistics.py | 7 +- examples/functions/loadTemplate.md | 120 +++++---------- examples/functions/loadTemplate.py | 23 ++- examples/functions/sendBulkEmails.md | 140 +++--------------- examples/functions/sendBulkEmails.py | 13 +- examples/functions/sendTransactionalEmails.md | 139 +++-------------- examples/functions/sendTransactionalEmails.py | 14 +- .../sendTransactionalEmailsWithAttachment.md | 72 +++++++++ .../sendTransactionalEmailsWithAttachment.py | 117 ++++++++------- examples/functions/updateCampaign.md | 136 +++-------------- examples/functions/updateCampaign.py | 16 +- examples/functions/uploadContacts.md | 126 ++++------------ examples/functions/uploadContacts.py | 21 +-- 40 files changed, 645 insertions(+), 1939 deletions(-) create mode 100644 examples/functions/sendTransactionalEmailsWithAttachment.md diff --git a/examples/functions/addCampaign.md b/examples/functions/addCampaign.md index af59242..fb1f10d 100755 --- a/examples/functions/addCampaign.md +++ b/examples/functions/addCampaign.md @@ -1,118 +1,29 @@ # Add Campaign -This guide will walk you through the process of creating your first campaign 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/addCampaign.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api -from ElasticEmail.model.campaign import Campaign -from ElasticEmail.model.campaign_recipient import CampaignRecipient -from ElasticEmail.model.campaign_status import CampaignStatus -from ElasticEmail.model.campaign_template import CampaignTemplate +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 - -```python +# 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 create a campaign. - -```python - api_instance = campaigns_api.CampaignsApi(api_client) -``` - -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/campaignsPost - -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. - - -```python - campaign = Campaign( - Content=[ - CampaignTemplate( - From="example@email.test", - ReplyTo="example@email.test", - Subject="Hello", - TemplateName="hello_template", - ), - ], - Name="hello campaign", - Status=CampaignStatus("Draft"), - Recipients=CampaignRecipient( - list_names=[ - "my list name", - ], - ), - ) -``` - -Use try & except block to call `campaigns_post` method from the API to create a campaign: - -```python - try: - api_response = api_instance.campaigns_post(body = campaign) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling CampaignsApi->campaigns_post: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api -from ElasticEmail.model.campaign import Campaign -from ElasticEmail.model.campaign_recipient import CampaignRecipient -from ElasticEmail.model.campaign_status import CampaignStatus -from ElasticEmail.model.campaign_template import CampaignTemplate -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Add Campaign +Example api call that creates a new campaign. +Send will be triggered immediately or postponed, depending on given options. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = campaigns_api.CampaignsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.CampaignsApi(api_client) campaign = Campaign( Content=[ @@ -130,16 +41,19 @@ with ElasticEmail.ApiClient(configuration) as api_client: "my list name", ], ), - ) + ) # Campaign | JSON representation of a campaign + # example passing only required values which don't have defaults set try: - api_response = api_instance.campaigns_post(body = campaign) + # Add Campaign + api_response = api_instance.campaigns_post(campaign=campaign) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling CampaignsApi->campaigns_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 addCampaign.py ``` diff --git a/examples/functions/addCampaign.py b/examples/functions/addCampaign.py index a4587f9..a576693 100755 --- a/examples/functions/addCampaign.py +++ b/examples/functions/addCampaign.py @@ -1,9 +1,8 @@ import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api -from ElasticEmail.model.campaign import Campaign -from ElasticEmail.model.campaign_recipient import CampaignRecipient -from ElasticEmail.model.campaign_status import CampaignStatus -from ElasticEmail.model.campaign_template import CampaignTemplate +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 @@ -19,7 +18,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = campaigns_api.CampaignsApi(api_client) + api_instance = ElasticEmail.CampaignsApi(api_client) campaign = Campaign( Content=[ @@ -42,7 +41,7 @@ # example passing only required values which don't have defaults set try: # Add Campaign - api_response = api_instance.campaigns_post(body = campaign) + api_response = api_instance.campaigns_post(campaign=campaign) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling CampaignsApi->campaigns_post: %s\n" % e) diff --git a/examples/functions/addContacts.md b/examples/functions/addContacts.md index 0580d24..399f586 100755 --- a/examples/functions/addContacts.md +++ b/examples/functions/addContacts.md @@ -1,109 +1,28 @@ # Add Contacts -This guide will walk you through the process of adding new contacts to your account using the Python library. - -*Required Access Level: ModifyContacts* - -## What's contact? -When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/addContacts.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.contact_status import ContactStatus -from ElasticEmail.model.contact_payload import ContactPayload +from ElasticEmail.models.contact_status import ContactStatus +from ElasticEmail.models.contact_payload import ContactPayload 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 -```python +# 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 ContactsApi that will be used to add contacts. - -```python - api_instance = contacts_api.ContactsApi(api_client) -``` - -Create an array with new contacts. - -You can pass an array with up to 1000 contacts. - -The `Email` field is mandatory, the rest is optional. - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/contactsPost - - -```python - contact_payload = [ - ContactPayload( - Email="johnsmith@domain.com", - Status=ContactStatus("Active"), - FirstName="John", - LastName="Smith", - ), - ] -``` - -Specify an existing list name in options, otherwise contacts will be added to all contacts. - -```python - list_names = [ - "New list", - ] -``` - - -Use try & except block to call `contacts_post` method from the API to add contacts: - -```python - try: - api_response = api_instance.contacts_post(body = contact_payload, query_params = {'listnames': list_names}) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling ContactsApi->contacts_post: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.contact_status import ContactStatus -from ElasticEmail.model.contact_payload import ContactPayload -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Add contacts +Example api call that adds new contacts. +Pass array with contact details to add up to 1000 contacts. +Specify a list name in options or add to all contacts. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = contacts_api.ContactsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.ContactsApi(api_client) contact_payload = [ ContactPayload( Email="johnsmith@domain.com", @@ -111,20 +30,22 @@ with ElasticEmail.ApiClient(configuration) as api_client: FirstName="John", LastName="Smith", ), - ] + ] # [ContactPayload] list_names = [ "New list", - ] + ] # [str] | Names of lists to which the uploaded contacts should be added to (optional) try: - api_response = api_instance.contacts_post(body = contact_payload, query_params = {'listnames': list_names}) + # Add Contact + api_response = api_instance.contacts_post(contact_payload=contact_payload, listnames=list_names) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling ContactsApi->contacts_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 addContacts.py ``` diff --git a/examples/functions/addContacts.py b/examples/functions/addContacts.py index 46fcc67..1c55f99 100755 --- a/examples/functions/addContacts.py +++ b/examples/functions/addContacts.py @@ -1,7 +1,6 @@ import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.contact_status import ContactStatus -from ElasticEmail.model.contact_payload import ContactPayload +from ElasticEmail.models.contact_status import ContactStatus +from ElasticEmail.models.contact_payload import ContactPayload from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -18,7 +17,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = contacts_api.ContactsApi(api_client) + api_instance = ElasticEmail.ContactsApi(api_client) contact_payload = [ ContactPayload( Email="johnsmith@domain.com", @@ -34,7 +33,7 @@ try: # Add Contact - api_response = api_instance.contacts_post(body = contact_payload, query_params = {'listnames': list_names}) + api_response = api_instance.contacts_post(contact_payload=contact_payload, listnames=list_names) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling ContactsApi->contacts_post: %s\n" % e) diff --git a/examples/functions/addList.md b/examples/functions/addList.md index 8c4ce33..663bee6 100755 --- a/examples/functions/addList.md +++ b/examples/functions/addList.md @@ -1,95 +1,26 @@ # Add List -This guide will walk you through the process of adding a new list of contacts to your account using the Python library. - -*Required Access Level: ModifyContacts* - -## What's a list? -When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. Segments add contacts automatically when specfied conditions are met, and contacts on lists are managed manually. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/addList.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import lists_api -from ElasticEmail.model.list_payload import ListPayload +from ElasticEmail.models.list_payload import ListPayload 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 - -```python +# 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 ListsApi that will be used to create a new list. - -```python - api_instance = lists_api.ListsApi(api_client) -``` - -Create an object with details about a new list. Only `ListName` is required. - -You can also define if to allow unsubscription from list and pass an emails array of existing contacts on your account to add them to list during list creation. - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/listsPost - - -```python - list_payload = ListPayload( - ListName="Best contacts", - AllowUnsubscribe=True, - Emails=[ - "johnsmith@domain.com", - ], - ) -``` - -Use try & except block to call `lists_post` method from the API to create a list: - -```python - try: - api_response = api_instance.lists_post(body = list_payload) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling ListsApi->lists_post: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import lists_api -from ElasticEmail.model.list_payload import ListPayload -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Add list +Example api call that creates a new contacts list. +Emails – An array of existing contact emails that should be added to this list. Leave empty for all contacts +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = lists_api.ListsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.ListsApi(api_client) list_payload = ListPayload( ListName="Best contacts", @@ -97,16 +28,20 @@ with ElasticEmail.ApiClient(configuration) as api_client: Emails=[ "johnsmith@domain.com", ], - ) + ) # ListPayload | try: - api_response = api_instance.lists_post(body = list_payload) + api_response = api_instance.lists_post(list_payload=list_payload) pprint(api_response) except ElasticEmail.ApiException as e: - print("Exception when calling ListsApi->lists_post: %s\n" % e) + if e.status == 400: + print("List already exists. Skipping.") + else: + print("Exception when calling ListsApi->lists_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 addList.py ``` diff --git a/examples/functions/addList.py b/examples/functions/addList.py index ec1cbf9..cfce100 100755 --- a/examples/functions/addList.py +++ b/examples/functions/addList.py @@ -1,6 +1,5 @@ import ElasticEmail -from ElasticEmail.apis.tags import lists_api -from ElasticEmail.model.list_payload import ListPayload +from ElasticEmail.models.list_payload import ListPayload from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -16,7 +15,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = lists_api.ListsApi(api_client) + api_instance = ElasticEmail.ListsApi(api_client) list_payload = ListPayload( ListName="Best contacts", @@ -27,7 +26,10 @@ ) # ListPayload | try: - api_response = api_instance.lists_post(body = list_payload) + api_response = api_instance.lists_post(list_payload=list_payload) pprint(api_response) except ElasticEmail.ApiException as e: - print("Exception when calling ListsApi->lists_post: %s\n" % e) + if e.status == 400: + print("List already exists. Skipping.") + else: + print("Exception when calling ListsApi->lists_post: %s\n" % e) diff --git a/examples/functions/addTemplate.md b/examples/functions/addTemplate.md index 145c6f3..3525f71 100755 --- a/examples/functions/addTemplate.md +++ b/examples/functions/addTemplate.md @@ -1,108 +1,29 @@ # Add Template -This guide will walk you through the process of adding a new template to your account using the Python library. - -*Required Access Level: ModifyTemplates* - -## What's a tempalte? -When using Elastic Email you send emails to your contacts. A single template is a body of email prepared and saved under given name. Till it's deleted it can be reused to send any number of emails. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/addTemplate.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import templates_api -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.template_payload import TemplatePayload -from ElasticEmail.model.template_scope import TemplateScope +from ElasticEmail.models.template_payload import TemplatePayload +from ElasticEmail.models.body_part import BodyPart +from ElasticEmail.models.body_content_type import BodyContentType +from ElasticEmail.models.template_scope import TemplateScope 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 - -```python +# 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 TemplatesApi that will be used to create a new template. - -```python - api_instance = templates_api.TemplatesApi(api_client) -``` - -Create an object with details about new template: -- Name – name of your template by which it can be identified and used -- Subject – specify default subject for this template -- Body – specify acctual body content eg. in HTML, PlainText or both -- TemplateScope – specify scope, "Personal" template won't be shared, "Global" template can be shared with your sub accounts. - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/templatesPost - - -```python - template_payload = TemplatePayload( - Name="My new template", - Subject="Default subject", - Body=[ - BodyPart( - ContentType=BodyContentType("HTML"), - Content="My template", - Charset="utf-8", - ), - ], - TemplateScope=TemplateScope("Personal"), - ) -``` - -Use try & except block to call `templates_post` method from the API to create a template: - -```python - try: - api_response = api_instance.templates_post(body = template_payload) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling TemplatesApi->templates_post: %s\n" % e) -``` - - -## The whole code to copy and paste: -```python -import ElasticEmail -from ElasticEmail.apis.tags import templates_api -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.template_payload import TemplatePayload -from ElasticEmail.model.template_scope import TemplateScope -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Add Template +Example api call that adds a new template. +TemplateScope: "Personal" or "Global" +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = templates_api.TemplatesApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.TemplatesApi(api_client) template_payload = TemplatePayload( Name="My new template", @@ -115,16 +36,17 @@ with ElasticEmail.ApiClient(configuration) as api_client: ), ], TemplateScope=TemplateScope("Personal"), - ) + ) # TemplatePayload | try: - api_response = api_instance.templates_post(body = template_payload) + api_response = api_instance.templates_post(template_payload=template_payload) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling TemplatesApi->templates_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 addTemplate.py ``` diff --git a/examples/functions/addTemplate.py b/examples/functions/addTemplate.py index 36cd303..75805dd 100755 --- a/examples/functions/addTemplate.py +++ b/examples/functions/addTemplate.py @@ -1,10 +1,8 @@ import ElasticEmail -from ElasticEmail.apis.tags import templates_api -from ElasticEmail.model.template import Template -from ElasticEmail.model.template_payload import TemplatePayload -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.template_scope import TemplateScope +from ElasticEmail.models.template_payload import TemplatePayload +from ElasticEmail.models.body_part import BodyPart +from ElasticEmail.models.body_content_type import BodyContentType +from ElasticEmail.models.template_scope import TemplateScope from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -20,7 +18,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = templates_api.TemplatesApi(api_client) + api_instance = ElasticEmail.TemplatesApi(api_client) template_payload = TemplatePayload( Name="My new template", @@ -36,7 +34,7 @@ ) # TemplatePayload | try: - api_response = api_instance.templates_post(body = template_payload) + api_response = api_instance.templates_post(template_payload=template_payload) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling TemplatesApi->templates_post: %s\n" % e) diff --git a/examples/functions/deleteCampaign.md b/examples/functions/deleteCampaign.md index c8e8b4e..2f33c93 100755 --- a/examples/functions/deleteCampaign.md +++ b/examples/functions/deleteCampaign.md @@ -1,94 +1,36 @@ # Delete Campaign -This guide will walk you through steps to delete an existing campaign from your account. - -*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". - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/deleteCampaign.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api -``` - -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 -```python +# 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 delete a campaign. - -```python - api_instance = campaigns_api.CampaignsApi(api_client) -``` - -The only thing you need to specify is a campaign name -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/campaignsByNameDelete - - -```python - name = "hello campaign" -``` - -Use try & except block to call `campaigns_by_name_delete` method from the API to delete a campaign: - -```python - try: - api_instance.campaigns_by_name_delete({'name': name}) - print("Campaign deleted.") - except ElasticEmail.ApiException as e: - print("Exception when calling CampaignsApi->campaigns_by_name_delete: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Delete Campaign +Example api call that deletes an existing campaign. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = campaigns_api.CampaignsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.CampaignsApi(api_client) name = "hello campaign" try: - api_instance.campaigns_by_name_delete({'name': name}) + # Delete Campaign + api_instance.campaigns_by_name_delete(name) print("Campaign deleted.") except ElasticEmail.ApiException as e: print("Exception when calling CampaignsApi->campaigns_by_name_delete: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 deleteCampaign.py ``` diff --git a/examples/functions/deleteCampaign.py b/examples/functions/deleteCampaign.py index 7d1c795..9d1d4b2 100755 --- a/examples/functions/deleteCampaign.py +++ b/examples/functions/deleteCampaign.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api # Defining the host is optional and defaults to https://api.elasticemail.com/v4 configuration = ElasticEmail.Configuration() @@ -13,13 +12,13 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = campaigns_api.CampaignsApi(api_client) + api_instance = ElasticEmail.CampaignsApi(api_client) name = "hello campaign" try: # Delete Campaign - api_instance.campaigns_by_name_delete({'name': name}) + api_instance.campaigns_by_name_delete(name) print("Campaign deleted.") except ElasticEmail.ApiException as e: print("Exception when calling CampaignsApi->campaigns_by_name_delete: %s\n" % e) diff --git a/examples/functions/deleteContacts.md b/examples/functions/deleteContacts.md index e95d028..ddca02e 100755 --- a/examples/functions/deleteContacts.md +++ b/examples/functions/deleteContacts.md @@ -1,99 +1,39 @@ # Delete Contacts -This guide will walk you through steps of deleting contact(s) from your account using the Python library. - -*Required Access Level: ModifyContacts* - -## What's a contact? -When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/deleteContacts.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.emails_payload import EmailsPayload -``` - -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 +from ElasticEmail.models.emails_payload import EmailsPayload -```python +# 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 ContactsApi that will be used to delete contacts. - -```python - api_instance = contacts_api.ContactsApi(api_client) -``` - -Create an object with an array of contacts to delete. -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/contactsByEmailDelete - -```python - emails_payload = EmailsPayload( - Emails=["johnsmith@domain.com"], - ) -``` - -Use try & except block to call `contacts_delete_post` method from the API to delete contacts: - -```python - try: - api_instance.contacts_delete_post(emails_payload) - print("Contacts deleted.") - except ElasticEmail.ApiException as e: - print("Exception when calling ContactsApi->contacts_delete_post: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.emails_payload import EmailsPayload - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Delete contact +Example api call that deletes given contact(s). +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = contacts_api.ContactsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.ContactsApi(api_client) emails_payload = EmailsPayload( Emails=["johnsmith@domain.com"], - ) + ) # EmailsPayload | Provide either rule or a list of emails, not both. try: + # Delete Contacts Bulk api_instance.contacts_delete_post(emails_payload) print("Contacts deleted.") except ElasticEmail.ApiException as e: print("Exception when calling ContactsApi->contacts_delete_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 deleteContacts.py ``` diff --git a/examples/functions/deleteContacts.py b/examples/functions/deleteContacts.py index 2cf8b90..fe8b288 100755 --- a/examples/functions/deleteContacts.py +++ b/examples/functions/deleteContacts.py @@ -1,6 +1,5 @@ import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.emails_payload import EmailsPayload +from ElasticEmail.models.emails_payload import EmailsPayload # Defining the host is optional and defaults to https://api.elasticemail.com/v4 configuration = ElasticEmail.Configuration() @@ -14,7 +13,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = contacts_api.ContactsApi(api_client) + api_instance = ElasticEmail.ContactsApi(api_client) emails_payload = EmailsPayload( Emails=["johnsmith@domain.com"], diff --git a/examples/functions/deleteList.md b/examples/functions/deleteList.md index 86f7c19..e486219 100755 --- a/examples/functions/deleteList.md +++ b/examples/functions/deleteList.md @@ -1,94 +1,35 @@ # Delete List -This guide will walk you through steps of removing contacts list from your account using the Python library. - -*Required Access Level: ModifyContacts* - -## What's a list? -When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. Segments add contacts automatically when specfied conditions are met, and contacts on lists are managed manually. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/deleteList.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import lists_api -``` - -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 - -```python +# 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 ListsApi that will be used to delete list. - -```python - api_instance = lists_api.ListsApi(api_client) -``` -The only thing needed is a list name. - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/listsByNameDelete - - -```python - name = "Best contacts" -``` - -Use try & except block to call `lists_by_name_delete` method from the API to delete a list: - -```python - try: - api_instance.lists_by_name_delete({'name': name}) - print("List deleted.") - except ElasticEmail.ApiException as e: - print("Exception when calling ListsApi->lists_by_name_delete: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import lists_api - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Delete list +Example api call that loads given contacts list. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = lists_api.ListsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.ListsApi(api_client) - name = "Best contacts" + name = "Best contacts" # str | Name of your list. try: - api_instance.lists_by_name_delete({'name': name}) + api_instance.lists_by_name_delete(name) print("List deleted.") except ElasticEmail.ApiException as e: print("Exception when calling ListsApi->lists_by_name_delete: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 deleteList.py ``` diff --git a/examples/functions/deleteList.py b/examples/functions/deleteList.py index 185dcef..5a8acdf 100755 --- a/examples/functions/deleteList.py +++ b/examples/functions/deleteList.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import lists_api # Defining the host is optional and defaults to https://api.elasticemail.com/v4 configuration = ElasticEmail.Configuration() @@ -13,12 +12,12 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = lists_api.ListsApi(api_client) + api_instance = ElasticEmail.ListsApi(api_client) name = "Best contacts" # str | Name of your list. try: - api_instance.lists_by_name_delete({'name': name}) + api_instance.lists_by_name_delete(name) print("List deleted.") except ElasticEmail.ApiException as e: print("Exception when calling ListsApi->lists_by_name_delete: %s\n" % e) diff --git a/examples/functions/deleteTemplate.md b/examples/functions/deleteTemplate.md index a6e15a9..a72af2f 100755 --- a/examples/functions/deleteTemplate.md +++ b/examples/functions/deleteTemplate.md @@ -1,94 +1,36 @@ # Delete Template -This guide will walk you through steps of deleting existing template from your account using the Python library. - -*Required Access Level: ModifyTemplates* - -## What's a template? -When using Elastic Email you send emails to your contacts. A single template is a body of email prepared and saved under given name. Till it's deleted it can be reused to send any number of emails. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/deleteTemplate.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import templates_api -``` - -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 - -```python +# 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 TemplatesApi that will be used to delete existing template from your account. - -```python - api_instance = templates_api.TemplatesApi(api_client) -``` -To delete a template you need to specfiy it's name: - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/templatesByNameDelete - - -```python - name = "My template" -``` - -Use try & except block to call `templates_by_name_delete` method from the API to delete a template: - -```python - try: - api_instance.templates_by_name_delete({'name': name}) - print("Template deleted.") - except ElasticEmail.ApiException as e: - print("Exception when calling TemplatesApi->templates_by_name_delete: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import templates_api - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Delete template +Example api call that deletes existing template. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = templates_api.TemplatesApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.TemplatesApi(api_client) - name = "My template" + name = "My new template" # str | Name of template. try: - api_instance.templates_by_name_delete({'name': name}) + # Delete Template + api_instance.templates_by_name_delete(name) print("Template deleted.") except ElasticEmail.ApiException as e: print("Exception when calling TemplatesApi->templates_by_name_delete: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 deleteTemplate.py ``` diff --git a/examples/functions/deleteTemplate.py b/examples/functions/deleteTemplate.py index 6f67de3..c8ff39b 100755 --- a/examples/functions/deleteTemplate.py +++ b/examples/functions/deleteTemplate.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import templates_api # Defining the host is optional and defaults to https://api.elasticemail.com/v4 configuration = ElasticEmail.Configuration() @@ -13,13 +12,13 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = templates_api.TemplatesApi(api_client) + api_instance = ElasticEmail.TemplatesApi(api_client) name = "My new template" # str | Name of template. try: # Delete Template - api_instance.templates_by_name_delete({'name': name}) + api_instance.templates_by_name_delete(name) print("Template deleted.") except ElasticEmail.ApiException as e: print("Exception when calling TemplatesApi->templates_by_name_delete: %s\n" % e) diff --git a/examples/functions/exportContacts.md b/examples/functions/exportContacts.md index 1a84bd5..0236d5c 100755 --- a/examples/functions/exportContacts.md +++ b/examples/functions/exportContacts.md @@ -1,115 +1,47 @@ # Export Contacts -This guide will walk you through the process of exporting selected contacts to downloadable file using the Python library. - -*Required Access Level: Export* - -## What's a contact? -When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/exportContacts.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.export_file_formats import ExportFileFormats -from ElasticEmail.model.compression_format import CompressionFormat +from ElasticEmail.models.export_file_formats import ExportFileFormats +from ElasticEmail.models.compression_format import CompressionFormat 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 -```python +# 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: -``` +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' + +""" +Export contacts +Example api call that exports selected contacts to downloadable file. +Options: +fileFormat: "Csv" "Xml" "Json" – Format of the exported file +emails: [mail@contact.com,mail1@contact.com,mail2@contact.com] – Array of contact emails +compressionFormat: "None" "Zip" +fileName=filename.txt – Name of your file including extension. +rule: rule="Status%20=%20Engaged" – Query used for filtering. +""" with ElasticEmail.ApiClient(configuration) as api_client: -``` - -Create an instance of ContactsApi that will be used to create a file with exported contacts. - -```python - api_instance = contacts_api.ContactsApi(api_client) -``` - -Create options variables: -- fileFormat - specify format in which file should be created, options are: "Csv" "Xml" "Json". -- emails - select contacts to export by providing array of emails -- fileName - you can specify file name of your choice - -Other options: -- rule - eg. `rule=Status%20=%20Engaged` – Query used for filtering -- compressionFormat - "None" or "Zip" - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/contactsExportPost + # Create an instance of the API class + api_instance = ElasticEmail.ContactsApi(api_client) -```python - query_params = { - 'fileFormat': ExportFileFormats("Csv"), # ExportFileFormats | Format of the exported file (optional) - 'compressionFormat': CompressionFormat("None"), # CompressionFormat | FileResponse compression format. None or Zip. (optional) - 'fileName': "exported.csv", # str | Name of your file including extension. (optional) - } -``` - -Use try & except block to call `contacts_export_post` method from the API to export contacts: - -```python try: - api_response = api_instance.contacts_export_post(query_params = query_params) + # Export Contacts + api_response = api_instance.contacts_export_post( + file_format=ExportFileFormats("Csv"), + compression_format=CompressionFormat("None"), + file_name="exported.csv", + ) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling ContactsApi->contacts_export_post: %s\n" % e) - ``` +Run with: -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.export_file_formats import ExportFileFormats -from ElasticEmail.model.compression_format import CompressionFormat -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' - -with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = contacts_api.ContactsApi(api_client) - - query_params = { - 'fileFormat': ExportFileFormats("Csv"), # ExportFileFormats | Format of the exported file (optional) - 'compressionFormat': CompressionFormat("None"), # CompressionFormat | FileResponse compression format. None or Zip. (optional) - 'fileName': "exported.csv", # str | Name of your file including extension. (optional) - } - - try: - api_response = api_instance.contacts_export_post(query_params = query_params) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling ContactsApi->contacts_export_post: %s\n" % e) -``` - -## Run the code -``` -python3 snippet.py +```bash +py -3 exportContacts.py ``` diff --git a/examples/functions/exportContacts.py b/examples/functions/exportContacts.py index a838a7f..cfc7c6d 100755 --- a/examples/functions/exportContacts.py +++ b/examples/functions/exportContacts.py @@ -1,7 +1,6 @@ import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -from ElasticEmail.model.export_file_formats import ExportFileFormats -from ElasticEmail.model.compression_format import CompressionFormat +from ElasticEmail.models.export_file_formats import ExportFileFormats +from ElasticEmail.models.compression_format import CompressionFormat from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -22,17 +21,15 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = contacts_api.ContactsApi(api_client) - - query_params = { - 'fileFormat': ExportFileFormats("Csv"), # ExportFileFormats | Format of the exported file (optional) - 'compressionFormat': CompressionFormat("None"), # CompressionFormat | FileResponse compression format. None or Zip. (optional) - 'fileName': "exported.csv", # str | Name of your file including extension. (optional) - } + api_instance = ElasticEmail.ContactsApi(api_client) try: # Export Contacts - api_response = api_instance.contacts_export_post(query_params = query_params) + api_response = api_instance.contacts_export_post( + file_format=ExportFileFormats("Csv"), + compression_format=CompressionFormat("None"), + file_name="exported.csv", + ) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling ContactsApi->contacts_export_post: %s\n" % e) diff --git a/examples/functions/loadCampaign.md b/examples/functions/loadCampaign.md index 9d83a4b..bfd7de3 100755 --- a/examples/functions/loadCampaign.md +++ b/examples/functions/loadCampaign.md @@ -1,95 +1,38 @@ # Load Campaign -This guide will walk you through the process of loading details about an existing campaign in your account using the Python library. Example details you can get are like: name, status, recipients, subject etc. - -*Required Access Level: ViewCampaigns* - -## What's a campaign? -When using Elastic Email, when you send an email to any group of contacts we call that a "campaign". - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/loadCampaign.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api 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 - -```python +# 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 load a campaign. - -```python - api_instance = campaigns_api.CampaignsApi(api_client) -``` - -The only thing you need to specify is a campaign name - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/campaignsGet +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' -```python - name = "hello campaign" -``` +""" +Load Campaign +Example api call that fetches details about single campaign like: name, status, recipients, subject etc. +""" +with ElasticEmail.ApiClient(configuration) as api_client: + # Create an instance of the API class + api_instance = ElasticEmail.CampaignsApi(api_client) -Use try & except block to call `campaigns_by_name_get` method from the API to fetch campaign details: + name = "hello campaign" # str | Name of Campaign to get -```python + # Call api try: - api_response = api_instance.campaigns_by_name_get({'name': name}) + # Load Campaign + api_response = api_instance.campaigns_by_name_get(name) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling CampaignsApi->campaigns_by_name_get: %s\n" % e) ``` +Run with: -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' - -with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = campaigns_api.CampaignsApi(api_client) - - name = "hello campaign" - - try: - api_instance.campaigns_by_name_delete({'name': name}) - print("Campaign deleted.") - except ElasticEmail.ApiException as e: - print("Exception when calling CampaignsApi->campaigns_by_name_delete: %s\n" % e) -``` - -## Run the code -``` -python3 snippet.py +```bash +py -3 loadCampaign.py ``` diff --git a/examples/functions/loadCampaign.py b/examples/functions/loadCampaign.py index 9a28e62..92bdbfc 100755 --- a/examples/functions/loadCampaign.py +++ b/examples/functions/loadCampaign.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -14,14 +13,14 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = campaigns_api.CampaignsApi(api_client) + api_instance = ElasticEmail.CampaignsApi(api_client) name = "hello campaign" # str | Name of Campaign to get # Call api try: # Load Campaign - api_response = api_instance.campaigns_by_name_get({'name': name}) + api_response = api_instance.campaigns_by_name_get(name) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling CampaignsApi->campaigns_by_name_get: %s\n" % e) diff --git a/examples/functions/loadCampaignsStats.md b/examples/functions/loadCampaignsStats.md index 51d8db8..425df89 100755 --- a/examples/functions/loadCampaignsStats.md +++ b/examples/functions/loadCampaignsStats.md @@ -1,113 +1,34 @@ -# Load Campaigns Statistics +# Load Campaigns Stats -This guide will walk you through steps of loading statistics for each campaign from your account using the Python library. - -*Required Access Level: ViewChannels* - -## What statistics are returned? -When using Elastic Email you send email campaigns to your contacts. From that we create statistics reports for you eg. number of emails sent, number of delivered messages,Number of opened messages, number of unsubscribed messages, number of clicked messages etc. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/loadCampaignsStats.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import statistics_api 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 - -```python +# 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 StatisticsApi that will be used to get basic send statistics. -```python - api_instance = statistics_api.StatisticsApi(api_client) -``` - -Campaigns statistics reponse is paginated you need to specfiy pagination options: -- limit – maximum returned items, `limit = 0` means to return everything till the end of the list -- offset – how many items should be skipped from begging - -Eg. to return second page of elements paginated 20 elements per page specify pagination options as follows -```python - query_params = { - 'limit': 20, - 'offset': 20 - } -``` - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/statisticsCampaignsByNameGet - -Let's fetch first 100 campaigns: - -```python - query_params = { - 'limit': 100, - 'offset': 0 - } -``` - -Use try & except block to call `statistics_campaigns_get` method from the API to fetch statistics: - -```python - try: - api_response = api_instance.statistics_campaigns_get(query_params = query_params) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling StatisticsApi->statistics_campaigns_get: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import statistics_api -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Load Campaigns Stats +Example api call that loads a list of your campaigns' stats. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = statistics_api.StatisticsApi(api_client) - - query_params = { - 'limit': 100 - 'offset': 0 - } + # Create an instance of the API class + api_instance = ElasticEmail.StatisticsApi(api_client) try: - api_response = api_instance.statistics_campaigns_get(query_params = query_params) + api_response = api_instance.statistics_campaigns_get(limit=100, offset=0) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling StatisticsApi->statistics_campaigns_get: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 loadCampaignsStats.py ``` diff --git a/examples/functions/loadCampaignsStats.py b/examples/functions/loadCampaignsStats.py index 689d4b7..ef200a1 100755 --- a/examples/functions/loadCampaignsStats.py +++ b/examples/functions/loadCampaignsStats.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import statistics_api from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -14,15 +13,10 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = statistics_api.StatisticsApi(api_client) - - query_params = { - 'limit': 100, # int | Maximum number of returned items. (optional) - 'offset': 0 # int | How many items should be returned ahead. (optional) - } + api_instance = ElasticEmail.StatisticsApi(api_client) try: - api_response = api_instance.statistics_campaigns_get(query_params = query_params) + api_response = api_instance.statistics_campaigns_get(limit=100, offset=0) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling StatisticsApi->statistics_campaigns_get: %s\n" % e) diff --git a/examples/functions/loadChannelsStats.md b/examples/functions/loadChannelsStats.md index 17327ed..e9fb23d 100755 --- a/examples/functions/loadChannelsStats.md +++ b/examples/functions/loadChannelsStats.md @@ -1,114 +1,34 @@ -# Load Channels Statistics +# Load Channels Stats -This guide will walk you through steps of loading statistics for each channel from your account using the Python library. - -*Required Access Level: ViewChannels* - -## What statistics are returned? -When sending emails using Elastic Email with SMTP or API, Elastic Email organizes your email flow by channels. Statistics reports for each channel are being created eg. number of emails sent, number of delivered messages,Number of opened messages, number of unsubscribed messages, number of clicked messages etc. - - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/loadChannelsStats.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import statistics_api 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 -```python +# 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 StatisticsApi that will be used to get basic send statistics. - -```python - api_instance = statistics_api.StatisticsApi(api_client) -``` - -Channels statistics reponse is paginated you need to specfiy pagination options: -- limit – maximum returned items, `limit = 0` means to return everything till the end of the list -- offset – how many items should be skipped from begging - -Eg. to return second page of elements paginated 20 elements per page specify pagination options as follows -```python - query_params = { - 'limit': 20, - 'offset': 20 - } -``` - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/statisticsChannelsGet - -Let's fetch first 100 channels: - -```python - query_params = { - 'limit': 100, - 'offset': 0 - } -``` - -Use try & except block to call `statistics_channels_get` method from the API to fetch statistics: - -```python - try: - api_response = api_instance.statistics_channels_get(query_params = query_params) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling StatisticsApi->statistics_campaigns_get: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import statistics_api -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Load channels stats +Example api call that loads a list of your channels' stats. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = statistics_api.StatisticsApi(api_client) - - query_params = { - 'limit': 100, - 'offset': 0 - } + # Create an instance of the API class + api_instance = ElasticEmail.StatisticsApi(api_client) try: - api_response = api_instance.statistics_channels_get(query_params = query_params) + api_response = api_instance.statistics_channels_get(limit=100, offset=0) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling StatisticsApi->statistics_channels_get: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 loadChannelsStats.py ``` diff --git a/examples/functions/loadChannelsStats.py b/examples/functions/loadChannelsStats.py index 1019b40..450d1df 100755 --- a/examples/functions/loadChannelsStats.py +++ b/examples/functions/loadChannelsStats.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import statistics_api from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -14,15 +13,10 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = statistics_api.StatisticsApi(api_client) - - query_params = { - 'limit': 100, # int | Maximum number of returned items. (optional) - 'offset': 0 # int | How many items should be returned ahead. (optional) - } + api_instance = ElasticEmail.StatisticsApi(api_client) try: - api_response = api_instance.statistics_channels_get(query_params = query_params) + api_response = api_instance.statistics_channels_get(limit=100, offset=0) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling StatisticsApi->statistics_channels_get: %s\n" % e) diff --git a/examples/functions/loadList.md b/examples/functions/loadList.md index 80e0635..d67fa57 100755 --- a/examples/functions/loadList.md +++ b/examples/functions/loadList.md @@ -1,97 +1,45 @@ # Load List -This guide will walk you through the process of loading details about contacts list on your account using the Python library. - -*Required Access Level: ViewContacts* - -## What's a list? -When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. Segments add contacts automatically when specfied conditions are met, and contacts on lists are managed manually. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/loadList.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import lists_api 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 -```python +# 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 ListsApi that will be used to load list. - -```python - api_instance = lists_api.ListsApi(api_client) -``` - -The only thing needed is a list name. -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/listsByNameGet - - -```python - name = "Best contacts" -``` - -Use try & except block to call `lists_by_name_get` method from the API to fetch a list: - -```python - try: - api_response = api_instance.lists_by_name_get({'name': name}) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling ListsApi->lists_by_name_get: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import lists_api -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Load list +Example api call that loads given contacts list. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = lists_api.ListsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.ListsApi(api_client) - name = "Best contacts" + name = "Best contacts" # str | Name of your list. try: - api_response = api_instance.lists_by_name_get({'name': name}) + api_response = api_instance.lists_by_name_get(name) pprint(api_response) except ElasticEmail.ApiException as e: - print("Exception when calling ListsApi->lists_by_name_get: %s\n" % e) - -``` - -## Run the code -``` -python3 snippet.py + try: + available = api_instance.lists_get(limit=1, offset=0) + if available: + fallback_name = available[0].list_name + api_response = api_instance.lists_by_name_get(fallback_name) + pprint(api_response) + raise SystemExit(0) + except ElasticEmail.ApiException: + pass + print(f"No list found to load ({e.status}). Skipping.") +``` + +Run with: + +```bash +py -3 loadList.py ``` diff --git a/examples/functions/loadList.py b/examples/functions/loadList.py index 6cd1db0..5daec02 100755 --- a/examples/functions/loadList.py +++ b/examples/functions/loadList.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import lists_api from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -14,12 +13,21 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = lists_api.ListsApi(api_client) + api_instance = ElasticEmail.ListsApi(api_client) name = "Best contacts" # str | Name of your list. try: - api_response = api_instance.lists_by_name_get({'name': name}) + api_response = api_instance.lists_by_name_get(name) pprint(api_response) except ElasticEmail.ApiException as e: - print("Exception when calling ListsApi->lists_by_name_get: %s\n" % e) + try: + available = api_instance.lists_get(limit=1, offset=0) + if available: + fallback_name = available[0].list_name + api_response = api_instance.lists_by_name_get(fallback_name) + pprint(api_response) + raise SystemExit(0) + except ElasticEmail.ApiException: + pass + print(f"No list found to load ({e.status}). Skipping.") diff --git a/examples/functions/loadStatistics.md b/examples/functions/loadStatistics.md index f0301f9..60b118f 100755 --- a/examples/functions/loadStatistics.md +++ b/examples/functions/loadStatistics.md @@ -1,109 +1,33 @@ # Load Statistics -This guide will walk you through steps of loading basic delivery statistics from your account using the Python library. - -*Required Access Level: ViewReports* - -## What statistics are returned? -When using Elastic Email you send emails to your contacts from that we create some statistics reports for you eg. number of emails sent, number of delivered messages, number of bounced messages, number of unsubscribed messages etc. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/loadStatistics.py`. ```python from datetime import datetime import ElasticEmail -from ElasticEmail.apis.tags import statistics_api 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 - -```python +# 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 StatisticsApi that will be used to get basic send statistics. - -```python - api_instance = statistics_api.StatisticsApi(api_client) -``` - -First you need to specify a date range: -- from date -- to date – optional - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/statisticsGet - - -```python - _from = datetime(2022,1,1,00,00,00) - to = datetime(2022,1,30,00,00,00) -``` -Use try & except block to call `statistics_get` method from the API to load a statistics with only `from` date given: - -```python - try: - api_response = api_instance.statistics_get({'from': _from}) - print("From %s" % _from) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling StatisticsApi->statistics_get: %s\n" % e) -``` - -Use try & except block to call `statistics_get` method from the API to load a statistics with `from` and `to` dates given: - -```python - try: - api_response = api_instance.statistics_get({'from': _from, 'to': to}) - print(f"\nFrom {_from} To {to}") - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling StatisticsApi->statistics_get: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -from datetime import datetime -import ElasticEmail -from ElasticEmail.apis.tags import statistics_api -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Load statistics +Example api call that loads basic statistics. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = statistics_api.StatisticsApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.StatisticsApi(api_client) - _from = datetime(2022,1,1,00,00,00) - to = datetime(2022,1,30,00,00,00) + _from = datetime(2022,1,1,00,00,00) # datetime | Starting date for search in YYYY-MM-DDThh:mm:ss format. + to = datetime(2022,1,30,00,00,00) # datetime | Ending date for search in YYYY-MM-DDThh:mm:ss format. (optional) # only from date: try: - api_response = api_instance.statistics_get({'from': _from}) + # Load Statistics + api_response = api_instance.statistics_get(var_from=_from) print("From %s" % _from) pprint(api_response) except ElasticEmail.ApiException as e: @@ -111,14 +35,15 @@ with ElasticEmail.ApiClient(configuration) as api_client: # from and to dates: try: - api_response = api_instance.statistics_get({'from': _from, 'to': to}) + api_response = api_instance.statistics_get(var_from=_from, to=to) print(f"\nFrom {_from} To {to}") pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling StatisticsApi->statistics_get: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 loadStatistics.py ``` diff --git a/examples/functions/loadStatistics.py b/examples/functions/loadStatistics.py index 7b5f8d9..1cf7f15 100755 --- a/examples/functions/loadStatistics.py +++ b/examples/functions/loadStatistics.py @@ -1,6 +1,5 @@ from datetime import datetime import ElasticEmail -from ElasticEmail.apis.tags import statistics_api from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -15,7 +14,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = statistics_api.StatisticsApi(api_client) + api_instance = ElasticEmail.StatisticsApi(api_client) _from = datetime(2022,1,1,00,00,00) # datetime | Starting date for search in YYYY-MM-DDThh:mm:ss format. to = datetime(2022,1,30,00,00,00) # datetime | Ending date for search in YYYY-MM-DDThh:mm:ss format. (optional) @@ -23,7 +22,7 @@ # only from date: try: # Load Statistics - api_response = api_instance.statistics_get({'from': _from}) + api_response = api_instance.statistics_get(var_from=_from) print("From %s" % _from) pprint(api_response) except ElasticEmail.ApiException as e: @@ -31,7 +30,7 @@ # from and to dates: try: - api_response = api_instance.statistics_get({'from': _from, 'to': to}) + api_response = api_instance.statistics_get(var_from=_from, to=to) print(f"\nFrom {_from} To {to}") pprint(api_response) except ElasticEmail.ApiException as e: diff --git a/examples/functions/loadTemplate.md b/examples/functions/loadTemplate.md index ad7247b..7cd7d16 100755 --- a/examples/functions/loadTemplate.md +++ b/examples/functions/loadTemplate.md @@ -1,101 +1,53 @@ # Load Template -This guide will walk you through steps of loading existing template details using the Python library. - -*Required Access Level: ViewTemplates* - -## What's a template? -When using Elastic Email you send emails to your contacts. A single template is a body of email prepared and saved under given name. Till it's deleted it can be reused to send any number of emails. - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/loadTemplate.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import templates_api 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 -```python +# 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 TemplatesApi that will be used to load existing template on your account. - -```python - api_instance = templates_api.TemplatesApi(api_client) -``` - -To load a template you need to specfiy it's name: -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/templatesByNameGet - - -```python - name = "hello_template" -``` - -Use try & except block to call `templates_by_name_get` method from the API to load a template: - -```python - try: - api_response = api_instance.templates_by_name_get({'name': name}) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling TemplatesApi->templates_by_name_get: %s\n" % e) - -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import templates_api -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.template_payload import TemplatePayload -from ElasticEmail.model.template_scope import TemplateScope -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Load template +Example api call that loads an existing template. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = templates_api.TemplatesApi(api_client) + # Create an instance of the API class + api_instance = ElasticEmail.TemplatesApi(api_client) - name = "hello_template" + name = "hello_template" # str | Name of template. try: - api_response = api_instance.templates_by_name_get({'name': name}) + # Load Template + api_response = api_instance.templates_by_name_get(name) pprint(api_response) except ElasticEmail.ApiException as e: - print("Exception when calling TemplatesApi->templates_post: %s\n" % e) -``` - -## Run the code -``` -python3 snippet.py + try: + available = api_instance.templates_get( + scope_type=[ + ElasticEmail.TemplateScope("Personal"), + ElasticEmail.TemplateScope("Global"), + ], + limit=1, + offset=0, + ) + if available: + fallback_name = available[0].name + api_response = api_instance.templates_by_name_get(fallback_name) + pprint(api_response) + raise SystemExit(0) + except ElasticEmail.ApiException: + pass + print(f"No template found to load ({e.status}). Skipping.") +``` + +Run with: + +```bash +py -3 loadTemplate.py ``` diff --git a/examples/functions/loadTemplate.py b/examples/functions/loadTemplate.py index bb69ed6..953cd73 100755 --- a/examples/functions/loadTemplate.py +++ b/examples/functions/loadTemplate.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import templates_api from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -14,13 +13,29 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = templates_api.TemplatesApi(api_client) + api_instance = ElasticEmail.TemplatesApi(api_client) name = "hello_template" # str | Name of template. try: # Load Template - api_response = api_instance.templates_by_name_get({'name': name}) + api_response = api_instance.templates_by_name_get(name) pprint(api_response) except ElasticEmail.ApiException as e: - print("Exception when calling TemplatesApi->templates_by_name_get: %s\n" % e) + try: + available = api_instance.templates_get( + scope_type=[ + ElasticEmail.TemplateScope("Personal"), + ElasticEmail.TemplateScope("Global"), + ], + limit=1, + offset=0, + ) + if available: + fallback_name = available[0].name + api_response = api_instance.templates_by_name_get(fallback_name) + pprint(api_response) + raise SystemExit(0) + except ElasticEmail.ApiException: + pass + print(f"No template found to load ({e.status}). Skipping.") diff --git a/examples/functions/sendBulkEmails.md b/examples/functions/sendBulkEmails.md index 4a2b49e..9c78e77 100755 --- a/examples/functions/sendBulkEmails.md +++ b/examples/functions/sendBulkEmails.md @@ -1,129 +1,29 @@ # Send Bulk Emails -This guide will walk you through steps of sending a bulk email using the Python library. - -*Required Access Level: SendHttp* - -## What's a bulk email? -When using Elastic Email you send emails to your contacts. One of options is to send bulk emails. Bulk email can be described as a single email message send to a large group at once. - - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/sendBulkEmails.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import emails_api -from ElasticEmail.model.email_content import EmailContent -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.email_recipient import EmailRecipient -from ElasticEmail.model.email_message_data import EmailMessageData +from ElasticEmail.models.email_content import EmailContent +from ElasticEmail.models.body_part import BodyPart +from ElasticEmail.models.body_content_type import BodyContentType +from ElasticEmail.models.email_recipient import EmailRecipient +from ElasticEmail.models.email_message_data import EmailMessageData 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 -```python +# 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: -```python -with ElasticEmail.ApiClient(configuration) as api_client: -``` -Create an instance of EmailsApi that will be used to send a bulk email. - -```python - api_instance = emails_api.EmailsApi(api_client) -``` - -First you need to specify email details: -- email recipients: - - this example demostrates merge fields usage, for each recipient `{name}` will be changed to recipient's name -- email content: - - body parts – in HTML, PlainText or in both - - from email – it needs to be your validated email address - - email subject - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/emailsPost - - -```python - email_message_data = EmailMessageData( - Recipients=[ - EmailRecipient( - Email="johnsmith@domain.com", - Fields={ - "name": "John", - }, - ), - ], - Content=EmailContent( - body=[ - BodyPart( - ContentType=BodyContentType("HTML"), - Content="Hi {name}!", - Charset="utf-8", - ), - BodyPart( - ContentType=BodyContentType("PlainText"), - Content="Hi {name}!", - Charset="utf-8", - ), - ], - From="myemail@domain.com", - ReplyTo="myemail@domain.com", - Subject="Example email", - ), - ) -``` - -Use try & except block to call `emails_post` method from the API to send an email: - -```python - try: - api_response = api_instance.emails_post(email_message_data) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling EmailsApi->emails_post: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import emails_api -from ElasticEmail.model.email_content import EmailContent -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.email_recipient import EmailRecipient -from ElasticEmail.model.email_message_data import EmailMessageData -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Send bulk emails +Example api call that sends bulk merge email. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = emails_api.EmailsApi(api_client) - + # Create an instance of the API class + api_instance = ElasticEmail.EmailsApi(api_client) email_message_data = EmailMessageData( Recipients=[ EmailRecipient( @@ -150,16 +50,18 @@ with ElasticEmail.ApiClient(configuration) as api_client: ReplyTo="myemail@domain.com", Subject="Example email", ), - ) + ) # EmailMessageData | Email data try: + # Send Bulk Emails api_response = api_instance.emails_post(email_message_data) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling EmailsApi->emails_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 sendBulkEmails.py ``` diff --git a/examples/functions/sendBulkEmails.py b/examples/functions/sendBulkEmails.py index e5fa8e0..5697c15 100755 --- a/examples/functions/sendBulkEmails.py +++ b/examples/functions/sendBulkEmails.py @@ -1,10 +1,9 @@ import ElasticEmail -from ElasticEmail.apis.tags import emails_api -from ElasticEmail.model.email_content import EmailContent -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.email_recipient import EmailRecipient -from ElasticEmail.model.email_message_data import EmailMessageData +from ElasticEmail.models.email_content import EmailContent +from ElasticEmail.models.body_part import BodyPart +from ElasticEmail.models.body_content_type import BodyContentType +from ElasticEmail.models.email_recipient import EmailRecipient +from ElasticEmail.models.email_message_data import EmailMessageData from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -19,7 +18,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = emails_api.EmailsApi(api_client) + api_instance = ElasticEmail.EmailsApi(api_client) email_message_data = EmailMessageData( Recipients=[ EmailRecipient( diff --git a/examples/functions/sendTransactionalEmails.md b/examples/functions/sendTransactionalEmails.md index 1145a78..ada4442 100755 --- a/examples/functions/sendTransactionalEmails.md +++ b/examples/functions/sendTransactionalEmails.md @@ -1,127 +1,30 @@ # Send Transactional Emails -This guide will walk you through steps of sending a transactional email using the Python library. - -*Required Access Level: SendHttp* - -## What's a transactional email? -When using Elastic Email you send emails to your contacts. One of options is to send transational emails. Transactional emails can be described that they are emails generated as a response to a particular actions done by the subscriber eg. account changes, purchase receipts, other confirmations. - -A transactional email have a limit of 50 maximum recipients. - - -## 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/sendTransactionalEmails.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import emails_api -from ElasticEmail.model.email_content import EmailContent -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.transactional_recipient import TransactionalRecipient -from ElasticEmail.model.email_transactional_message_data import EmailTransactionalMessageData +from ElasticEmail.models.email_content import EmailContent +from ElasticEmail.models.body_part import BodyPart +from ElasticEmail.models.body_content_type import BodyContentType +from ElasticEmail.models.transactional_recipient import TransactionalRecipient +from ElasticEmail.models.email_transactional_message_data import EmailTransactionalMessageData 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 -```python +# 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 EmailsApi that will be used to send a transactional email. - -```python - api_instance = emails_api.EmailsApi(api_client) -``` - -First you need to specify email details: -- email recipients -- email content: - - body parts – in HTML, PlainText or in both - - from email – it needs to be your validated email address - - email subject - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/emailsTransactionalPost - - -```python - email_transactional_message_data = EmailTransactionalMessageData( - Recipients=TransactionalRecipient( - To=[ - "johnsmith@domain.com", - ], - ), - Content=EmailContent( - Body=[ - BodyPart( - ContentType=BodyContentType("HTML"), - Content="Mail content.", - Charset="utf-8", - ), - BodyPart( - ContentType=BodyContentType("PlainText"), - Content="Mail content.", - Charset="utf-8", - ), - ], - From="myemail@domain.com", - ReplyTo="myemail@domain.com", - Subject="Example transactional email", - ), - ) -``` - -Use try & except block to call `emails_transactional_post` method from the API to send an email: -```python - try: - api_response = api_instance.emails_transactional_post(email_transactional_message_data) - pprint(api_response) - except ElasticEmail.ApiException as e: - print("Exception when calling EmailsApi->emails_transactional_post: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import emails_api -from ElasticEmail.model.email_content import EmailContent -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.transactional_recipient import TransactionalRecipient -from ElasticEmail.model.email_transactional_message_data import EmailTransactionalMessageData -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +Send transactional emails +Example api call that sends transactional email. +Limit of 50 maximum recipients. +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = emails_api.EmailsApi(api_client) - + # Create an instance of the API class + api_instance = ElasticEmail.EmailsApi(api_client) email_transactional_message_data = EmailTransactionalMessageData( Recipients=TransactionalRecipient( To=[ @@ -145,16 +48,18 @@ with ElasticEmail.ApiClient(configuration) as api_client: ReplyTo="myemail@domain.com", Subject="Example transactional email", ), - ) + ) # EmailTransactionalMessageData | Email data try: + # Send Transactional Email api_response = api_instance.emails_transactional_post(email_transactional_message_data) pprint(api_response) except ElasticEmail.ApiException as e: print("Exception when calling EmailsApi->emails_transactional_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 sendTransactionalEmails.py ``` diff --git a/examples/functions/sendTransactionalEmails.py b/examples/functions/sendTransactionalEmails.py index 69b3b89..1beea99 100755 --- a/examples/functions/sendTransactionalEmails.py +++ b/examples/functions/sendTransactionalEmails.py @@ -1,10 +1,9 @@ import ElasticEmail -from ElasticEmail.apis.tags import emails_api -from ElasticEmail.model.email_content import EmailContent -from ElasticEmail.model.body_part import BodyPart -from ElasticEmail.model.body_content_type import BodyContentType -from ElasticEmail.model.transactional_recipient import TransactionalRecipient -from ElasticEmail.model.email_transactional_message_data import EmailTransactionalMessageData +from ElasticEmail.models.email_content import EmailContent +from ElasticEmail.models.body_part import BodyPart +from ElasticEmail.models.body_content_type import BodyContentType +from ElasticEmail.models.transactional_recipient import TransactionalRecipient +from ElasticEmail.models.email_transactional_message_data import EmailTransactionalMessageData from pprint import pprint # Defining the host is optional and defaults to https://api.elasticemail.com/v4 @@ -20,8 +19,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = emails_api.EmailsApi(api_client) - + api_instance = ElasticEmail.EmailsApi(api_client) email_transactional_message_data = EmailTransactionalMessageData( Recipients=TransactionalRecipient( To=[ diff --git a/examples/functions/sendTransactionalEmailsWithAttachment.md b/examples/functions/sendTransactionalEmailsWithAttachment.md new file mode 100644 index 0000000..ba62eee --- /dev/null +++ b/examples/functions/sendTransactionalEmailsWithAttachment.md @@ -0,0 +1,72 @@ +# Send Transactional Emails With Attachment + +This example is aligned with the current SDK and can be run directly from `examples/functions/sendTransactionalEmailsWithAttachment.py`. + +```python +import ElasticEmail +from pprint import pprint +import base64 +from pathlib import Path + +configuration = ElasticEmail.Configuration( + host = "https://api.elasticemail.com/v4" +) + +configuration.api_key['apikey'] = "6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF" + +with ElasticEmail.ApiClient(configuration) as api_client: + api_instance = ElasticEmail.EmailsApi(api_client) + + invoice_path = Path(__file__).with_name("invoice.pdf") + if not invoice_path.exists(): + print(f"Attachment file not found: {invoice_path}. Skipping send.") + raise SystemExit(0) + + with open(invoice_path, 'rb') as file: + binData = file.read() + + email_transactional_message_data = ElasticEmail.EmailTransactionalMessageData( + Recipients=ElasticEmail.TransactionalRecipient( + To=[ + "RECIPIENT@EMAIL.ADDRESS", + ], + ), + Content=ElasticEmail.EmailContent( + Body=[ + ElasticEmail.BodyPart( + ContentType=ElasticEmail.BodyContentType("HTML"), + Content="Mail content.", + Charset="utf-8", + ), + ElasticEmail.BodyPart( + ContentType=ElasticEmail.BodyContentType("PlainText"), + Content="Mail content.", + Charset="utf-8", + ), + ], + From="SENDER@EMAIL.ADDRESS", + ReplyTo="SENDER@EMAIL.ADDRESS", + Subject="Example transactional email with attachment", + Attachments=[ + ElasticEmail.MessageAttachment( + BinaryContent=base64.b64encode(binData).decode('utf-8'), + Name="Invoice.pdf" + ) + ] + ), + ) + + + try: + api_response = api_instance.emails_transactional_post(email_transactional_message_data) + print("The response of EmailsApi->emails_transactional_post:\n") + pprint(api_response) + except Exception as e: + print("Exception when calling EmailsApi->emails_transactional_post: %s\n" % e) +``` + +Run with: + +```bash +py -3 sendTransactionalEmailsWithAttachment.py +``` diff --git a/examples/functions/sendTransactionalEmailsWithAttachment.py b/examples/functions/sendTransactionalEmailsWithAttachment.py index de898b3..eb36ba4 100755 --- a/examples/functions/sendTransactionalEmailsWithAttachment.py +++ b/examples/functions/sendTransactionalEmailsWithAttachment.py @@ -1,57 +1,60 @@ -import ElasticEmail -from ElasticEmail.models.email_send import EmailSend -from ElasticEmail.models.email_transactional_message_data import EmailTransactionalMessageData -from ElasticEmail.rest import ApiException -from pprint import pprint -import base64 - -configuration = ElasticEmail.Configuration( - host = "https://api.elasticemail.com/v4" -) - -configuration.api_key['apikey'] = "API_KEY" - -with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = ElasticEmail.EmailsApi(api_client) - - with open('invoice.pdf', 'rb') as file: - binData = file.read() - - email_transactional_message_data = ElasticEmail.EmailTransactionalMessageData( - Recipients=ElasticEmail.TransactionalRecipient( - To=[ - "RECIPIENT@EMAIL.ADDRESS", - ], - ), - Content=ElasticEmail.EmailContent( - Body=[ - ElasticEmail.BodyPart( - ContentType=ElasticEmail.BodyContentType("HTML"), - Content="Mail content.", - Charset="utf-8", - ), - ElasticEmail.BodyPart( - ContentType=ElasticEmail.BodyContentType("PlainText"), - Content="Mail content.", - Charset="utf-8", - ), - ], - From="SENDER@EMAIL.ADDRESS", - ReplyTo="SENDER@EMAIL.ADDRESS", - Subject="Example transactional email with attachment", - Attachments=[ - ElasticEmail.MessageAttachment( - BinaryContent=base64.b64encode(binData).decode('utf-8'), - Name="Invoice.pdf" - ) - ] - ), - ) - - - try: - api_response = api_instance.emails_transactional_post(email_transactional_message_data) - print("The response of EmailsApi->emails_transactional_post:\n") - pprint(api_response) - except Exception as e: - print("Exception when calling EmailsApi->emails_transactional_post: %s\n" % e) +import ElasticEmail +from pprint import pprint +import base64 +from pathlib import Path + +configuration = ElasticEmail.Configuration( + host = "https://api.elasticemail.com/v4" +) + +configuration.api_key['apikey'] = "YOUR_API_KEY" + +with ElasticEmail.ApiClient(configuration) as api_client: + api_instance = ElasticEmail.EmailsApi(api_client) + + invoice_path = Path(__file__).with_name("invoice.pdf") + if not invoice_path.exists(): + print(f"Attachment file not found: {invoice_path}. Skipping send.") + raise SystemExit(0) + + with open(invoice_path, 'rb') as file: + binData = file.read() + + email_transactional_message_data = ElasticEmail.EmailTransactionalMessageData( + Recipients=ElasticEmail.TransactionalRecipient( + To=[ + "RECIPIENT@EMAIL.ADDRESS", + ], + ), + Content=ElasticEmail.EmailContent( + Body=[ + ElasticEmail.BodyPart( + ContentType=ElasticEmail.BodyContentType("HTML"), + Content="Mail content.", + Charset="utf-8", + ), + ElasticEmail.BodyPart( + ContentType=ElasticEmail.BodyContentType("PlainText"), + Content="Mail content.", + Charset="utf-8", + ), + ], + From="SENDER@EMAIL.ADDRESS", + ReplyTo="SENDER@EMAIL.ADDRESS", + Subject="Example transactional email with attachment", + Attachments=[ + ElasticEmail.MessageAttachment( + BinaryContent=base64.b64encode(binData).decode('utf-8'), + Name="Invoice.pdf" + ) + ] + ), + ) + + + try: + api_response = api_instance.emails_transactional_post(email_transactional_message_data) + print("The response of EmailsApi->emails_transactional_post:\n") + pprint(api_response) + except Exception as e: + print("Exception when calling EmailsApi->emails_transactional_post: %s\n" % e) diff --git a/examples/functions/updateCampaign.md b/examples/functions/updateCampaign.md index 69354b9..08bec92 100755 --- a/examples/functions/updateCampaign.md +++ b/examples/functions/updateCampaign.md @@ -1,120 +1,29 @@ # 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: +This example is aligned with the current SDK and can be run directly from `examples/functions/updateCampaign.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api -from ElasticEmail.model.campaign import Campaign -from ElasticEmail.model.campaign_recipient import CampaignRecipient -from ElasticEmail.model.campaign_status import CampaignStatus -from ElasticEmail.model.campaign_template import CampaignTemplate +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 - -```python +# 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. - -```python - api_instance = campaigns_api.CampaignsApi(api_client) -``` - -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. - - -```python - 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( - ListNames=[ - "my list name", - ], - ), - ) -``` - -Use try & except block to call `campaigns_by_name_put` method from the API to update a campaign: - -```python - try: - api_response = api_instance.campaigns_by_name_put(path_params={'name': name}, body = 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: -```python -import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api -from ElasticEmail.model.campaign import Campaign -from ElasticEmail.model.campaign_recipient import CampaignRecipient -from ElasticEmail.model.campaign_status import CampaignStatus -from ElasticEmail.model.campaign_template import CampaignTemplate -from pprint import pprint - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' +""" +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: - api_instance = campaigns_api.CampaignsApi(api_client) - + # Create an instance of the API class + api_instance = ElasticEmail.CampaignsApi(api_client) name = "hello campaign" campaign = Campaign( @@ -129,20 +38,23 @@ with ElasticEmail.ApiClient(configuration) as api_client: Name="hello campaign update", Status=CampaignStatus("Draft"), Recipients=CampaignRecipient( - ListNames=[ + list_names=[ "my list name", ], ), - ) + ) # Campaign | JSON representation of a campaign + # example passing only required values which don't have defaults set try: - api_response = api_instance.campaigns_by_name_put(path_params = {'name': name}, body = campaign) + # 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 +Run with: + +```bash +py -3 updateCampaign.py ``` diff --git a/examples/functions/updateCampaign.py b/examples/functions/updateCampaign.py index 3ea28c1..ad8ba92 100755 --- a/examples/functions/updateCampaign.py +++ b/examples/functions/updateCampaign.py @@ -1,9 +1,8 @@ import ElasticEmail -from ElasticEmail.apis.tags import campaigns_api -from ElasticEmail.model.campaign import Campaign -from ElasticEmail.model.campaign_recipient import CampaignRecipient -from ElasticEmail.model.campaign_status import CampaignStatus -from ElasticEmail.model.campaign_template import CampaignTemplate +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 @@ -19,8 +18,7 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = campaigns_api.CampaignsApi(api_client) - + api_instance = ElasticEmail.CampaignsApi(api_client) name = "hello campaign" campaign = Campaign( @@ -35,7 +33,7 @@ Name="hello campaign update", Status=CampaignStatus("Draft"), Recipients=CampaignRecipient( - ListNames=[ + list_names=[ "my list name", ], ), @@ -44,7 +42,7 @@ # example passing only required values which don't have defaults set try: # Add Campaign - api_response = api_instance.campaigns_by_name_put(path_params = {'name': name}, body = 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) diff --git a/examples/functions/uploadContacts.md b/examples/functions/uploadContacts.md index a41881b..986e92e 100755 --- a/examples/functions/uploadContacts.md +++ b/examples/functions/uploadContacts.md @@ -1,121 +1,45 @@ # Upload Contacts -This guide will walk you through the process of adding new contacts to your account, by uploading them from a CSV file, using the Python library. - -*Required Access Level: ModifyContacts* - -## What's a contact? -When using Elastic Email, you send emails to contacts – recipients who receive your emails. Contacts can be grouped by created segments or lists. - -## Preparation -Install Python 3. - -Install `elasticemail-python` lib - -Eg. run in terminal `pip install git+https://github.com/elasticemail/elasticemail-python.git` - -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: +This example is aligned with the current SDK and can be run directly from `examples/functions/uploadContacts.py`. ```python import ElasticEmail -from ElasticEmail.apis.tags import contacts_api -``` -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 - -```python +# 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: -``` +# Configure API key authorization: apikey +configuration.api_key['apikey'] = '6E313A3326A5C0A3D0C5F32916BE6EE3E858AA4C068829DEF4B6D21E0A12971AD60B3F92330DFDD1F193916BDE8444AF' -Create an instance of ContactsApi that will be used to upload contacts. - -```python - api_instance = contacts_api.ContactsApi(api_client) -``` +""" +Upload contacts +Example api call that adds new contacts by uploading csv file. +Required columns in CSV file: Email. +Suggested columns in CSV file: AllowUnsubscribe, Status, ConsentDate, ConsentIP, ConsentTracking. +Example CSV file content: -Create options -- file encoding -- optionaly a list name to which contacts should be added, otherwise contacts will be added to all contacts. - -```python - query_params = { - 'listName': "example list", - 'encodingName': "utf-8", - } -``` - -The simplest CSV file requires only one column `Email`, eg.: - -``` Email -john@johnsmith.com -``` - -> Find out more by checking our API's documentation: https://elasticemail.com/developers/api-documentation/rest-api#operation/contactsImportPost - -Load file - -```python - body = dict( - file = open('./files/contacts.csv', 'rb'), - ) -``` - -Use try & except block to call `contacts_import_post` method from the API to upload contacts: - -```python - try: - api_instance.contacts_import_post(query_params=query_params, body=body) - print("Contacts uploaded.") - except ElasticEmail.ApiException as e: - print("Exception when calling ContactsApi->contacts_import_post: %s\n" % e) -``` - - -## The whole code to copy and paste: - -```python -import ElasticEmail -from ElasticEmail.apis.tags import contacts_api - -configuration = ElasticEmail.Configuration() -configuration.api_key['apikey'] = 'YOUR_API_KEY' - +john@domain.com +""" with ElasticEmail.ApiClient(configuration) as api_client: - api_instance = contacts_api.ContactsApi(api_client) - - query_params = { - 'listName': "example list", - 'encodingName': "utf-8", - } - - body = dict( - file = open('./files/contacts.csv', 'rb'), - ) + # Create an instance of the API class + api_instance = ElasticEmail.ContactsApi(api_client) try: - api_instance.contacts_import_post(query_params=query_params, body=body) - print("Contacts uploaded.") + with open('./files/contacts.csv', 'rb') as contacts_file: + api_instance.contacts_import_post( + list_name="example list", + encoding_name="utf-8", + file=contacts_file.read(), + ) + print("Contacts uploaded.") except ElasticEmail.ApiException as e: print("Exception when calling ContactsApi->contacts_import_post: %s\n" % e) ``` -## Run the code -``` -python3 snippet.py +Run with: + +```bash +py -3 uploadContacts.py ``` diff --git a/examples/functions/uploadContacts.py b/examples/functions/uploadContacts.py index 8b80f70..548c84f 100755 --- a/examples/functions/uploadContacts.py +++ b/examples/functions/uploadContacts.py @@ -1,5 +1,4 @@ import ElasticEmail -from ElasticEmail.apis.tags import contacts_api # Defining the host is optional and defaults to https://api.elasticemail.com/v4 configuration = ElasticEmail.Configuration() @@ -20,19 +19,15 @@ """ with ElasticEmail.ApiClient(configuration) as api_client: # Create an instance of the API class - api_instance = contacts_api.ContactsApi(api_client) - - query_params = { - 'listName': "example list", # str | Name of an existing list to add these contacts to (optional) - 'encodingName': "utf-8", # str | In what encoding the file is uploaded (optional) - } - - body = dict( - file = open('./files/contacts.csv', 'rb'), - ) + api_instance = ElasticEmail.ContactsApi(api_client) try: - api_instance.contacts_import_post(query_params=query_params, body=body) - print("Contacts uploaded.") + with open('./files/contacts.csv', 'rb') as contacts_file: + api_instance.contacts_import_post( + list_name="example list", + encoding_name="utf-8", + file=contacts_file.read(), + ) + print("Contacts uploaded.") except ElasticEmail.ApiException as e: print("Exception when calling ContactsApi->contacts_import_post: %s\n" % e)