Created
September 23, 2011 18:12
-
-
Save GrooveStomp/1238052 to your computer and use it in GitHub Desktop.
Unbounce WebHook Email
This file contains 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 | |
// This is a sample PHP script that demonstrates accepting a POST from the | |
// Unbounce form submission webhook, and then sending an email notification. | |
function stripslashes_deep($value) { | |
$value = is_array($value) ? | |
array_map('stripslashes_deep', $value) : | |
stripslashes($value); | |
return $value; | |
} | |
// This will traverse $form_data, collecting every key, value pair. | |
// For instance, if the JSON form data looks like: | |
// { | |
// "email": ["support@unbounce.com"], | |
// "first_name": ["Joe"], | |
// "last_name": ["Support"], | |
// "selections": [1, 2, 3, 4, 5] | |
// } | |
// Then then string built below will look like: | |
// email: support@unbounce.com | |
// first_name: Joe | |
// last_name: Support | |
// selections: 1, 2, 4, 5 | |
function form_data_as_string($form_data) { | |
$string = ""; | |
// Traverse the object as $key => $value pairs. | |
foreach($form_data as $key => $value) { | |
$string .= "$key: "; | |
// Check the length of the $value array. | |
if (count($value) == 1) { | |
// If there's just one item, then append it to $string. | |
$string .= "$value[0]\n"; | |
} | |
else { | |
// Otherwise loop through all elements in the $value array, | |
// appending each to $string. | |
for ($i = 0; $i < count($value)-1; $i++) { | |
$string .= "$value[$i], "; | |
} | |
$string .= "$value[$i]\n"; | |
} | |
} | |
return $string; | |
} | |
// First, grab the form data. Some things to note: | |
// 1. PHP replaces the '.' in 'data.json' with an underscore. | |
// 2. Your fields names will appear in the JSON data in all lower-case, | |
// with underscores for spaces. | |
// 3. We need to handle the case where PHP's 'magic_quotes_gpc' option | |
// is enabled and automatically escapes quotation marks. | |
if (get_magic_quotes_gpc()) { | |
$unescaped_post_data = stripslashes_deep($_POST); | |
} else { | |
$unescaped_post_data = $_POST; | |
} | |
$form_data = json_decode($unescaped_post_data['data_json']); | |
// If your form data has an 'Email Address' field, here's how you extract | |
// explicitly by name. | |
// Note: look at form_data_as_string above if you want to extract | |
// all fields and you don't necessarily know what they are before hand. | |
$email_address = $form_data->email_address[0]; | |
// Grab the remaining page data. | |
$page_id = $_POST['page_id']; | |
$page_url = $_POST['page_url']; | |
$variant = $_POST['variant']; | |
// Now send an email. | |
// $_REQUEST['email_address'] is the 'email_address' query parameter. | |
// For example, using this curl invocation would send an email to | |
// "support@unbounce.com": | |
// curl http://my-domain.com/this-php-script.php?email_address=support@unbounce.com | |
mail($_REQUEST['email_address'], | |
'New Unbounce Form Submission!', | |
form_data_as_string($form_data) . | |
"\nPage ID: $page_id" . | |
"\nPage URL: $page_url" . | |
"\nVariant: $variant" | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment