PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Postmark.php

https://github.com/hgmnz/Bogota-Conf-Website
PHP | 452 lines | 254 code | 76 blank | 122 comment | 48 complexity | 071034388b6d3313f29d85271f34e687 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Postmark Email Library
  4. *
  5. * Permits email to be sent using Postmarkapp.com's Servers
  6. *
  7. * @category Libraries
  8. * @author Based on work by János Rusiczki & Markus Hedlund’s.
  9. * @modified Heavily Modified by Zack Kitzmiller
  10. * @link http://www.github.com/zackkitzmiller/postmark-codeigniter
  11. */
  12. class Postmark {
  13. //private
  14. var $CI;
  15. var $api_key = '';
  16. var $validation = FALSE;
  17. var $strip_html = FALSE;
  18. var $develop = FALSE;
  19. var $from_name;
  20. var $from_address;
  21. var $_reply_to_name;
  22. var $_reply_to_address;
  23. var $_to_name;
  24. var $_to_address;
  25. var $_cc_name;
  26. var $_cc_address;
  27. var $_subject;
  28. var $_message_plain;
  29. var $_message_html;
  30. var $_tag;
  31. /**
  32. * Constructor
  33. *
  34. * @access public
  35. * @param array initialization parameters
  36. */
  37. function Postmark($params = array())
  38. {
  39. $this->CI =& get_instance();
  40. if (count($params) > 0)
  41. {
  42. $this->initialize($params);
  43. }
  44. if ($this->develop == TRUE)
  45. {
  46. $this->api_key = 'POSTMARK_API_TEST';
  47. }
  48. log_message('debug', 'Postmark Class Initialized');
  49. }
  50. // --------------------------------------------------------------------
  51. /**
  52. * Initialize Preferences
  53. *
  54. * @access public
  55. * @param array initialization parameters
  56. * @return void
  57. */
  58. function initialize($params)
  59. {
  60. $this->clear();
  61. if (count($params) > 0)
  62. {
  63. foreach ($params as $key => $value)
  64. {
  65. if (isset($this->$key))
  66. {
  67. $this->$key = $value;
  68. }
  69. }
  70. }
  71. }
  72. // --------------------------------------------------------------------
  73. /**
  74. * Clear the Email Data
  75. *
  76. * @access public
  77. * @return void
  78. */
  79. function clear() {
  80. $this->from_name = '';
  81. $this->from_address = '';
  82. $this->_to_name = '';
  83. $this->_to_address = '';
  84. $this->_cc_name = '';
  85. $this->_cc_address = '';
  86. $this->_subject = '';
  87. $this->_message_plain = '';
  88. $this->_message_html = '';
  89. $this->_tag = '';
  90. }
  91. // --------------------------------------------------------------------
  92. /**
  93. * Set Email FROM address
  94. *
  95. * This could also be set in the config file
  96. *
  97. * TODO:
  98. * Validate Email Addresses ala CodeIgniter's Email Class
  99. *
  100. * @access public
  101. * @return void
  102. */
  103. function from($address, $name = null)
  104. {
  105. if ( ! $this->validation == TRUE)
  106. {
  107. $this->from_address = $address;
  108. $this->from_name = $name;
  109. }
  110. else
  111. {
  112. if ($this->_validate_email($address))
  113. {
  114. $this->from_address = $address;
  115. $this->from_name = $name;
  116. }
  117. else
  118. {
  119. show_error('You have entered an invalid sender address.');
  120. }
  121. }
  122. }
  123. // --------------------------------------------------------------------
  124. /**
  125. * Set Email TO address
  126. *
  127. * TODO:
  128. * Validate Email Addresses ala CodeIgniter's Email Class
  129. *
  130. * @access public
  131. * @return void
  132. */
  133. function to($address, $name = null)
  134. {
  135. if ( ! $this->validation == TRUE)
  136. {
  137. $this->_to_address = $address;
  138. $this->_to_name = $name;
  139. }
  140. else
  141. {
  142. if ($this->_validate_email($address))
  143. {
  144. $this->_to_address = $address;
  145. $this->_to_name = $name;
  146. }
  147. else
  148. {
  149. show_error('You have entered an invalid recipient address.');
  150. }
  151. }
  152. }
  153. // --------------------------------------------------------------------
  154. /**
  155. * Set Email ReplyTo address
  156. *
  157. * TODO:
  158. * Validate Email Addresses ala CodeIgniter's Email Class
  159. *
  160. * @access public
  161. * @return void
  162. */
  163. function reply_to($address, $name = null)
  164. {
  165. if ( ! $this->validation == TRUE)
  166. {
  167. $this->_reply_to_address = $address;
  168. $this->_reply_to_name = $name;
  169. }
  170. else
  171. {
  172. if ($this->_validate_email($address))
  173. {
  174. $this->_reply_to_address = $address;
  175. $this->_reply_to_name = $name;
  176. }
  177. else
  178. {
  179. show_error('You have entered an invalid reply to address.');
  180. }
  181. }
  182. }
  183. // --------------------------------------------------------------------
  184. /**
  185. * Set Email CC address
  186. *
  187. * TODO:
  188. * Validate Email Addresses ala CodeIgniter's Email Class
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. function cc($address, $name = null)
  194. {
  195. if ( ! $this->validation == TRUE)
  196. {
  197. $this->_cc_address = $address;
  198. $this->_cc_name = $name;
  199. }
  200. else
  201. {
  202. if ($this->_validate_email($address))
  203. {
  204. $this->_cc_address = $address;
  205. $this->_cc_name = $name;
  206. }
  207. else
  208. {
  209. show_error('You have entered an invalid recipient address.');
  210. }
  211. }
  212. }
  213. // --------------------------------------------------------------------
  214. /**
  215. * Set Email Subject
  216. *
  217. * @access public
  218. * @return void
  219. */
  220. function subject($subject)
  221. {
  222. $this->_subject = $subject;
  223. }
  224. // --------------------------------------------------------------------
  225. /**
  226. * Set Tag
  227. *
  228. * @access public
  229. * @return void
  230. */
  231. function tag($tag)
  232. {
  233. $this->_tag = $tag;
  234. }
  235. // --------------------------------------------------------------------
  236. /**
  237. * Set Email Message in Plain Text
  238. *
  239. * @access public
  240. * @return void
  241. */
  242. function message_plain($message)
  243. {
  244. if ( ! $this->strip_html )
  245. {
  246. $this->_message_plain = $message;
  247. }
  248. else
  249. {
  250. $this->_message_plain = $this->_strip_html($message);
  251. }
  252. }
  253. // --------------------------------------------------------------------
  254. /**
  255. * Set Email Message in HTML
  256. *
  257. * @access public
  258. * @return void
  259. */
  260. function message_html($message)
  261. {
  262. $this->_message_html = $message;
  263. }
  264. // --------------------------------------------------------------------
  265. /**
  266. * Private Function to prepare and send email
  267. */
  268. function _prepare_data()
  269. {
  270. $data = array();
  271. $data['Subject'] = $this->_subject;
  272. $data['From'] = is_null($this->from_name) ? $this->from_address : "{$this->from_name} <{$this->from_address}>";
  273. $data['To'] = is_null($this->_to_name) ? $this->_to_address : "{$this->_to_name} <{$this->_to_address}>";
  274. if (!is_null($this->_cc_address) && ($this->_cc_address != '')) {
  275. $data['Cc'] = is_null($this->_cc_name) ? $this->_cc_address : "{$this->_cc_name} <{$this->_cc_address}>";
  276. }
  277. if (!is_null($this->_reply_to_address) && ($this->_reply_to_address != '')) {
  278. $data['ReplyTo'] = is_null($this->_reply_to_name) ? $this->_reply_to_address : "{$this->_reply_to_name} <{$this->_reply_to_address}>";
  279. }
  280. if (!is_null($this->_tag) && ($this->_tag != '')) {
  281. $data['tag'] = $this->_tag;
  282. }
  283. if (!is_null($this->_message_html)) {
  284. $data['HtmlBody'] = $this->_message_html;
  285. }
  286. if (!is_null($this->_message_plain)) {
  287. $data['TextBody'] = $this->_message_plain;
  288. }
  289. return $data;
  290. }
  291. function send($from_address = null, $from_name = null, $to_address = null, $to_name = null, $subject = null, $message_plain = null, $message_html = null)
  292. {
  293. if (!function_exists('curl_init'))
  294. {
  295. if(function_exists('log_message'))
  296. {
  297. log_message('error', 'Postmark - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
  298. }
  299. return false;
  300. }
  301. if (!is_null($from_address)) $this->from($from_address, $from_name);
  302. if (!is_null($to_address)) $this->to($to_address, $to_name);
  303. if (!is_null($subject)) $this->subject($subject);
  304. if (!is_null($message_plain)) $this->message_plain($message_plain);
  305. if (!is_null($message_html)) $this->message_html($message_html);
  306. if (is_null($this->api_key)) {
  307. show_error("Postmark API key is not set!");
  308. }
  309. if (is_null($this->from_address)) {
  310. show_error("From address is not set!");
  311. }
  312. if (is_null($this->_to_address)) {
  313. show_error("To address is not set!");
  314. }
  315. if (is_null($this->_subject)) {
  316. show_error("Subject is not set!");
  317. }
  318. if (is_null($this->_message_plain) && is_null($this->_message_html)) {
  319. show_error("Please either set plain message, HTML message or both!");
  320. }
  321. $encoded_data = json_encode($this->_prepare_data());
  322. $headers = array(
  323. 'Accept: application/json',
  324. 'Content-Type: application/json',
  325. 'X-Postmark-Server-Token: ' . $this->api_key
  326. );
  327. $ch = curl_init();
  328. curl_setopt($ch, CURLOPT_URL, 'http://api.postmarkapp.com/email');
  329. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  330. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  331. curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded_data);
  332. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  333. $return = curl_exec($ch);
  334. log_message('debug', 'POSTMARK JSON: ' . $encoded_data . "\nHeaders: \n\t" . implode("\n\t", $headers) . "\nReturn:\n$return");
  335. if (curl_error($ch) != '') {
  336. show_error(curl_error($ch));
  337. }
  338. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  339. log_message('debug', 'POSTMARK http code:' . $httpCode);
  340. if (intval($httpCode / 100) != 2) {
  341. $message = json_decode($return)->Message;
  342. show_error('Error while mailing. Postmark returned HTTP code ' . $httpCode . ' with message "'.$message.'"');
  343. }
  344. }
  345. // --------------------------------------------------------------------
  346. /**
  347. * Email Validation
  348. *
  349. * @access public
  350. * @param string
  351. * @return bool
  352. */
  353. function _validate_email($address)
  354. {
  355. $addresses = explode(',', $address);
  356. foreach($addresses as $k => $v) {
  357. if ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", trim($v))) {
  358. return FALSE;
  359. }
  360. }
  361. return TRUE;
  362. }
  363. // --------------------------------------------------------------------
  364. /**
  365. * Strip Html
  366. *
  367. * @access public
  368. * @param string
  369. * @return string
  370. */
  371. function _strip_html($message)
  372. {
  373. $message = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $message);
  374. return strip_tags($message);
  375. }
  376. }