Skip to content

Instantly share code, notes, and snippets.

@cmpscabral
Last active February 16, 2017 01:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cmpscabral/b0945fecfdea14e88869769dc15b7484 to your computer and use it in GitHub Desktop.
Save cmpscabral/b0945fecfdea14e88869769dc15b7484 to your computer and use it in GitHub Desktop.
<?php
/*
// recipients array example
$recipients = array(
array('address'=>
array(
'email' => 'email@tld.com',
'name'=>'Carlos Cabral'
)
)
)
// images array example
$images = array
array(
'type'=>'image/jpeg',
'name'=>$cid,
'data'=>base64_encode(file_get_contents($filename))
)
);
*/
function sendMessage($key,$campaign_id,$from,$replyTo,$recipients,$subject,$text,$html,$images=array(),$attachments=array()) {
$curl = curl_init();
$array_post_fields = array(
"campaign_id" => $campaign_id,
"transactional" => true,
"content" => array(
"reply_to" => $replyTo,
"from" => $from,
"subject" => $subject,
"text" => $text,
"html" => $html
)
);
if (is_array($recipients)) {
$array_post_fields['recipients'] = $recipients;
} else {
$array_post_fields['list_id'] = $recipients;
}
if (sizeof($images)>0) {
$array_post_fields["content"]["inline_images"] = $images;
}
if (sizeof($attachments)>0) {
$array_post_fields["content"]["attachments"] = $attachments;
}
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sparkpost.com/api/v1/transmissions?num_rcpt_errors=3",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($array_post_fields),
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: ".$key
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$status = 'error';
$result = $err;
} else {
$status = 'ok';
$result = $response;
}
return array('status'=>$status,'result'=>$result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment