Skip to content

Instantly share code, notes, and snippets.

@waqashassan98
Created January 28, 2018 16:23
Show Gist options
  • Save waqashassan98/a444a03567deb0bc85fe16e200873748 to your computer and use it in GitHub Desktop.
Save waqashassan98/a444a03567deb0bc85fe16e200873748 to your computer and use it in GitHub Desktop.
Edit Ontraport Emails using Ontraport API
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/mailmerge/vendor/autoload.php';
use OntraportAPI\Ontraport;
//live
$key = "tbpy9FUGu9XXXX";
$appid = "2_9614_MnlYYYY";
$client = new Ontraport($appid, $key);
function log_data($data)
{
file_put_contents('log.txt',$data);
}
function log_entry($id, $divs_count, $inserted_bofore)
{
$content = file_get_contents('log-entry.txt');
$add = "Inserted at the End";
if ($inserted_bofore) {
$add = "Inserted Before the Last Div";
}
$newdata = "Message ID: ".$id." Divs Found: ".$divs_count." ".$add."\n";
file_put_contents('log-entry.txt',$newdata.$content);
}
function read_last_entry()
{
return (int) file_get_contents('log.txt');
}
///////////////Fetch
$conditions = new OntraportAPI\Criteria("type", "=", "e-mail");
$conditions->andCondition("id", ">", read_last_entry());
$requestParams = array(
"condition" => $conditions->fromArray(),
"sort" => "id",
"sortDir" => "asc",
);
// echo "<pre>";
$response = $client->message()->retrieveMultiple($requestParams);
$result = json_decode($response);
foreach ($result->data as $data) {
update_message_with_id($data->message_body, $data->plaintext, $data->id);//$data->id
log_data($data->id);
echo $data->id;
//break;
}
////////////Update Message
function update_message_with_id($message_body, $plaintext, $email_id)
{
global $client;
$pos = strrpos($message_body, "</div>");
$num_of_divs = substr_count($message_body,"</div>");
if ($pos === false) {
$message_body .= "<br/>
[Email_Support_Signature_1578]<br/>
[Email_Footer_A_1582]<br/>
[Email_Footer_B_1579]<br/>
[Email_Footer_C_1580]<br/>
[Email_Footer_D_1581]<br/>
[Emails_Footer_5_1583]";
log_entry($email_id, $num_of_divs, 0);
}
else if ((strlen($message_body) -$pos) <10){
$str_to_insert = "<br/>
[Email_Support_Signature_1578]<br/>
[Email_Footer_A_1582]<br/>
[Email_Footer_B_1579]<br/>
[Email_Footer_C_1580]<br/>
[Email_Footer_D_1581]<br/>
[Emails_Footer_5_1583]<br/>";
$message_body = substr_replace($message_body, $str_to_insert, $pos, 0);
log_entry($email_id, $num_of_divs, 1);
}
else{
$message_body .= "<br/>
[Email_Support_Signature_1578]<br/>
[Email_Footer_A_1582]<br/>
[Email_Footer_B_1579]<br/>
[Email_Footer_C_1580]<br/>
[Email_Footer_D_1581]<br/>
[Emails_Footer_5_1583]<br/>";
log_entry($email_id, $num_of_divs, 0);
}
if(strlen($plaintext) >5){
$plaintext .= "
[Email_Support_Signature_1578]
[Email_Footer_A_1582]
[Email_Footer_B_1579]
[Email_Footer_C_1580]
[Email_Footer_D_1581]
[Emails_Footer_5_1583]";
}
// $message = "Hello. How are you. This is a test email";//"[kootac_nickname]";
$requestParams = array(
"id" => $email_id,
"type" => "e-mail",
"plaintext" => $plaintext,
"message_body" => $message_body,
);
$response = $client->message()->update($requestParams);
$needle = "code";
if( strpos( $response, $needle ) !== false ) {
return true;
}
else{
var_dump($response);
die();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment