PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/sendgrid/lib/SendGrid/Email.php

https://bitbucket.org/andreustimm/zurmo
PHP | 377 lines | 292 code | 69 blank | 16 comment | 26 complexity | 3f4080eec8fae6aa7af47b36dddb675a MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. namespace SendGrid;
  3. class Email {
  4. public $to,
  5. $from,
  6. $from_name,
  7. $reply_to,
  8. $cc_list,
  9. $bcc_list,
  10. $subject,
  11. $text,
  12. $html,
  13. $headers,
  14. $smtpapi,
  15. $attachments;
  16. public function __construct() {
  17. $this->from_name = false;
  18. $this->reply_to = false;
  19. $this->smtpapi = new \Smtpapi\Header();
  20. }
  21. /**
  22. * _removeFromList
  23. * Given a list of key/value pairs, removes the associated keys
  24. * where a value matches the given string ($item)
  25. * @param Array $list - the list of key/value pairs
  26. * @param String $item - the value to be removed
  27. */
  28. private function _removeFromList(&$list, $item, $key_field = null) {
  29. foreach ($list as $key => $val) {
  30. if($key_field) {
  31. if($val[$key_field] == $item) {
  32. unset($list[$key]);
  33. }
  34. } else {
  35. if ($val == $item) {
  36. unset($list[$key]);
  37. }
  38. }
  39. }
  40. //repack the indices
  41. $list = array_values($list);
  42. }
  43. public function addTo($email, $name=null) {
  44. $this->smtpapi->addTo($email, $name);
  45. return $this;
  46. }
  47. public function setTos(array $emails) {
  48. $this->smtpapi->setTos($emails);
  49. return $this;
  50. }
  51. public function setFrom($email) {
  52. $this->from = $email;
  53. return $this;
  54. }
  55. public function getFrom($as_array = false) {
  56. if($as_array && ($name = $this->getFromName())) {
  57. return array("$this->from" => $name);
  58. } else {
  59. return $this->from;
  60. }
  61. }
  62. public function setFromName($name) {
  63. $this->from_name = $name;
  64. return $this;
  65. }
  66. public function getFromName() {
  67. return $this->from_name;
  68. }
  69. public function setReplyTo($email) {
  70. $this->reply_to = $email;
  71. return $this;
  72. }
  73. public function getReplyTo() {
  74. return $this->reply_to;
  75. }
  76. public function setCc($email) {
  77. $this->cc_list = array($email);
  78. return $this;
  79. }
  80. public function setCcs(array $email_list) {
  81. $this->cc_list = $email_list;
  82. return $this;
  83. }
  84. public function addCc($email) {
  85. $this->cc_list[] = $email;
  86. return $this;
  87. }
  88. public function removeCc($email) {
  89. $this->_removeFromList($this->cc_list, $email);
  90. return $this;
  91. }
  92. public function getCcs() {
  93. return $this->cc_list;
  94. }
  95. public function setBcc($email) {
  96. $this->bcc_list = array($email);
  97. return $this;
  98. }
  99. public function setBccs($email_list) {
  100. $this->bcc_list = $email_list;
  101. return $this;
  102. }
  103. public function addBcc($email) {
  104. $this->bcc_list[] = $email;
  105. return $this;
  106. }
  107. public function removeBcc($email) {
  108. $this->_removeFromList($this->bcc_list, $email);
  109. return $this;
  110. }
  111. public function getBccs() {
  112. return $this->bcc_list;
  113. }
  114. public function setSubject($subject) {
  115. $this->subject = $subject;
  116. return $this;
  117. }
  118. public function getSubject() {
  119. return $this->subject;
  120. }
  121. public function setText($text) {
  122. $this->text = $text;
  123. return $this;
  124. }
  125. public function getText() {
  126. return $this->text;
  127. }
  128. public function setHtml($html) {
  129. $this->html = $html;
  130. return $this;
  131. }
  132. public function getHtml() {
  133. return $this->html;
  134. }
  135. public function setAttachments(array $files) {
  136. $this->attachments = array();
  137. foreach($files as $filename => $file) {
  138. if (is_string($filename)) {
  139. $this->addAttachment($file, $filename);
  140. } else {
  141. $this->addAttachment($file);
  142. }
  143. }
  144. return $this;
  145. }
  146. public function setAttachment($file, $custom_filename=null) {
  147. $this->attachments = array($this->_getAttachmentInfo($file, $custom_filename));
  148. return $this;
  149. }
  150. public function addAttachment($file, $custom_filename=null) {
  151. $this->attachments[] = $this->_getAttachmentInfo($file, $custom_filename);
  152. return $this;
  153. }
  154. public function getAttachments() {
  155. return $this->attachments;
  156. }
  157. public function removeAttachment($file) {
  158. $this->_removeFromList($this->attachments, $file, "file");
  159. return $this;
  160. }
  161. private function _getAttachmentInfo($file, $custom_filename=null) {
  162. $info = pathinfo($file);
  163. $info['file'] = $file;
  164. if (!is_null($custom_filename)) {
  165. $info['custom_filename'] = $custom_filename;
  166. }
  167. return $info;
  168. }
  169. public function setCategories($categories) {
  170. $this->smtpapi->setCategories($categories);
  171. return $this;
  172. }
  173. public function setCategory($category) {
  174. $this->smtpapi->setCategory($category);
  175. return $this;
  176. }
  177. public function addCategory($category) {
  178. $this->smtpapi->addCategory($category);
  179. return $this;
  180. }
  181. public function removeCategory($category) {
  182. $this->smtpapi->removeCategory($category);
  183. return $this;
  184. }
  185. public function setSubstitutions($key_value_pairs) {
  186. $this->smtpapi->setSubstitutions($key_value_pairs);
  187. return $this;
  188. }
  189. public function addSubstitution($from_value, array $to_values) {
  190. $this->smtpapi->addSubstitution($from_value, $to_values);
  191. return $this;
  192. }
  193. public function setSections(array $key_value_pairs) {
  194. $this->smtpapi->setSections($key_value_pairs);
  195. return $this;
  196. }
  197. public function addSection($from_value, $to_value) {
  198. $this->smtpapi->addSection($from_value, $to_value);
  199. return $this;
  200. }
  201. public function setUniqueArgs(array $key_value_pairs) {
  202. $this->smtpapi->setUniqueArgs($key_value_pairs);
  203. return $this;
  204. }
  205. ## synonym method
  206. public function setUniqueArguments(array $key_value_pairs) {
  207. $this->smtpapi->setUniqueArgs($key_value_pairs);
  208. return $this;
  209. }
  210. public function addUniqueArg($key, $value) {
  211. $this->smtpapi->addUniqueArg($key, $value);
  212. return $this;
  213. }
  214. ## synonym method
  215. public function addUniqueArgument($key, $value) {
  216. $this->smtpapi->addUniqueArg($key, $value);
  217. return $this;
  218. }
  219. public function setFilters($filter_settings) {
  220. $this->smtpapi->setFilters($filter_settings);
  221. return $this;
  222. }
  223. ## synonym method
  224. public function setFilterSettings($filter_settings) {
  225. $this->smtpapi->setFilters($filter_settings);
  226. return $this;
  227. }
  228. public function addFilter($filter_name, $parameter_name, $parameter_value) {
  229. $this->smtpapi->addFilter($filter_name, $parameter_name, $parameter_value);
  230. return $this;
  231. }
  232. ## synonym method
  233. public function addFilterSetting($filter_name, $parameter_name, $parameter_value) {
  234. $this->smtpapi->addFilter($filter_name, $parameter_name, $parameter_value);
  235. return $this;
  236. }
  237. public function getHeaders() {
  238. return $this->headers;
  239. }
  240. public function getHeadersJson() {
  241. if (count($this->getHeaders()) <= 0) {
  242. return "{}";
  243. }
  244. return json_encode($this->getHeaders(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
  245. }
  246. public function setHeaders($key_value_pairs) {
  247. $this->headers = $key_value_pairs;
  248. return $this;
  249. }
  250. public function addHeader($key, $value) {
  251. $this->headers[$key] = $value;
  252. return $this;
  253. }
  254. public function removeHeader($key) {
  255. unset($this->headers[$key]);
  256. return $this;
  257. }
  258. public function toWebFormat() {
  259. $web = array(
  260. 'to' => $this->to,
  261. 'from' => $this->getFrom(),
  262. 'x-smtpapi' => $this->smtpapi->jsonString(),
  263. 'subject' => $this->getSubject(),
  264. 'text' => $this->getText(),
  265. 'html' => $this->getHtml(),
  266. 'headers' => $this->getHeadersJson(),
  267. );
  268. if ($this->getCcs()) { $web['cc'] = $this->getCcs(); }
  269. if ($this->getBccs()) { $web['bcc'] = $this->getBccs(); }
  270. if ($this->getFromName()) { $web['fromname'] = $this->getFromName(); }
  271. if ($this->getReplyTo()) { $web['replyto'] = $this->getReplyTo(); }
  272. if ($this->smtpapi->to && (count($this->smtpapi->to) > 0)) { $web['to'] = ""; }
  273. $web = $this->updateMissingTo($web);
  274. if ($this->getAttachments()) {
  275. foreach($this->getAttachments() as $f) {
  276. $file = $f['file'];
  277. $extension = null;
  278. if (array_key_exists('extension', $f)) {
  279. $extension = $f['extension'];
  280. };
  281. $filename = $f['filename'];
  282. $full_filename = $filename;
  283. if (isset($extension)) {
  284. $full_filename = $filename.'.'.$extension;
  285. }
  286. if (array_key_exists('custom_filename', $f)) {
  287. $full_filename = $f['custom_filename'];
  288. }
  289. $contents = '@' . $file;
  290. if (class_exists('CurlFile', false)) { // php >= 5.5
  291. $contents = new \CurlFile($file, $extension, $filename);
  292. }
  293. $web['files['.$full_filename.']'] = $contents;
  294. };
  295. }
  296. return $web;
  297. }
  298. /**
  299. * There needs to be at least 1 to address, or else the mail won't send.
  300. * This method modifies the data that will be sent via either Rest
  301. */
  302. public function updateMissingTo($data) {
  303. if ($this->smtpapi->to && (count($this->smtpapi->to) > 0)) {
  304. $data['to'] = $this->getFrom();
  305. }
  306. return $data;
  307. }
  308. }