Skip to content

Instantly share code, notes, and snippets.

@emmgfx
Created June 25, 2018 15:16
Show Gist options
  • Save emmgfx/705fc7ebd92ce558def738475bb458c9 to your computer and use it in GitHub Desktop.
Save emmgfx/705fc7ebd92ce558def738475bb458c9 to your computer and use it in GitHub Desktop.
Test mailrelay
<?php
class MR {
private $url = null;
private $apikey = null;
public $id_package = null;
function __construct($url, $apikey){
$this->url = $url;
$this->apikey = $apikey;
}
public function prepare(){
$this->id_package = $this->makeRequest('getPackages')['data'][0]['id'];
}
public function makeRequest($function, $data = array()){
$curl = curl_init($this->url);
$postData = array_merge(array(
'function' => $function,
'apiKey' => $this->apikey,
), $data);
$post = http_build_query($postData);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($curl);
if ($json === false) {
die('Request failed with error: '. curl_error($curl));
}
$result = json_decode($json, true);
if($result['status'] != 1){
var_dump($result);
throw new Exception("Error Processing Request", 1);
}
return $result;
}
public function addCampaign($data = null){
$postData = array_merge(array(
'mailboxFromId' => 1,
'mailboxReplyId' => 1,
'mailboxReportId' => 1,
'emailReport' => true,
'groups' => array(1),
'text' => null,
'packageId' => $this->id_package,
), $data);
return $this->makeRequest('addCampaign', $postData);
}
public function getOrCreateCampaignFolderId($name){
$folders = $this->makeRequest('getCampaignFolders')['data'];
$wanted_folder = array_filter($folders, function($folder) use ($name){
return $folder['name'] == $name;
});
if(empty($wanted_folder)){
return $this->makeRequest('addCampaignFolder', array('name' => $name))['data'];
}else{
return $wanted_folder[0]['id'];
}
}
}
<?php
require_once 'class.mr.php';
# Instancia y prepara la clase:
$mr = new MR(
'http://[...].ip-zone.com/ccm/admin/api/version/2/&type=json',
'[...]'
);
$mr->prepare();
# Averigua el id de la carpeta que se quiere usar:
$id_folder = $mr->getOrCreateCampaignFolderId('wordpress');
# Crea una campaña y guarda el id:
# El array de parámetros sobreescribe los que tenga el método por defecto.
$new_campaign_id = $mr->addCampaign(array(
'subject' => '[name], nosequé',
'html' => 'Hola [name], deberías darte de baja, desde aquí: [unsubscribe_url].',
'campaignFolderId' => $id_folder
))['data'];
# Envía la campaña recién creada:
$send = $mr->makeRequest('sendCampaign', array(
'id' => $new_campaign_id
));
if($send['status'] == 1){
echo 'Se está enviando la campaña ' . $new_campaign_id;
}else{
echo '...';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment