Skip to content

Instantly share code, notes, and snippets.

@astagi
Created August 10, 2019 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astagi/93cbb32f43bcb9ac019a3640ecc6ab9b to your computer and use it in GitHub Desktop.
Save astagi/93cbb32f43bcb9ac019a3640ecc6ab9b to your computer and use it in GitHub Desktop.
from createsend import Subscriber
from createsend.createsend import BadRequest
class CampaignMonitorException(Exception):
pass
class CampaignMonitorListSubscriber():
def __init__(self, api_key, list_id):
self._api_key = api_key
self._list_id = list_id
def subscribe(self, email, name, custom_fields, resubscribe, consent_to_track):
try:
subscriber = Subscriber(
{'api_key': self._api_key},
self._list_id
)
subscriber.add(list_id, email, name, custom_fields, resubscribe, consent_to_track)
except BadRequest as ex:
raise CampaignMonitorException(str(ex))
def unsubscribe(self, email):
try:
subscriber = Subscriber(
{'api_key': self._api_key},
self._list_id,
email,
)
subscriber.unsubscribe()
except BadRequest as ex:
raise CampaignMonitorException(str(ex))
def delete(self, email):
try:
subscriber = Subscriber(
{'api_key': self._api_key},
self._list_id,
email,
)
subscriber.delete()
except BadRequest as ex:
raise CampaignMonitorException(str(ex))
if __name__ == '__main__':
api_key = 'APIK3YAPIK3YAPIK3YAPIK3Y'
list_id = 'L1ST1DL1ST1DL1ST1DL1ST1D'
cmls = CampaignMonitorListSubscriber(api_key, list_id)
try:
cmls.subscribe("mymail@gmail.com", "Andrea Stagi", None, True, 'yes')
cmls.unsubscribe("mymail@gmail.com")
cmls.delete("mymail@gmail.com")
except CampaignMonitorException as ex:
print (ex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment