Created
August 1, 2013 04:21
-
-
Save rothwerx/6128390 to your computer and use it in GitHub Desktop.
PHP: Custom Drupal (5) module for Bronto email APIv4. Only putting it here because the internet lacks good examples of the Bronto API. Add error handling as needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function bronto_menu($may_cache) { | |
| $items = array(); | |
| if ($may_cache) { | |
| $items[] = array( | |
| 'path' => 'admin/mymodule/settings/bronto', | |
| 'access' => user_access('administer site configuration'), | |
| 'title' => t('Bronto settings'), | |
| 'description' => t('Configure Bronto login'), | |
| 'callback' => 'drupal_get_form', | |
| 'callback arguments' => array('bronto_settings_form') | |
| ); | |
| } | |
| return $items; | |
| } | |
| function bronto_settings_form() { | |
| $form = array(); | |
| $form['bronto_api_key'] = array( | |
| '#type' => 'textfield', | |
| '#title' => t('Bronto API Key'), | |
| '#default_value' => variable_get('bronto_api_key', ''), | |
| '#description' => t('In version 4 of the Bronto API, the API key replaces username/password.') | |
| ); | |
| $form['bronto_footer'] = array( | |
| '#type' => 'textarea', | |
| '#title' => t('footer message'), | |
| '#default_value' => variable_get('bronto_footer', ''), | |
| '#description' => t('Enter text and or html for footer in emails.') | |
| ); | |
| return system_settings_form($form); | |
| } | |
| /* | |
| * Create and send a message using Bronto APIv4. | |
| * Returns id, isError, isNew, and errorCode, as well as | |
| * errorString if there's an error. Later we can use the id | |
| * to pull back stats for the delivery, I believe. | |
| */ | |
| function bronto_send_message($list_id, $subject, $body, $from_email, $from_name, $type = 'text') { | |
| $binding = bronto_get_binding(); | |
| $now = date('c'); | |
| $deliveryRecipientObject = array('type' => 'list', | |
| 'id' => $list_id); | |
| $message = bronto_create_message($subject, $body, 'html'); | |
| $delivery = array( | |
| 'start' => $now, | |
| 'messageId' => $message->id, | |
| 'fromName' => $from_name, | |
| 'fromEmail' => $from_email, | |
| 'replyEmail' => $from_email, | |
| 'recipients' => array($deliveryRecipientObject), | |
| ); | |
| $result = $binding->addDeliveries(array($delivery))->return; | |
| return $result->results; | |
| } | |
| /* | |
| * Update an email address. Returns nothing, but prints to watchdog. | |
| */ | |
| function bronto_change_email_for_contact($old_email, $new_email) { | |
| $binding = bronto__get_binding(); | |
| $contact = bronto_get_status($old_email); | |
| if ($contact) { | |
| $contact->email = $new_email; | |
| } | |
| $result = $binding->addOrUpdateContacts(array($contact))->return; | |
| if ($result->isError) { | |
| watchdog('bronto', t('bronto_change_email_for_contact failure for %email Reason given: | |
| %error_string', array('%email' => $email, '%error_string' => $result->errorString)), WATCHDOG_NOTICE); | |
| } elseif ($result) { | |
| watchdog('bronto', t('Changed email for %old_email to %new_email', array('%old_email' => $old_email, | |
| '%new_email' => $new_email)), WATCHDOG_INFO); | |
| } | |
| } | |
| /* | |
| * Given an array of either email(s) or contact ID(s), | |
| * and a list ID, remove the contact(s) from the list. | |
| */ | |
| function bronto_unsubscribe($ids, $list_id, $by_email = FALSE) { | |
| $binding = bronto_get_binding(); | |
| $contacts = array(); | |
| if ($by_email) { | |
| foreach($ids as $id) { | |
| $contacts[] = array('email' => $id); | |
| } | |
| } | |
| else { | |
| foreach($ids as $id) { | |
| $contacts[] = array('id' => $id); | |
| } | |
| } | |
| $to_rem = array( | |
| "list" => array('id' => $list_id), | |
| "contacts" => $contacts); | |
| $result = $binding4->removeFromList($to_rem)->return; | |
| return $result->results; | |
| } | |
| /* | |
| * Given a list ID, change the name and label. | |
| * Return results for chaper.module | |
| */ | |
| function bronto_rename_list($list_id, $name) { | |
| $binding = bronto_get_binding(); | |
| $list = bronto_get_list($list_id); | |
| if ($list) { | |
| $list->name = $name; | |
| $list->label = $name; | |
| } | |
| $result = $binding->updateLists(array($list))->return; | |
| return $result->results; | |
| } | |
| /* | |
| * Returns an array of all the lists + info: | |
| * id, name, label, activeCount, status | |
| */ | |
| function bronto_get_all_lists() { | |
| $binding = bronto_get_binding(); | |
| $filter = array(); | |
| $lists = $binding->readLists(array('pageNumber' => 1, | |
| 'filter' => $filter))->return; | |
| return $lists; | |
| } | |
| /* | |
| * Given a list name or id, return an object with | |
| * id, name, label, activeCount, and status | |
| */ | |
| function bronto_get_list($list_id, $by_name = FALSE) { | |
| $binding = bronto_get_binding(); | |
| if ($by_name) { | |
| $filter = array( | |
| // "name" is a stringValue | |
| "name" => array( | |
| array("operator" => "EqualTo", "value" => $list_id))); | |
| } else { | |
| $filter = array( | |
| // "id" is a string | |
| "id" => $list_id); | |
| } | |
| $result = $binding->readLists(array( | |
| "filter" => $filter, | |
| "pageNumber" => 1)); | |
| return $result->return; | |
| } | |
| /* | |
| * Create a new list with the given name. | |
| * Returns results for chapter.module to work with. | |
| */ | |
| function bronto_create_list($list_name) { | |
| $binding = bronto_get_binding(); | |
| $list_params = array('name' => $list_name, 'label' => $list_name); | |
| $result = $binding->addLists(array($list_params))->return; | |
| return $result->results; | |
| } | |
| /* | |
| * Get a messageFolder for a given folder name. | |
| */ | |
| function bronto_get_message_folder_by_name($name) { | |
| $binding = bronto_get_binding(); | |
| $filter = array("name" => array("operator" => "EqualTo", | |
| "value" => $name)); | |
| $result = $binding->readMessageFolders(array( | |
| 'filter' => $filter, | |
| 'pageNumber' => 1))->return; | |
| return $result; | |
| } | |
| /* | |
| * Create a message in Bronto, return the message ID. | |
| * Used in _send_message(). | |
| */ | |
| function bronto_create_message($subject, $body, $type = 'text') { | |
| $binding = bronto_get_binding(); | |
| /* We're not using folders really, so no need to make an extra call | |
| $message_folder = bronto_get_message_folder_by_name('Messages'); | |
| if (!$message_folder) { | |
| return FALSE; | |
| } */ | |
| $bmid = db_next_id('{bronto_messages}_bmid'); | |
| // Add a custom footer to each message | |
| $body .= variable_get('bronto_footer', ''); | |
| $MessageContentObject = array( | |
| 'subject' => $subject, | |
| 'content' => $body, | |
| 'type' => $type); | |
| $name = $bmid . ': ' . $subject; | |
| $message = array('name' => $name, | |
| 'content' => $MessageContentObject); | |
| // 'messageFolderId' => $message_folder->id); | |
| $result = $binding->addMessages(array($message))->return; | |
| return $result->results; | |
| } | |
| /* | |
| * Get all contacts for a given list ID. | |
| */ | |
| function bronto_get_contacts($list_id) { | |
| $binding = bronto_get_binding(); | |
| $filter = array("listId" => $list_id); | |
| $result = $binding->readContacts(array( | |
| "filter" => $filter, | |
| // Bronto current limit is 5000 per page. | |
| // our module limits to 1000 so we don't need more than one page. | |
| "pageNumber" => 1 | |
| ))->return; | |
| return $result; | |
| } | |
| /* | |
| * Not implemented, but an example of how to pull specific lists. | |
| */ | |
| function bronto_get_ny_chapters() { | |
| $binding = bronto_get_binding(); | |
| $filter = array( | |
| "name" => array( | |
| array("operator" => "EndsWith", "value" => ', NY'))); | |
| $result = $binding->readLists(array("filter" => $filter, | |
| "pageNumber" => 1))->return; | |
| $chapters = array(); | |
| foreach($result as $chapter) { | |
| $chapters[$chapter->name] = $chapter->id; | |
| } | |
| return $chapters; | |
| } | |
| function bronto_login() { | |
| ini_set("soap.wsdl_cache_enabled", "0"); | |
| date_default_timezone_set('America/New_York'); | |
| $wsdl = "https://api.bronto.com/v4?wsdl"; | |
| $url = "https://api.bronto.com/v4"; | |
| $client = new SoapClient($wsdl, array('trace' => 1, 'encoding' => 'UTF-8')); | |
| $client->__setLocation($url); | |
| // Login | |
| $token = variable_get('bronto_api_key', ''); | |
| $sessionId = $client->login(array("apiToken" => $token))->return; | |
| $client->__setSoapHeaders(array(new SoapHeader("http://api.bronto.com/v4", | |
| 'sessionHeader', | |
| array('sessionId' => $sessionId)))); | |
| return $client; | |
| } | |
| /* | |
| * Takes email(s) as an array and a list ID scalar. | |
| */ | |
| function bronto_subscribe($emails, $list_id) { | |
| //This one may make no sense because I pulled out some code for the public gist - | |
| //some ugly hard-coded values I haven't moved to a serialized variable yet... | |
| $binding = bronto_get_binding(); | |
| $contacts = array(); | |
| foreach($emails as $email) { | |
| unset($lists); | |
| // First, check to see if the contact is unsubscribed or in a bounce state, and set them to onboarding if so | |
| $contact_status = bronto_get_status($email); | |
| if ($contact_status) { | |
| if ($contact_status->status == 'unsub' || $contact_status->status == 'bounce') { | |
| $contact_activate = bronto_make_active($email); | |
| } | |
| } | |
| foreach($contact_status->listIds as $list) { | |
| $lists[] = $list; | |
| } | |
| $lists[] = $list_id; | |
| // If they're subscribing to a NY list, add them to the NYC Newsletter | |
| if (in_array($list_id, $ny_lists)) { | |
| $lists[] = $nyc_newsletter; | |
| } | |
| $contacts[] = array('customSource' => 'bronto_subscribe', 'email' => $email, 'listIds' => $lists); | |
| try { | |
| $subscribe_result = $binding->addOrUpdateContacts($contacts)->return; | |
| } | |
| catch (Exception $e) { | |
| watchdog('bronto', t('Bronto exception on subscribe: %value', array('%value' => $e->detail->fault->code . ' :: ' . $e->detail->fault->message)), WATCHDOG_ERROR); | |
| return FALSE; | |
| } | |
| } | |
| return $subscribe_result->results; | |
| } | |
| function bronto_make_active($email) { | |
| $binding = bronto_get_binding(); | |
| // We can't make someone active, but we can set their status to onboarding | |
| $contact = array( | |
| array("email" => $email, "status" => "onboarding") | |
| ); | |
| $result = $binding->updateContacts(array("contacts" => $contact))->return->results; | |
| if ($result->isError) { | |
| watchdog('bronto', t('bronto_make_active error. Could not transition %email to onboarding status. Error: %error_string', array('%email' => $email, '%error_string' => $result->errorString)), WATCHDOG_ERROR); | |
| return $result; | |
| } | |
| watchdog('bronto', t('bronto_make_active: Reactivated %email', array('%email' => $email)), WATCHDOG_NOTICE); | |
| return $result; | |
| } | |
| function bronto_get_status($email) { | |
| // This will give a full readContacts for a given contact, so theoretically it can be used elsewhere | |
| $binding = bronto_get_binding(); | |
| //Get the status of the contact, one of: active, onboarding, transactional, bounce, unconfirmed, or unsub | |
| $filter = array( | |
| "email" => array(array("operator" => "EqualTo", "value" => $email)) | |
| ); | |
| $result = $binding->readContacts(array( | |
| "filter" => $filter, | |
| "includeLists" => true, | |
| "fields" => null, | |
| "pageNumber" => 1 | |
| ))->return; | |
| if ($result->isError) { | |
| watchdog('bronto', t('bronto_get_status: Could not read contact info for %email Reason given: %error_string', array('%email' => $email, '%error_string' => $result->errorString)), WATCHDOG_NOTICE); | |
| } elseif ($result) { | |
| return $result; | |
| } else { | |
| return FALSE; | |
| } | |
| } | |
| /*** | |
| readContacts provides this: | |
| stdClass Object | |
| ( | |
| [id] => 8f3eff9d-81d4-4189-be5d-e2bd8c7363a0 | |
| [email] => phfh@me.com | |
| [status] => bounce | |
| [msgPref] => html | |
| [source] => api | |
| [customSource] => bronto_subscribe | |
| [created] => 2011-10-05T04:28:43Z | |
| [modified] => 2011-10-05T17:25:21Z | |
| [deleted] => | |
| [listIds] => 0bba03ec000000000000000000000002ec3c | |
| [numSends] => 1 | |
| [numBounces] => 1 | |
| [numOpens] => 0 | |
| [numClicks] => 0 | |
| [numConversions] => 0 | |
| [conversionAmount] => 0 | |
| ) | |
| **/ | |
| function bronto_verify_contact_on_list($email, $listId) { | |
| // Remove echos and prints if this is ever to be used in Drupal | |
| $binding = bronto_get_binding(); | |
| // Verify the contact is on the list | |
| $filter = array( | |
| "email" => array(array("operator" => "EqualTo", "value" => $email)) | |
| ); | |
| $result = $binding->readContacts(array( | |
| "filter" => $filter, | |
| "includeLists" => true, | |
| "fields" => null, | |
| "pageNumber" => 1 | |
| ))->return; | |
| if (!in_array($listId, $result->listIds)) { | |
| echo "Contact is not on the list!\n"; | |
| print_r($result->listIds); | |
| return; | |
| } | |
| echo "Contact is on the list.\n"; | |
| } | |
| function bronto_get_binding() { | |
| static $binding; | |
| if (!$binding) { | |
| $binding = bronto_login(); | |
| } | |
| return $binding; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment