Mailin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * Mailin REST client
  4. */
  5. class Mailin_Rest
  6. {
  7. public $api_key;
  8. public $base_url;
  9. public $curl_opts = array();
  10. public function __construct($base_url,$api_key)
  11. {
  12. if(!function_exists('curl_init'))
  13. {
  14. throw new Exception('Mailin requires CURL module');
  15. }
  16. $this->base_url = $base_url;
  17. $this->api_key = $api_key;
  18. }
  19. /**
  20. * Do CURL request with authorization
  21. */
  22. private function do_request($resource,$method,$input)
  23. {
  24. $called_url = $this->base_url."/".$resource;
  25. $ch = curl_init($called_url);
  26. $auth_header = 'api-key:'.$this->api_key;
  27. $content_header = "Content-Type:application/json";
  28. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  29. // Windows only over-ride
  30. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  31. }
  32. curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth_header,$content_header));
  33. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  34. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  35. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  36. curl_setopt($ch, CURLOPT_HEADER, 0);
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
  38. $data = curl_exec($ch);
  39. if(curl_errno($ch))
  40. {
  41. echo 'Curl error: ' . curl_error($ch). '\n';
  42. }
  43. curl_close($ch);
  44. return json_decode($data,true);
  45. }
  46. public function get($resource,$input)
  47. {
  48. return $this->do_request($resource,"GET",$input);
  49. }
  50. public function put($resource,$input)
  51. {
  52. return $this->do_request($resource,"PUT",$input);
  53. }
  54. public function post($resource,$input)
  55. {
  56. return $this->do_request($resource,"POST",$input);
  57. }
  58. public function delete($resource,$input)
  59. {
  60. return $this->do_request($resource,"DELETE",$input);
  61. }
  62. public function get_account()
  63. {
  64. return $this->get("account","");
  65. }
  66. public function get_smtp_details()
  67. {
  68. return $this->get("account/smtpdetail","");
  69. }
  70. public function create_child_account($email,$password,$company_org,$first_name,$last_name,$credits,$associate_ip)
  71. {
  72. return $this->post("account",json_encode(array("child_email"=>$email,"password"=>$password,"company_org"=>$company_org,"first_name"=>$first_name,"last_name"=>$last_name,"credits"=>$credits,"associate_ip"=>$associate_ip)));
  73. }
  74. public function update_child_account($child_authkey,$company_org,$first_name,$last_name,$password,$associate_ip,$disassociate_ip)
  75. {
  76. return $this->put("account",json_encode(array("auth_key"=>$child_authkey,"company_org"=>$company_org,"first_name"=>$first_name,"last_name"=>$last_name,"password"=>$password,"associate_ip"=>$associate_ip,"disassociate_ip"=>$disassociate_ip)));
  77. }
  78. public function delete_child_account($child_authkey)
  79. {
  80. return $this->delete("account/".$child_authkey,"");
  81. }
  82. public function get_reseller_child($child_authkey)
  83. {
  84. return $this->post("account/getchildv2",json_encode(array("auth_key"=>$child_authkey)));
  85. }
  86. public function add_remove_child_credits($child_authkey,$add_credits,$remove_credits)
  87. {
  88. return $this->post("account/addrmvcredit",json_encode(array("auth_key"=>$child_authkey,"add_credit"=>$add_credits,"rmv_credit"=>$remove_credits)));
  89. }
  90. public function send_sms($to,$from,$text,$web_url,$tag,$type)
  91. {
  92. return $this->post("sms",json_encode(array("text"=>$text,"tag"=>$tag,"web_url"=>$web_url,"from"=>$from,"to"=>$to,"type"=>$type)));
  93. }
  94. public function create_sms_campaign($camp_name,$sender,$content,$bat_sent,$listids,$exclude_list,$scheduled_date)
  95. {
  96. return $this->post("sms",json_encode(array("name"=>$camp_name,"sender"=>$sender,"content"=>$content,"bat"=>$bat_sent,"listid"=>$listids,"exclude_list"=>$exclude_list, "scheduled_date"=>$scheduled_date)));
  97. }
  98. public function update_sms_campaign($id,$camp_name,$sender,$content,$bat_sent,$listids,$exclude_list,$scheduled_date)
  99. {
  100. return $this->put("sms/".$id,json_encode(array("name"=>$camp_name,"sender"=>$sender,"content"=>$content,"bat"=>$bat_sent,"listid"=>$listids,"exclude_list"=>$exclude_list, "scheduled_date"=>$scheduled_date)));
  101. }
  102. public function send_bat_sms($campid,$mobilephone)
  103. {
  104. return $this->get("sms/".$campid,json_encode(array("to"=>$mobilephone)));
  105. }
  106. public function get_campaigns_v2($type,$status,$page,$page_limit)
  107. {
  108. return $this->get("campaign/detailsv2",json_encode(array("type"=>$type,"status"=>$status,"page"=>$page,"page_limit"=>$page_limit)));
  109. }
  110. public function get_campaign_v2($id)
  111. {
  112. return $this->get("campaign/".$id."/detailsv2","");
  113. }
  114. public function create_campaign($category,$from_name,$name,$bat_sent,$html_content,$html_url,$listid,$scheduled_date,$subject,$from_email,$reply_to,$to_field,$exclude_list,$attachmentUrl,$inline_image)
  115. {
  116. return $this->post("campaign",json_encode(array("category"=>$category,"from_name"=>$from_name,"name"=>$name,"bat"=>$bat_sent,"html_content"=>$html_content,"html_url"=>$html_url,"listid"=>$listid,"scheduled_date"=>$scheduled_date,"subject"=>$subject,"from_email"=>$from_email,"reply_to"=>$reply_to,"to_field"=>$to_field,"exclude_list"=>$exclude_list,"attachment_url"=>$attachmentUrl,"inline_image"=>$inline_image)));
  117. }
  118. public function delete_campaign($id)
  119. {
  120. return $this->delete("campaign/".$id,"");
  121. }
  122. public function update_campaign($id,$category,$from_name,$name,$bat_sent,$html_content,$html_url,$listid,$scheduled_date,$subject,$from_email,$reply_to,$to_field,$exclude_list,$attachmentUrl,$inline_image)
  123. {
  124. return $this->put("campaign/".$id,json_encode(array("category"=>$category,"from_name"=>$from_name,"name"=>$name,"bat"=>$bat_sent,"html_content"=>$html_content,"html_url"=>$html_url,"listid"=>$listid,"scheduled_date"=>$scheduled_date,"subject"=>$subject,"from_email"=>$from_email,"reply_to"=>$reply_to,"to_field"=>$to_field,"exclude_list"=>$exclude_list,"attachment_url"=>$attachmentUrl,"inline_image"=>$inline_image)));
  125. }
  126. public function campaign_report_email($id,$lang,$email_subject,$email_to,$email_content_type,$email_bcc,$email_cc,$email_body)
  127. {
  128. return $this->post("campaign/".$id."/report",json_encode(array("lang"=>$lang,"email_subject"=>$email_subject,"email_to"=>$email_to,"email_content_type"=>$email_content_type,"email_bcc"=>$email_bcc,"email_cc"=>$email_cc,"email_body"=>$email_body)));
  129. }
  130. public function campaign_recipients_export($id,$notify_url,$type)
  131. {
  132. return $this->post("campaign/".$id."/recipients",json_encode(array("notify_url"=>$notify_url,"type"=>$type)));
  133. }
  134. public function send_bat_email($campid,$email_to)
  135. {
  136. return $this->post("campaign/".$campid."/test",json_encode(array("emails"=>$email_to)));
  137. }
  138. public function create_trigger_campaign($category,$from_name,$name,$bat_sent,$html_content,$html_url,$listid,$scheduled_date,$subject,$from_email,$reply_to,$to_field,$exclude_list,$recurring,$attachmentUrl,$inline_image)
  139. {
  140. return $this->post("campaign",json_encode(array("category"=>$category,"from_name"=>$from_name,"trigger_name"=>$name,"bat"=>$bat_sent,"html_content"=>$html_content,"html_url"=>$html_url,"listid"=>$listid,"scheduled_date"=>$scheduled_date,"subject"=>$subject,"from_email"=>$from_email,"reply_to"=>$reply_to,"to_field"=>$to_field,"exclude_list"=>$exclude_list,"recurring"=>$recurring,"attachment_url"=>$attachmentUrl,"inline_image"=>$inline_image)));
  141. }
  142. public function update_trigger_campaign($id,$category,$from_name,$name,$bat_sent,$html_content,$html_url,$listid,$scheduled_date,$subject,$from_email,$reply_to,$to_field,$exclude_list,$recurring,$attachmentUrl,$inline_image)
  143. {
  144. return $this->put("campaign/".$id,json_encode(array("category"=>$category,"from_name"=>$from_name,"trigger_name"=>$name,"bat"=>$bat_sent,"html_content"=>$html_content,"html_url"=>$html_url,"listid"=>$listid,"scheduled_date"=>$scheduled_date,"subject"=>$subject,"from_email"=>$from_email,"reply_to"=>$reply_to,"to_field"=>$to_field,"exclude_list"=>$exclude_list,"recurring"=>$recurring,"attachment_url"=>$attachmentUrl,"inline_image"=>$inline_image)));
  145. }
  146. public function share_campaign($campaign_ids)
  147. {
  148. return $this->post("campaign/sharelinkv2",json_encode(array("camp_ids"=>$campaign_ids)));
  149. }
  150. public function update_campaign_status($id,$status)
  151. {
  152. return $this->put("campaign/".$id."/updatecampstatus",json_encode(array("status"=>$status)));
  153. }
  154. public function get_processes($page,$page_limit)
  155. {
  156. return $this->get("process",json_encode(array("page"=>$page,"page_limit"=>$page_limit)));
  157. }
  158. public function get_process($id)
  159. {
  160. return $this->get("process/".$id,"");
  161. }
  162. public function get_lists($page,$page_limit)
  163. {
  164. return $this->get("list",json_encode(array("page"=>$page,"page_limit"=>$page_limit)));
  165. }
  166. public function get_list($id)
  167. {
  168. return $this->get("list/".$id,"");
  169. }
  170. public function create_list($list_name,$list_parent)
  171. {
  172. return $this->post("list",json_encode(array("list_name"=>$list_name,"list_parent"=>$list_parent)));
  173. }
  174. public function delete_list($id)
  175. {
  176. return $this->delete("list/".$id,"");
  177. }
  178. public function update_list($id,$list_name,$list_parent)
  179. {
  180. return $this->put("list/".$id,json_encode(array("list_name"=>$list_name,"list_parent"=>$list_parent)));
  181. }
  182. public function display_list_users($listids,$page,$page_limit)
  183. {
  184. return $this->post("list/display",json_encode(array("listids"=>$listids, "page"=>$page, "page_limit"=>$page_limit)));
  185. }
  186. public function add_users_list($id,$users)
  187. {
  188. return $this->post("list/".$id."/users",json_encode(array("users"=>$users)));
  189. }
  190. public function delete_users_list($id,$users)
  191. {
  192. return $this->delete("list/".$id."/delusers",json_encode(array("users"=>$users)));
  193. }
  194. public function send_email($to,$subject,$from,$html,$text,$cc,$bcc,$replyto,$attachment,$headers)
  195. {
  196. return $this->post("email",json_encode(array("cc"=>$cc,"text"=>$text,"bcc"=>$bcc,"replyto"=>$replyto,"html"=>$html,"to"=>$to,"attachment"=>$attachment,"from"=>$from,"subject"=>$subject,"headers"=>$headers)));
  197. }
  198. public function get_webhooks($is_plat)
  199. {
  200. return $this->get("webhook",json_encode(array("is_plat"=>$is_plat)));
  201. }
  202. public function get_webhook($id)
  203. {
  204. return $this->get("webhook/".$id,"");
  205. }
  206. public function create_webhook($url,$description,$events,$is_plat)
  207. {
  208. return $this->post("webhook",json_encode(array("url"=>$url,"description"=>$description,"events"=>$events,"is_plat"=>$is_plat)));
  209. }
  210. public function delete_webhook($id)
  211. {
  212. return $this->delete("webhook/".$id,"");
  213. }
  214. public function update_webhook($id,$url,$description,$events)
  215. {
  216. return $this->put("webhook/".$id,json_encode(array("url"=>$url,"description"=>$description,"events"=>$events)));
  217. }
  218. public function get_statistics($aggregate,$tag,$days,$end_date,$start_date)
  219. {
  220. return $this->post("statistics",json_encode(array("aggregate"=>$aggregate,"tag"=>$tag,"days"=>$days,"end_date"=>$end_date,"start_date"=>$start_date)));
  221. }
  222. public function get_user($email)
  223. {
  224. return $this->get("user/".$email,"");
  225. }
  226. public function create_user($attributes,$blacklisted,$email,$listid)
  227. {
  228. return $this->post("user",json_encode(array("attributes"=>$attributes,"blacklisted"=>$blacklisted,"email"=>$email,"listid"=>$listid)));
  229. }
  230. public function delete_user($email)
  231. {
  232. return $this->delete("user/".$email,"");
  233. }
  234. public function update_user($email,$attributes,$blacklisted,$listid,$listid_unlink)
  235. {
  236. return $this->put("user/".$email,json_encode(array("attributes"=>$attributes,"blacklisted"=>$blacklisted,"listid"=>$listid,"listid_unlink"=>$listid_unlink)));
  237. }
  238. public function import_users($url,$listids,$notify_url,$name,$folder_id)
  239. {
  240. return $this->post("user/import",json_encode(array("url"=>$url,"listids"=>$listids,"notify_url"=>$notify_url,"name"=>$name,"list_parent"=>$folder_id)));
  241. }
  242. public function export_users($export_attrib,$filter,$notify_url)
  243. {
  244. return $this->post("user/export",json_encode(array("export_attrib"=>$export_attrib,"filter"=>$filter,"notify_url"=>$notify_url)));
  245. }
  246. public function create_update_user($email,$attributes,$blacklisted,$listid,$listid_unlink,$blacklisted_sms)
  247. {
  248. return $this->post("user/createdituser",json_encode(array("email"=>$email,"attributes"=>$attributes,"blacklisted"=>$blacklisted,"listid"=>$listid,"listid_unlink"=>$listid_unlink,"blacklisted_sms"=>$blacklisted_sms)));
  249. }
  250. public function get_attributes()
  251. {
  252. return $this->get("attribute","");
  253. }
  254. public function get_attribute($type)
  255. {
  256. return $this->get("attribute/".$type,"");
  257. }
  258. public function create_attribute($type,$data)
  259. {
  260. return $this->post("attribute",json_encode(array("type"=>$type,"data"=>$data)));
  261. }
  262. public function delete_attribute($type,$data)
  263. {
  264. return $this->post("attribute/".$type,json_encode(array("data"=>$data)));
  265. }
  266. public function get_report($limit,$start_date,$end_date,$offset,$date,$days,$email)
  267. {
  268. return $this->post("report",json_encode(array("limit"=>$limit,"start_date"=>$start_date,"end_date"=>$end_date,"offset"=>$offset,"date"=>$date,"days"=>$days,"email"=>$email)));
  269. }
  270. public function get_folders($page,$page_limit)
  271. {
  272. return $this->get("folder",json_encode(array("page"=>$page,"page_limit"=>$page_limit)));
  273. }
  274. public function get_folder($id)
  275. {
  276. return $this->get("folder/".$id,"");
  277. }
  278. public function create_folder($name)
  279. {
  280. return $this->post("folder",json_encode(array("name"=>$name)));
  281. }
  282. public function delete_folder($id)
  283. {
  284. return $this->delete("folder/".$id,"");
  285. }
  286. public function update_folder($id,$name)
  287. {
  288. return $this->put("folder/".$id,json_encode(array("name"=>$name)));
  289. }
  290. public function delete_bounces($start_date,$end_date,$email)
  291. {
  292. return $this->post("bounces",json_encode(array("start_date"=>$start_date,"end_date"=>$end_date,"email"=>$email)));
  293. }
  294. public function send_transactional_template($id,$to,$cc,$bcc,$attr,$attachmentUrl,$attachment)
  295. {
  296. return $this->put("template/".$id,json_encode(array("cc"=>$cc,"to"=>$to,"attr"=>$attr,"bcc"=>$bcc,"attachment_url"=>$attachmentUrl,"attachment"=>$attachment)));
  297. }
  298. public function create_template($from_name,$name,$bat_sent,$html_content,$html_url,$subject,$from_email,$reply_to,$to_field,$status,$attach)
  299. {
  300. return $this->post("template",json_encode(array("from_name"=>$from_name,"template_name"=>$name,"bat"=>$bat_sent,"html_content"=>$html_content,"html_url"=>$html_url,"subject"=>$subject,"from_email"=>$from_email,"reply_to"=>$reply_to,"to_field"=>$to_field,"status"=>$status,"attachment"=>$attach)));
  301. }
  302. public function update_template($id,$from_name,$name,$bat_sent,$html_content,$html_url,$subject,$from_email,$reply_to,$to_field,$status,$attach)
  303. {
  304. return $this->put("template/".$id,json_encode(array("from_name"=>$from_name,"template_name"=>$name,"bat"=>$bat_sent,"html_content"=>$html_content,"html_url"=>$html_url,"subject"=>$subject,"from_email"=>$from_email,"reply_to"=>$reply_to,"to_field"=>$to_field,"status"=>$status,"attachment"=>$attach)));
  305. }
  306. public function get_senders($option)
  307. {
  308. return $this->get("advanced",json_encode(array("option"=>$option)));
  309. }
  310. public function create_sender($sender_name,$sender_email,$ip_domain)
  311. {
  312. return $this->post("advanced",json_encode(array("name"=>$sender_name,"email"=>$sender_email,"ip_domain"=>$ip_domain)));
  313. }
  314. public function update_sender($id,$sender_name,$sender_email,$ip_domain)
  315. {
  316. return $this->put("advanced/".$id,json_encode(array("name"=>$sender_name,"email"=>$sender_email,"ip_domain"=>$ip_domain)));
  317. }
  318. public function delete_sender($id)
  319. {
  320. return $this->delete("advanced/".$id,"");
  321. }
  322. }
  323. ?>