tony-cain of Papercut Interactive
11/26/2018 - 9:34 PM

myEmma Signup Form

This snippet uses the myEmma API to sign users up to a mailing list. It uses a php filter for server-side email validation. I've commented out the "Name" fields since they are rarely used.

<form class="emma-form">
		<h6>Sign Up for Our Newsletter!</h6>
	<div>
    <!-- for ee -->
    <input type="hidden" name="csrf_token" value="{csrf_token}">
		<input id="email" name="email" placeholder="Email" type="text">
		<input id="emma-submit" type="submit" value="">
	</div>

</form>
<div class="printArea"></div>
$('.emma-form').on('submit',function(e){
				e.preventDefault();
				$.ajax({
					type     : "POST",
					cache    : false,
					/* this is the php file */
					url      : "/inc/emma-signup",
					data     : $(this).serialize(),
					success  : function(data) {
						$('.printArea').html(data)
					}
				});
	
			});
<?php
// Authentication Variables
//place keys here
$account_id = "xxx";
$public_api_key = "xxx";
$private_api_key = "xxx";

// Form variable(s)
$email = $_POST['email'];
// $first_name = $_POST['first_name'];
// $last_name = $_POST['last_name'];
//place the group id here
$groups = array(xxx);

//custom php validation for email addresses
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo("Thank You for Signing up! Your Email has been added!");
} else {
    echo("$email is not a valid email address");
}


// Member data other than email should be passed in an array called "fields"
$member_data = array(
  "email" => $email,
//   "fields" => array(
//     "first_name" => $first_name,
//     "last_name" => $last_name
//   ),
  "group_ids" => $groups
);

// Set URL
$url = "https://api.e2ma.net/".$account_id."/members/add";

// setup and execute the cURL command
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, $public_api_key . ":" . $private_api_key);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($member_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($member_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

//execute post
if($http_code > 200) {
  $app_message = "Error sending subscription request";
} else {
  $app_message = "Success!";
}

// echo $app_message;
?>