def AddSubscriberToList(self, emailAddress, listIDs, subscriberKey = None): """ add or update a subscriber with a list """ newSub = ET_Subscriber() newSub.auth_stub = self lists = [] for p in listIDs: lists.append({"ID" : p}) newSub.props = {"EmailAddress" : emailAddress, "Lists" : lists} if subscriberKey is not None: newSub.props['SubscriberKey'] = subscriberKey # Try to add the subscriber postResponse = newSub.post() if not postResponse.status: # If the subscriber already exists in the account then we need to do an update. # Update Subscriber On List if postResponse.results[0]['ErrorCode'] == 12014: patchResponse = newSub.patch() return patchResponse return postResponse
def get_or_create_subscriber(cls, email, email_xml_context=None): et_client = cls.get_client() et_subscriber = ET_Subscriber() et_subscriber.auth_stub = et_client et_subscriber.props = ["SubscriberKey", "EmailAddress", "Status"] et_subscriber.search_filter = { 'Property': 'SubscriberKey', 'SimpleOperator': 'equals', 'Value': email } get_response = et_subscriber.get() attributes = {} if email_xml_context: attributes = { 'Attributes': [{ 'Name': "XML", 'Value': email_xml_context }] } if get_response.code == 200 and len(get_response.results) == 0: et_subscriber.props = { "SubscriberKey": email, "EmailAddress": email, } if attributes: et_subscriber.props.update(attributes) et_subscriber.post() elif get_response.code == 200: subscriber = get_response.results[0] et_subscriber.props = { "SubscriberKey": email, } patch = False if attributes: et_subscriber.props.update(attributes) patch = True if not subscriber.Status != 'Active': et_subscriber.props.update({"Status": "Active"}) patch = True if patch: et_subscriber.patch() return {'EmailAddress': email, 'SubscriberKey': email}
def get_or_create_subscriber(cls, email, email_xml_context=None): et_client = cls.get_client() et_subscriber = ET_Subscriber() et_subscriber.auth_stub = et_client et_subscriber.props = ["SubscriberKey", "EmailAddress", "Status"] et_subscriber.search_filter = {"Property": "SubscriberKey", "SimpleOperator": "equals", "Value": email} get_response = et_subscriber.get() attributes = {} if email_xml_context: attributes = {"Attributes": [{"Name": "XML", "Value": email_xml_context}]} if get_response.code == 200 and len(get_response.results) == 0: et_subscriber.props = {"SubscriberKey": email, "EmailAddress": email} if attributes: et_subscriber.props.update(attributes) et_subscriber.post() elif get_response.code == 200: subscriber = get_response.results[0] et_subscriber.props = {"SubscriberKey": email} patch = False if attributes: et_subscriber.props.update(attributes) patch = True if not subscriber.Status != "Active": et_subscriber.props.update({"Status": "Active"}) patch = True if patch: et_subscriber.patch() return {"EmailAddress": email, "SubscriberKey": email}