status === 'error') { throw new Exception(json_encode($r->errors[0])); } $ec_id = $r->data->Email_Content_ID; if (!$ec_id) { throw new Exception("No email_content_id found"); } // EMAIL VALIDATION $ipost_api_url = $ipost_base_api_url . $client_token . '/emails/' . $ec_id . '/validate/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ipost_api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $rcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $r = json_decode($response); if ($r && $r->status === 'error') { throw new Exception(json_encode($r->errors[0])); } // DATA TABLE CREATION $ipost_api_url = $ipost_base_api_url . $client_token . '/DataTables/create/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ipost_api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // CREATE TABLE SCHEMA $proto = $decoded_data["contacts"][0]; // first element defines schema $field_names = array_keys($proto); if (!is_array($field_names)) { throw new Exception('"contacts" parameter must be an array of data field_names'); } $email_field_name = "email"; $attrs = array(); foreach ($field_names as $key) { switch(gettype($proto[$key])) { case "string" : $dtype = "Short Text"; break; case "boolean" : $dtype = "Boolean"; break; case "integer" : $dtype = "Integer"; break; default : $dtype = "Short Text"; break; } $field["Name"] = $key; $field["DataType"] = $dtype; $field["Required"] = false; $field["PrimaryKey"] = false; $field["Sendable"] = false; if ($key === 'email') { $email_field_name = $key; $field["DataType"] = "Email"; $field["Required"] = true; $field["PrimaryKey"] = true; $field["Sendable"] = true; } array_push($attrs, $field); } $payload = array(); $payload["Attributes"] = $attrs; $payload["DataTableName"] = preg_replace('/(\W+)/i' ,"_",$decoded_data['mailing_name'] . $name_suffix); $payload["Description"] = "API Upload"; $payload["ContentType"] = "Custom"; $payload["Sendable"] = true; $payload["SendableType"] = 1; $payload["Testing"] = false; $rk["Name"] = $email_field_name; $payload["RecipientKey"] = Array($rk); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); $response = curl_exec($ch); $rcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $r = json_decode($response); if ($r && $r->status === 'error') { throw new Exception(json_encode($r->errors[0])); } $cdt_id = $r->data->data_table_id; // DATABASE UPLOAD $ipost_api_url = $ipost_base_api_url . $client_token . '/DataTables/' . $cdt_id . '/addupdate/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ipost_api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $payload = json_encode(array('Records' => $contacts)); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); $response = curl_exec($ch); $rcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $r = json_decode($response); if ($r && $r->status === 'error') { throw new Exception(json_encode($r->errors[0])); } // AUTOMATION CREATION $ipost_api_url = $ipost_base_api_url . $client_token . '/automations/process/create/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ipost_api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $activity = array(); $activity[0]["type"] = "email"; $activity[0]["name"] = $decoded_data["mailing_name"] . $name_suffix; $activity[0]["from_name"] = $decoded_data["from"]["name"]; $activity[0]["from_address"] = $decoded_data["from"]["email"]; $activity[0]["email_id"] = $ec_id; $activity[0]["subject"] = $decoded_data["subject"]; $activity[0]["destination"][0]["destination_type"] = "cdt"; $activity[0]["destination"][0]["destination_ids"] = array($cdt_id); $schedule = array(); $schedule[0]["schedule_date"] = date('Y-m-d H:i:s', time() + 7*60*60); $schedule[0]["schedule_time"] = date('Y-m-d H:i:s', time() + 7*60*60); $schedule[0]["schedule_reccurence"] = "run-once"; $schedule[0]["email_frequency_limit"] = 0; $schedule[0]["EndDate"] = date('Y-m-d', time() + 60*60*24*30); $schedule[0]["EndOcurrences"] = 1; $payload = array(); $payload["Automation_Name"] = $decoded_data["mailing_name"] . $name_suffix; $payload["Automation_Status"] = "Active"; $payload["type"] = "email"; $payload["Automation_Activities"] = $activity; $payload["Automation_Schedule"] = $schedule; $payload["schedule_date"] = date('Y-m-d H:i:s', time() + 7*60*60); $payload["schedule_time_zone"] = "America/Los_Angeles"; $payload = json_encode($payload); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); $response = curl_exec($ch); $rcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $r = json_decode($response); if ($r && $r->status === 'error') { throw new Exception(json_encode($r->errors[0])); } $auto_id = $r->data->Automation_ID; if (!$ec_id) { throw new Exception("No automation_id found"); } // START AUTOMATION $ipost_api_url = $ipost_base_api_url . $client_token . '/automations/' . $auto_id . '/startProcess/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ipost_api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $rcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $r = json_decode($response); if ($r && $r->status === 'error') { throw new Exception(json_encode($r->errors[0])); } $ipost_api_url = 'http://g003.enterprise.ipost.com/elliancedigitalmediallc/f2f/9z4zlv51f3f24kc347orl0gfmpmabtuuoas5nao17b8?email_content_id=' . $ec_id . '&automation_id=' . $auto_id; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ipost_api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $rcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $r = json_decode($response); if (!$r || !$r->Mailing_ID) { throw new Exception('Mailing_ID not found'); } $mailing_id=$r->mailing_id; $response_code = 200; throw new Exception($response); } catch (Exception $e) { $msg = $e->getMessage(); $status = 'error'; if ($response_code == 200) { $status = 'success'; } $resp = '{"' . $status . '":'; if ($status != 'error') { $resp .= '"data":' . $msg . '}'; } else { if ($msg[0] != '{') { $resp .= '{"error":"' . $msg . '"}'; } else { $resp .= $msg; } } $resp .= "}"; # {"errors":[{"code":"EAUTH","description":"Invalid client_id / client_secret combination.","error":"Authentication failed"}],"status":"error"} http_response_code($response_code); echo $resp; exit; ?>