Email.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. namespace SendGrid;
  3. class Email
  4. {
  5. public
  6. $to,
  7. $toName,
  8. $from,
  9. $fromName,
  10. $replyTo,
  11. $cc,
  12. $ccName,
  13. $bcc,
  14. $bccName,
  15. $subject,
  16. $text,
  17. $html,
  18. $date,
  19. $content,
  20. $headers,
  21. $smtpapi,
  22. $attachments;
  23. public function __construct()
  24. {
  25. $this->fromName = false;
  26. $this->replyTo = false;
  27. $this->smtpapi = new \Smtpapi\Header();
  28. }
  29. /**
  30. * _removeFromList
  31. * Given a list of key/value pairs, removes the associated keys
  32. * where a value matches the given string ($item)
  33. *
  34. * @param Array $list - the list of key/value pairs
  35. * @param String $item - the value to be removed
  36. */
  37. private function _removeFromList(&$list, $item, $key_field = null)
  38. {
  39. foreach ($list as $key => $val) {
  40. if ($key_field) {
  41. if ($val[$key_field] == $item) {
  42. unset($list[$key]);
  43. }
  44. } else {
  45. if ($val == $item) {
  46. unset($list[$key]);
  47. }
  48. }
  49. }
  50. //repack the indices
  51. $list = array_values($list);
  52. }
  53. public function addTo($email, $name = null)
  54. {
  55. if ($this->to == null) {
  56. $this->to = array();
  57. }
  58. if (is_array($email)) {
  59. foreach ($email as $e) {
  60. $this->to[] = $e;
  61. }
  62. } else {
  63. $this->to[] = $email;
  64. }
  65. if (is_array($name)) {
  66. foreach ($name as $n) {
  67. $this->addToName($n);
  68. }
  69. } elseif ($name) {
  70. $this->addToName($name);
  71. }
  72. return $this;
  73. }
  74. public function addSmtpapiTo($email, $name = null)
  75. {
  76. $this->smtpapi->addTo($email, $name);
  77. return $this;
  78. }
  79. public function setTos(array $emails)
  80. {
  81. $this->to = $emails;
  82. return $this;
  83. }
  84. public function setSmtpapiTos(array $emails)
  85. {
  86. $this->smtpapi->setTos($emails);
  87. return $this;
  88. }
  89. public function addToName($name)
  90. {
  91. if ($this->toName == null) {
  92. $this->toName = array();
  93. }
  94. $this->toName[] = $name;
  95. return $this;
  96. }
  97. public function getToNames()
  98. {
  99. return $this->toName;
  100. }
  101. public function setFrom($email)
  102. {
  103. $this->from = $email;
  104. return $this;
  105. }
  106. public function getFrom($as_array = false)
  107. {
  108. if ($as_array && ($name = $this->getFromName())) {
  109. return array("$this->from" => $name);
  110. } else {
  111. return $this->from;
  112. }
  113. }
  114. public function setFromName($name)
  115. {
  116. $this->fromName = $name;
  117. return $this;
  118. }
  119. public function getFromName()
  120. {
  121. return $this->fromName;
  122. }
  123. public function setReplyTo($email)
  124. {
  125. $this->replyTo = $email;
  126. return $this;
  127. }
  128. public function getReplyTo()
  129. {
  130. return $this->replyTo;
  131. }
  132. public function setCc($email)
  133. {
  134. $this->cc = array($email);
  135. return $this;
  136. }
  137. public function setCcs(array $email_list)
  138. {
  139. $this->cc = $email_list;
  140. return $this;
  141. }
  142. public function addCc($email, $name = null)
  143. {
  144. if ($this->cc == null) {
  145. $this->cc = array();
  146. }
  147. if (is_array($email)) {
  148. foreach ($email as $e) {
  149. $this->cc[] = $e;
  150. }
  151. } else {
  152. $this->cc[] = $email;
  153. }
  154. if (is_array($name)) {
  155. foreach ($name as $n) {
  156. $this->addCcName($n);
  157. }
  158. } elseif ($name) {
  159. $this->addCcName($name);
  160. }
  161. return $this;
  162. }
  163. public function addCcName($name)
  164. {
  165. if ($this->ccName == null) {
  166. $this->ccName = array();
  167. }
  168. $this->ccName[] = $name;
  169. return $this;
  170. }
  171. public function removeCc($email)
  172. {
  173. $this->_removeFromList($this->cc, $email);
  174. return $this;
  175. }
  176. public function getCcs()
  177. {
  178. return $this->cc;
  179. }
  180. public function getCcNames()
  181. {
  182. return $this->ccName;
  183. }
  184. public function setBcc($email)
  185. {
  186. $this->bcc = array($email);
  187. return $this;
  188. }
  189. public function setBccs($email_list)
  190. {
  191. $this->bcc = $email_list;
  192. return $this;
  193. }
  194. public function addBcc($email, $name = null)
  195. {
  196. if ($this->bcc == null) {
  197. $this->bcc = array();
  198. }
  199. if (is_array($email)) {
  200. foreach ($email as $e) {
  201. $this->bcc[] = $e;
  202. }
  203. } else {
  204. $this->bcc[] = $email;
  205. }
  206. if (is_array($name)) {
  207. foreach ($name as $n) {
  208. $this->addBccName($n);
  209. }
  210. } elseif ($name) {
  211. $this->addBccName($name);
  212. }
  213. return $this;
  214. }
  215. public function addBccName($name)
  216. {
  217. if ($this->bccName == null) {
  218. $this->bccName = array();
  219. }
  220. $this->bccName[] = $name;
  221. return $this;
  222. }
  223. public function getBccNames()
  224. {
  225. return $this->bccName;
  226. }
  227. public function removeBcc($email)
  228. {
  229. $this->_removeFromList($this->bcc, $email);
  230. return $this;
  231. }
  232. public function getBccs()
  233. {
  234. return $this->bcc;
  235. }
  236. public function setSubject($subject)
  237. {
  238. $this->subject = $subject;
  239. return $this;
  240. }
  241. public function getSubject()
  242. {
  243. return $this->subject;
  244. }
  245. public function setDate($date)
  246. {
  247. $this->date = $date;
  248. return $this;
  249. }
  250. public function getDate()
  251. {
  252. return $this->date;
  253. }
  254. public function setText($text)
  255. {
  256. $this->text = $text;
  257. return $this;
  258. }
  259. public function getText()
  260. {
  261. return $this->text;
  262. }
  263. public function setHtml($html)
  264. {
  265. $this->html = $html;
  266. return $this;
  267. }
  268. public function getHtml()
  269. {
  270. return $this->html;
  271. }
  272. public function setSendAt($timestamp)
  273. {
  274. $this->smtpapi->setSendAt($timestamp);
  275. return $this;
  276. }
  277. public function setSendEachAt(array $timestamps)
  278. {
  279. $this->smtpapi->setSendEachAt($timestamps);
  280. return $this;
  281. }
  282. public function addSendEachAt($timestamp)
  283. {
  284. $this->smtpapi->addSendEachAt($timestamp);
  285. return $this;
  286. }
  287. /**
  288. * Convenience method to add template
  289. *
  290. * @param string The id of the template
  291. *
  292. * @return $this
  293. */
  294. public function setTemplateId($templateId)
  295. {
  296. $this->addFilter('templates', 'enabled', 1);
  297. $this->addFilter('templates', 'template_id', $templateId);
  298. return $this;
  299. }
  300. /** Convenience method to set asm group id
  301. *
  302. * @param string the group id
  303. *
  304. * @return $this
  305. */
  306. public function setAsmGroupId($groupId)
  307. {
  308. $this->smtpapi->setASMGroupID($groupId);
  309. return $this;
  310. }
  311. public function setAttachments(array $files)
  312. {
  313. $this->attachments = array();
  314. foreach ($files as $filename => $file) {
  315. if (is_string($filename)) {
  316. $this->addAttachment($file, $filename);
  317. } else {
  318. $this->addAttachment($file);
  319. }
  320. }
  321. return $this;
  322. }
  323. public function setAttachment($file, $custom_filename = null, $cid = null)
  324. {
  325. $this->attachments = array($this->getAttachmentInfo($file, $custom_filename, $cid));
  326. return $this;
  327. }
  328. public function addAttachment($file, $custom_filename = null, $cid = null)
  329. {
  330. $this->attachments[] = $this->getAttachmentInfo($file, $custom_filename, $cid);
  331. return $this;
  332. }
  333. public function getAttachments()
  334. {
  335. return $this->attachments;
  336. }
  337. public function removeAttachment($file)
  338. {
  339. $this->_removeFromList($this->attachments, $file, "file");
  340. return $this;
  341. }
  342. private function getAttachmentInfo($file, $custom_filename = null, $cid = null)
  343. {
  344. $info = pathinfo($file);
  345. $info['file'] = $file;
  346. if (!is_null($custom_filename)) {
  347. $info['custom_filename'] = $custom_filename;
  348. }
  349. if ($cid !== null) {
  350. $info['cid'] = $cid;
  351. }
  352. return $info;
  353. }
  354. public function setCategories($categories)
  355. {
  356. $this->smtpapi->setCategories($categories);
  357. return $this;
  358. }
  359. public function setCategory($category)
  360. {
  361. $this->smtpapi->setCategory($category);
  362. return $this;
  363. }
  364. public function addCategory($category)
  365. {
  366. $this->smtpapi->addCategory($category);
  367. return $this;
  368. }
  369. public function removeCategory($category)
  370. {
  371. $this->smtpapi->removeCategory($category);
  372. return $this;
  373. }
  374. public function setSubstitutions($key_value_pairs)
  375. {
  376. $this->smtpapi->setSubstitutions($key_value_pairs);
  377. return $this;
  378. }
  379. public function addSubstitution($from_value, array $to_values)
  380. {
  381. $this->smtpapi->addSubstitution($from_value, $to_values);
  382. return $this;
  383. }
  384. public function setSections(array $key_value_pairs)
  385. {
  386. $this->smtpapi->setSections($key_value_pairs);
  387. return $this;
  388. }
  389. public function addSection($from_value, $to_value)
  390. {
  391. $this->smtpapi->addSection($from_value, $to_value);
  392. return $this;
  393. }
  394. public function setUniqueArgs(array $key_value_pairs)
  395. {
  396. $this->smtpapi->setUniqueArgs($key_value_pairs);
  397. return $this;
  398. }
  399. ## synonym method
  400. public function setUniqueArguments(array $key_value_pairs)
  401. {
  402. $this->smtpapi->setUniqueArgs($key_value_pairs);
  403. return $this;
  404. }
  405. public function addUniqueArg($key, $value)
  406. {
  407. $this->smtpapi->addUniqueArg($key, $value);
  408. return $this;
  409. }
  410. ## synonym method
  411. public function addUniqueArgument($key, $value)
  412. {
  413. $this->smtpapi->addUniqueArg($key, $value);
  414. return $this;
  415. }
  416. public function setFilters($filter_settings)
  417. {
  418. $this->smtpapi->setFilters($filter_settings);
  419. return $this;
  420. }
  421. ## synonym method
  422. public function setFilterSettings($filter_settings)
  423. {
  424. $this->smtpapi->setFilters($filter_settings);
  425. return $this;
  426. }
  427. public function addFilter($filter_name, $parameter_name, $parameter_value)
  428. {
  429. $this->smtpapi->addFilter($filter_name, $parameter_name, $parameter_value);
  430. return $this;
  431. }
  432. ## synonym method
  433. public function addFilterSetting($filter_name, $parameter_name, $parameter_value)
  434. {
  435. $this->smtpapi->addFilter($filter_name, $parameter_name, $parameter_value);
  436. return $this;
  437. }
  438. public function getHeaders()
  439. {
  440. return $this->headers;
  441. }
  442. public function getHeadersJson()
  443. {
  444. if (count($this->getHeaders()) <= 0) {
  445. return "{}";
  446. }
  447. return json_encode($this->getHeaders(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
  448. }
  449. public function setHeaders($key_value_pairs)
  450. {
  451. $this->headers = $key_value_pairs;
  452. return $this;
  453. }
  454. public function addHeader($key, $value)
  455. {
  456. $this->headers[$key] = $value;
  457. return $this;
  458. }
  459. public function removeHeader($key)
  460. {
  461. unset($this->headers[$key]);
  462. return $this;
  463. }
  464. public function getSmtpapi()
  465. {
  466. return $this->smtpapi;
  467. }
  468. public function toWebFormat()
  469. {
  470. $web = array(
  471. 'to' => $this->to,
  472. 'from' => $this->getFrom(),
  473. 'x-smtpapi' => $this->smtpapi->jsonString(),
  474. 'subject' => $this->getSubject(),
  475. 'text' => $this->getText(),
  476. 'html' => $this->getHtml(),
  477. 'headers' => $this->getHeadersJson(),
  478. );
  479. if ($this->getToNames()) {
  480. $web['toname'] = $this->getToNames();
  481. }
  482. if ($this->getCcs()) {
  483. $web['cc'] = $this->getCcs();
  484. }
  485. if ($this->getCcNames()) {
  486. $web['ccname'] = $this->getCcNames();
  487. }
  488. if ($this->getBccs()) {
  489. $web['bcc'] = $this->getBccs();
  490. }
  491. if ($this->getBccNames()) {
  492. $web['bccname'] = $this->getBccNames();
  493. }
  494. if ($this->getFromName()) {
  495. $web['fromname'] = $this->getFromName();
  496. }
  497. if ($this->getReplyTo()) {
  498. $web['replyto'] = $this->getReplyTo();
  499. }
  500. if ($this->getDate()) {
  501. $web['date'] = $this->getDate();
  502. }
  503. if ($this->smtpapi->to && (count($this->smtpapi->to) > 0)) {
  504. $web['to'] = "";
  505. }
  506. $web = $this->updateMissingTo($web);
  507. if ($this->getAttachments()) {
  508. foreach ($this->getAttachments() as $f) {
  509. $file = $f['file'];
  510. $extension = null;
  511. if (array_key_exists('extension', $f)) {
  512. $extension = $f['extension'];
  513. };
  514. $filename = $f['filename'];
  515. $full_filename = $filename;
  516. if (isset($extension)) {
  517. $full_filename = $filename . '.' . $extension;
  518. }
  519. if (array_key_exists('custom_filename', $f)) {
  520. $full_filename = $f['custom_filename'];
  521. }
  522. if (array_key_exists('cid', $f)) {
  523. $web['content[' . $full_filename . ']'] = $f['cid'];
  524. }
  525. $contents = '@' . $file;
  526. // Guzzle handles this for us.
  527. // http://guzzle3.readthedocs.org/en/latest/http-client/request.html#post-requests
  528. // if (class_exists('CurlFile', false)) { // php >= 5.5
  529. // $contents = new \CurlFile($file, $extension, $filename);
  530. // }
  531. $web['files[' . $full_filename . ']'] = $contents;
  532. };
  533. }
  534. return $web;
  535. }
  536. /**
  537. * There needs to be at least 1 to address, or else the mail won't send.
  538. * This method modifies the data that will be sent via either Rest
  539. */
  540. public function updateMissingTo($data)
  541. {
  542. if ($this->smtpapi->to && (count($this->smtpapi->to) > 0)) {
  543. $data['to'] = $this->getFrom();
  544. }
  545. return $data;
  546. }
  547. }