PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/_app/core/api/email.php

https://bitbucket.org/sirestudios/fortis-wellness
PHP | 408 lines | 148 code | 56 blank | 204 comment | 20 complexity | 5a0e0f916fd1fccf5404aeac6b5d9e4a MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * Email
  4. * API for sending email through the server or through third-party services
  5. *
  6. * @author Jack McDade
  7. * @author Fred LeBlanc
  8. * @author Mubashar Iqbal
  9. * @package API
  10. * @copyright 2013 Statamic
  11. */
  12. class Email
  13. {
  14. /**
  15. * Fields required to be passed into send()
  16. *
  17. * @var array
  18. */
  19. public static $required = array('to', 'from', 'subject');
  20. /**
  21. * Fields allowed to be passed into send()
  22. *
  23. * @var array
  24. */
  25. public static $allowed = array('to', 'from', 'subject', 'cc', 'bcc', 'headers', 'text', 'html', 'mail_handler', 'mail_handler_key');
  26. /**
  27. * Available Transactional email services
  28. *
  29. * @var array
  30. */
  31. public static $email_handlers = array('postmark', 'mandrill', 'sendgrid', 'mailgun');
  32. /**
  33. * Send an email using a Transactional email service
  34. * or native PHP as a fallback.
  35. *
  36. * @param array $attributes A list of attributes for sending
  37. * @return bool on success
  38. */
  39. public static function send($attributes = array())
  40. {
  41. /*
  42. |--------------------------------------------------------------------------
  43. | Required attributes
  44. |--------------------------------------------------------------------------
  45. |
  46. | We first need to ensure we have the minimum fields necessary to send
  47. | an email.
  48. |
  49. */
  50. $required = array_intersect_key($attributes, array_flip(self::$required));
  51. if (count($required) >= 3) {
  52. /*
  53. |--------------------------------------------------------------------------
  54. | Load handler from config
  55. |--------------------------------------------------------------------------
  56. |
  57. | We check the passed data for a mailer + key first, and then fall back
  58. | to the global Statamic config.
  59. |
  60. */
  61. $email_handler = array_get($attributes, 'email_handler', Config::get('email_handler', null));
  62. $email_handler_key = array_get($attributes, 'email_handler_key', Config::get('email_handler_key', null));
  63. if (in_array($email_handler, self::$email_handlers) && $email_handler_key) {
  64. /*
  65. |--------------------------------------------------------------------------
  66. | Initialize Stampie
  67. |--------------------------------------------------------------------------
  68. |
  69. | Stampie provides numerous adapters for popular email handlers, such as
  70. | Mandrill, Postmark, and SendGrid. Each is written as an abstract
  71. | interface in an Adapter Pattern.
  72. |
  73. */
  74. $mailer = self::initializeEmailHandler($email_handler, $email_handler_key);
  75. /*
  76. |--------------------------------------------------------------------------
  77. | Initialize Message class
  78. |--------------------------------------------------------------------------
  79. |
  80. | The message class is an implementation of the Stampie MessageInterface
  81. |
  82. */
  83. $email = new Message($attributes['to']);
  84. /*
  85. |--------------------------------------------------------------------------
  86. | Set email attributes
  87. |--------------------------------------------------------------------------
  88. |
  89. | I hardly think this requires much explanation.
  90. |
  91. */
  92. $email->setFrom($attributes['from']);
  93. $email->setSubject($attributes['subject']);
  94. if (isset($attributes['text'])) {
  95. $email->setText($attributes['text']);
  96. }
  97. if (isset($attributes['html'])) {
  98. $email->setHtml($attributes['html']);
  99. }
  100. if (isset($attributes['cc'])) {
  101. $email->setCc($attributes['cc']);
  102. }
  103. if (isset($attributes['bcc'])) {
  104. $email->setBcc($attributes['bcc']);
  105. }
  106. if (isset($attributes['headers'])) {
  107. $email->setHeaders($attributes['headers']);
  108. }
  109. $mailer->send($email);
  110. return true;
  111. } else {
  112. /*
  113. |--------------------------------------------------------------------------
  114. | Native PHP Mail
  115. |--------------------------------------------------------------------------
  116. |
  117. | We're utilizing the popular PHPMailer class to handle the messy
  118. | email headers and do-dads. Emailing from PHP in general isn't the best
  119. | idea known to man, so this is really a lackluster fallback.
  120. |
  121. */
  122. $email = new PHPMailer(true);
  123. $email->IsMAIL();
  124. $email->CharSet = 'UTF-8';
  125. $email->AddAddress($attributes['to']);
  126. $email->From = $attributes['from'];
  127. $email->FromName = $attributes['from'];
  128. $email->Subject = $attributes['subject'];
  129. if (isset($attributes['text'])) {
  130. $email->AltBody = $attributes['text'];
  131. }
  132. if (isset($attributes['html'])) {
  133. $email->Body = $attributes['html'];
  134. $email->IsHTML(true);
  135. }
  136. if (isset($attributes['cc'])) {
  137. $email->AddCC($attributes['cc']);
  138. }
  139. if (isset($attributes['bcc'])) {
  140. $email->AddBCC($attributes['bcc']);
  141. }
  142. if ($email->Send()) {
  143. return true;
  144. }
  145. }
  146. }
  147. return false;
  148. }
  149. /**
  150. * Instantiates a Stampie Mailer instance
  151. *
  152. * @param string $email_handler Name of the email handler to use
  153. * @param string $email_handler_key Email Handler Token
  154. * @return object
  155. */
  156. private static function initializeEmailHandler($email_handler, $email_handler_key)
  157. {
  158. $adapter = new Stampie\Adapter\Buzz(new Buzz\Browser());
  159. if ($email_handler == 'postmark') {
  160. return new Stampie\Mailer\Postmark($adapter, $email_handler_key);
  161. } elseif ($email_handler == 'mandrill') {
  162. return new Stampie\Mailer\Mandrill($adapter, $email_handler_key);
  163. } elseif ($email_handler == 'sendgrid') {
  164. return new Stampie\Mailer\SendGrid($adapter, $email_handler_key);
  165. } elseif ($email_handler == 'mailgun') {
  166. return new Stampie\Mailer\MailGun($adapter, $email_handler_key);
  167. }
  168. Log::error("Could not initialize email handler `" . $email_handler . "`. Unknown service.", "core", "email");
  169. }
  170. }
  171. /**
  172. * Message
  173. * Generic object for email sending
  174. *
  175. * @author Jack McDade
  176. * @author Fred LeBlanc
  177. * @author Mubashar Iqbal
  178. * @package API
  179. * @copyright 2013 Statamic
  180. */
  181. class Message extends \Stampie\Message
  182. {
  183. /**
  184. * Message subject
  185. * @var string
  186. */
  187. public $subject = "";
  188. /**
  189. * Message from address
  190. * @var string
  191. */
  192. public $from = "";
  193. /**
  194. * Message headers
  195. * @var array
  196. */
  197. public $headers = array();
  198. /**
  199. * Message CC address(es)
  200. * @var string|null
  201. */
  202. public $cc = null;
  203. /**
  204. * Message BCC address(es)
  205. * @var string|null
  206. */
  207. public $bcc = null;
  208. /**
  209. * Sets the HTML of the message
  210. *
  211. * @param string $html
  212. */
  213. public function setHtml($html)
  214. {
  215. $this->html = $html;
  216. }
  217. /**
  218. * Sets the plain text of the message
  219. *
  220. * @param string $text
  221. * @throws \InvalidArgumentException
  222. */
  223. public function setText($text)
  224. {
  225. if ($text !== strip_tags($text)) {
  226. throw new \InvalidArgumentException('HTML Detected');
  227. }
  228. $this->text = $text;
  229. }
  230. /**
  231. * Gets the HTML of the message
  232. *
  233. * @return string
  234. */
  235. public function getHtml()
  236. {
  237. return $this->html;
  238. }
  239. /**
  240. * Gets the plain text of the message
  241. *
  242. * @return string
  243. */
  244. public function getText()
  245. {
  246. return $this->text;
  247. }
  248. /**
  249. * Sets the from of the message
  250. *
  251. * @param string $from
  252. * @return void
  253. */
  254. public function setFrom($from)
  255. {
  256. $this->from = $from;
  257. }
  258. /**
  259. * Gets the from of the message
  260. *
  261. * @return string
  262. */
  263. public function getFrom()
  264. {
  265. return $this->from;
  266. }
  267. /**
  268. * Sets the subject of the message
  269. *
  270. * @param string $subject Subject to use
  271. * @return string
  272. */
  273. public function setSubject($subject = null)
  274. {
  275. $this->subject = $subject;
  276. }
  277. /**
  278. * Gets the subject of the message
  279. *
  280. * @return string
  281. */
  282. public function getSubject()
  283. {
  284. return $this->subject;
  285. }
  286. /**
  287. * Sets a list of headers for the message
  288. *
  289. * @param array $headers Headers to set
  290. * @return void
  291. */
  292. public function setHeaders($headers = array())
  293. {
  294. $this->headers = $headers;
  295. }
  296. /**
  297. * Gets a list of headers for the message
  298. *
  299. * @return array
  300. */
  301. public function getHeaders()
  302. {
  303. return $this->headers;
  304. }
  305. /**
  306. * Gets the reply-to for the message
  307. *
  308. * @return string
  309. */
  310. public function getReplyTo()
  311. {
  312. return $this->getFrom();
  313. }
  314. /**
  315. * Sets the CC emails for the message
  316. *
  317. * @param string $cc Email address(es) to CC
  318. * @return void
  319. */
  320. public function setCc($cc = null)
  321. {
  322. $this->cc = $cc;
  323. }
  324. /**
  325. * Gets the CC emails for the message
  326. *
  327. * @return string
  328. */
  329. public function getCc()
  330. {
  331. return $this->cc;
  332. }
  333. /**
  334. * Sets the BCC emails for the message
  335. *
  336. * @param string $bcc Email address(es) to BCC
  337. * @return void
  338. */
  339. public function setBcc($bcc = null)
  340. {
  341. $this->bcc = $bcc;
  342. }
  343. /**
  344. * Gets the BCC emails for the message
  345. *
  346. * @return string
  347. */
  348. public function getBcc()
  349. {
  350. return $this->bcc;
  351. }
  352. }