Skip to content

Instantly share code, notes, and snippets.

@corbanb
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save corbanb/11296407 to your computer and use it in GitHub Desktop.

Select an option

Save corbanb/11296407 to your computer and use it in GitHub Desktop.
Simple method for subscribing and unsubscribing up users in Exact Target using the SOAP API w/ the savon gem

Thanks @MichaelAllenClark for the help with his examples. Not sure why this info wasn't on the code.exacttarget.com website to begin with.

def manage_newsletter_sub(email, role, subscribe, update = false)
et_client = Savon.client(
wsdl: ENV['ET_WSDL'],
endpoint: ENV['ET_ENDPOINT'],
wsse_auth: [ENV['ET_USERNAME'], ENV['ET_PASSWORD']],
raise_errors: true,
log: true,
open_timeout: 180,
read_timeout: 180
)
lists = []
lists.push({'ID' => 'XXXXXXXXX'}, {'ID' => 'XXXXXXXXX'})
opts = {}
if subscribe == 0
lists.each {|list| list['Status'] = 'Unsubscribed'}
else
lists.each {|list| list['Status'] = 'Active'}
end
if update
opts = {'SaveOptions' => [{'SaveOption' => {'PropertyName'=> "*", 'SaveAction' => "UpdateAdd"}}]}
end
subscriber = {}
subscriber['@xsi:type'] = "tns:Subscriber"
subscriber['EmailAddress'] = email
subscriber['Lists'] = lists
# subscriber['Attributes'] = [{"Name" =>"First Name", "Value" => "Example"}]
response = et_client.call(:create, :message => {'Objects' => subscriber, 'Options' => opts})
if !response.nil? then
envelope = response.hash[:envelope]
createresponse = envelope[:body][:create_response]
results = createresponse[:results]
if !results.kind_of?(Array)
results = [results]
end
if createresponse[:overall_status] == "OK"
logger.info 'Success'
results.each {|result| logger.info "Subscriber: #{result[:object][:id]} #{result[:object][:email_address]}"}
else
logger.error 'Failed Exact Target'
results.each {|result| logger.info "Failure Message: #{result[:status_message]}"}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment