PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/community/Ebizmarts/MageMonkey/Model/TransactionalEmail/MANDRILL.php

https://github.com/arush/desparation-deprecated
PHP | 354 lines | 138 code | 66 blank | 150 comment | 5 complexity | e9c9ade76a71d4feb9f78636ab9dbc4b MD5 | raw file
  1. <?php
  2. /**
  3. * MailChimp MANDRILL API wrapper
  4. *
  5. * @category Ebizmarts
  6. * @package Ebizmarts_MageMonkey
  7. * @author Ebizmarts Team <info@ebizmarts.com>
  8. */
  9. class Ebizmarts_MageMonkey_Model_TransactionalEmail_MANDRILL
  10. {
  11. /**
  12. * API version number
  13. *
  14. * @var string
  15. */
  16. public $version = '1.0';
  17. /**
  18. * Error Message storage
  19. *
  20. * @var string
  21. */
  22. public $errorMessage;
  23. /**
  24. * Error Code storage
  25. *
  26. * @var integer
  27. */
  28. public $errorCode;
  29. /**
  30. * Cache the user api_key so we only have to log in once per client instantiation
  31. *
  32. * @var string MailChimp API key
  33. */
  34. public $api_key;
  35. /**
  36. * STS API URL
  37. *
  38. * @var string
  39. */
  40. public $apiUrl;
  41. /**
  42. * Request output format
  43. *
  44. * @var string
  45. */
  46. protected $_output = 'json';
  47. /**
  48. * Setup data
  49. *
  50. * @param string $apikey Your MailChimp apikey
  51. * @param string $secure Whether or not this should use a secure connection
  52. */
  53. function __construct($apikey = null)
  54. {
  55. if($apikey){
  56. $this->setApiKey($apikey);
  57. }
  58. }
  59. /**
  60. * Api key setter
  61. *
  62. * @param string $key API Key
  63. * @return Ebizmarts_MageMonkey_Model_TransactionalEmail_MANDRILL
  64. */
  65. public function setApiKey($key)
  66. {
  67. $this->api_key = $key;
  68. $this->apiUrl = "http://mandrillapp.com/api/{$this->version}/";
  69. return $this;
  70. }
  71. /**
  72. * ===== Users 유 Calls =====
  73. */
  74. /**
  75. * Validate an API key and respond to a ping
  76. *
  77. */
  78. public function usersPing()
  79. {
  80. return $this->_callServer("users/ping");
  81. }
  82. /**
  83. * Return the information about the API-connected user
  84. *
  85. */
  86. public function usersInfo()
  87. {
  88. return $this->_callServer("users/info");
  89. }
  90. /**
  91. * Return the senders that have tried to use this account, both verified and unverified
  92. *
  93. */
  94. public function usersSenders()
  95. {
  96. return $this->_callServer("users/senders");
  97. }
  98. /**
  99. * Disable a sender from being able to send
  100. *
  101. * @param string $email
  102. */
  103. public function usersDisableSender($email)
  104. {
  105. $params = array();
  106. $params["email"] = $email;
  107. return $this->_callServer("users/disable-sender", $params);
  108. }
  109. /**
  110. * Send an email to the given address to verify that it is an accepted sender for your Mandrill account.
  111. *
  112. * @param string $email
  113. */
  114. public function usersVerifySender($email)
  115. {
  116. $params = array();
  117. $params["email"] = $email;
  118. return $this->_callServer("users/verify-sender", $params);
  119. }
  120. public function verifyEmailAddress($email)
  121. {
  122. return $this->usersVerifySender($email);
  123. }
  124. /**
  125. *
  126. * ===== Users 유 Calls =====
  127. */
  128. /**
  129. * ===== Messages ✐ Calls =====
  130. */
  131. /**
  132. * Send a new transactional message through Mandrill
  133. *
  134. * @param array $message The message data with the following keys:
  135. * string html the full HTML content to be sent
  136. * string text optional full text content to be sent
  137. * string subject the message subject
  138. * string from_email the sender email address. If this address has not been verified, the message will be queued and not sent until it is verified
  139. * string from_name optional from name to be used
  140. * array to an array of email addresses to use as recipients. Each item in the array should be a struct with two keys - email: the email address of the recipient, and name: the optional display name to use for the recipient
  141. * struct headers optional extra headers to add to the message (currently only Reply-To and X-* headers are allowed)
  142. * boolean track_opens whether or not to turn on open tracking for the message
  143. * boolean track_clicks whether or not to turn on click tracking for the message
  144. * array tags an array of string to tag the message with. Stats are accumulated using tags, though we only store the first 100 we see, so this should not be unique or change frequently. Tags should be 50 characters or less. Any tags starting with an understore are reserved for internal use and will cause errors.
  145. */
  146. public function messagesSend($message)
  147. {
  148. $to = array();
  149. foreach($message['to_email'] as $pos => $email){
  150. $to []= array(
  151. 'email' => $email,
  152. 'name' => $message['to_name'][$pos]
  153. );
  154. }
  155. $message['to'] = $to;
  156. unset($message['to_email'], $message['to_name']);
  157. $params = array();
  158. $params["message"] = $message;
  159. return $this->_callServer("messages/send", $params);
  160. }
  161. public function sendEmail($message)
  162. {
  163. return $this->messagesSend($message);
  164. }
  165. /**
  166. * ===== Messages ✐ Calls =====
  167. */
  168. /**
  169. * ===== Tags ✰ Calls =====
  170. */
  171. /**
  172. * Return all of the user-defined tag information
  173. *
  174. */
  175. public function tagsList()
  176. {
  177. return $this->_callServer("tags/list");
  178. }
  179. /**
  180. * Return the recent history (hourly stats for the last 30 days) for a tag
  181. *
  182. * @param string $tag
  183. */
  184. public function tagsTimeSeries($tag)
  185. {
  186. $params = array();
  187. $params["tag"] = $tag;
  188. return $this->_callServer("tags/time-series", $params);
  189. }
  190. /**
  191. * Return the recent history (hourly stats for the last 30 days) for all tags
  192. *
  193. */
  194. public function tagsAllTimeSeries()
  195. {
  196. return $this->_callServer("tags/all-time-series");
  197. }
  198. /**
  199. * ===== Tags ✰ Calls =====
  200. */
  201. /**
  202. * ===== Urls ≎ Calls =====
  203. */
  204. /**
  205. * Get the 100 most clicked URLs
  206. *
  207. */
  208. public function urlsList()
  209. {
  210. return $this->_callServer("urls/list");
  211. }
  212. /**
  213. * Return the 100 most clicked URLs that match the search query given
  214. *
  215. * @param string $query
  216. */
  217. public function urlsSearch($query)
  218. {
  219. $params = array();
  220. $params["q"] = $query;
  221. return $this->_callServer("urls/search", $params);
  222. }
  223. /**
  224. * Return the recent history (hourly stats for the last 30 days) for a url
  225. *
  226. * @param string $url
  227. */
  228. public function urlsTimeSeries($url)
  229. {
  230. $params = array();
  231. $params["url"] = $url;
  232. return $this->_callServer("urls/time-series", $params);
  233. }
  234. /**
  235. * ===== Urls ≎ Calls =====
  236. */
  237. /**
  238. * Actually connect to the server and call the requested methods, parsing the result
  239. *
  240. * @param string $method
  241. * @param array OPTIONAL $params
  242. * @return object|false
  243. */
  244. protected function _callServer($method, $params = array())
  245. {
  246. $this->errorMessage = null;
  247. $this->errorCode = null;
  248. $params['key'] = $this->api_key;
  249. $url = $this->apiUrl . $method . '.' . $this->_output;
  250. Mage::helper('monkey')->log($url, 'MageMonkey_ApiCall.log');
  251. Mage::helper('monkey')->log($params, 'MageMonkey_ApiCall.log');
  252. $curlSession = curl_init();
  253. curl_setopt($curlSession, CURLOPT_USERAGENT, Mage::helper('monkey')->getUserAgent());
  254. curl_setopt($curlSession, CURLOPT_URL, $url);
  255. curl_setopt($curlSession, CURLOPT_HEADER, 0);
  256. curl_setopt($curlSession, CURLOPT_POST, TRUE);
  257. curl_setopt($curlSession, CURLOPT_POSTFIELDS, http_build_query($params));
  258. curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);
  259. //curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, FALSE);
  260. //curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 1);
  261. $result = curl_exec($curlSession);
  262. if(!$result){
  263. $errstr = curl_error($curlSession);
  264. $errno = curl_errno($curlSession);
  265. $this->errorMessage = "Could not connect (ERR $errno: $errstr)";
  266. $this->errorCode = "-99";
  267. return false;
  268. }
  269. // Check that a connection was made
  270. if (curl_error($curlSession)) {
  271. $this->errorMessage = curl_error($curlSession);
  272. $this->errorCode = "-99";
  273. return false;
  274. }
  275. $httpCode = curl_getinfo($curlSession, CURLINFO_HTTP_CODE);
  276. curl_close($curlSession);
  277. $resultObject = json_decode($result);
  278. Mage::helper('monkey')->log($resultObject, 'MageMonkey_ApiCall.log');
  279. //You can consider any non-200 HTTP response code an error
  280. //the returned data will contain more detailed information
  281. if($httpCode != 200){
  282. $this->errorMessage = $resultObject->message;
  283. $this->errorCode = "-99";
  284. return false;
  285. }
  286. return $resultObject;
  287. }
  288. }