Message.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /***************************************************\
  3. *
  4. * Mailer (https://github.com/txthinking/Mailer)
  5. *
  6. * A lightweight PHP SMTP mail sender.
  7. * Implement RFC0821, RFC0822, RFC1869, RFC2045, RFC2821
  8. *
  9. * Support html body, don't worry that the receiver's
  10. * mail client can't support html, because Mailer will
  11. * send both text/plain and text/html body, so if the
  12. * mail client can't support html, it will display the
  13. * text/plain body.
  14. *
  15. * Create Date 2012-07-25.
  16. * Under the MIT license.
  17. *
  18. \***************************************************/
  19. namespace Tx\Mailer;
  20. class Message
  21. {
  22. /**
  23. * from name
  24. */
  25. protected $fromName;
  26. /**
  27. * from email
  28. */
  29. protected $fromEmail;
  30. /**
  31. * fake from name
  32. */
  33. protected $fakeFromName;
  34. /**
  35. * fake from email
  36. */
  37. protected $fakeFromEmail;
  38. /**
  39. * to email
  40. */
  41. protected $to = array();
  42. /**
  43. * cc email
  44. */
  45. protected $cc = array();
  46. /**
  47. * bcc email
  48. */
  49. protected $bcc = array();
  50. /**
  51. * mail subject
  52. */
  53. protected $subject;
  54. /**
  55. * mail body
  56. */
  57. protected $body;
  58. /**
  59. *mail attachment
  60. */
  61. protected $attachment = array();
  62. /**
  63. * message header
  64. */
  65. protected $header = array();
  66. /**
  67. * charset
  68. */
  69. protected $charset = "UTF-8";
  70. /**
  71. * header multipart boundaryMixed
  72. */
  73. protected $boundaryMixed;
  74. /**
  75. * header multipart alternative
  76. */
  77. protected $boundaryAlternative;
  78. /**
  79. * $this->CRLF
  80. * @var string
  81. */
  82. protected $CRLF = "\r\n";
  83. /**
  84. * Address for the reply-to header
  85. * @var string
  86. */
  87. protected $replyToName;
  88. /**
  89. * Address for the reply-to header
  90. * @var string
  91. */
  92. protected $replyToEmail;
  93. public function setReplyTo($name, $email)
  94. {
  95. $this->replyToName = $name;
  96. $this->replyToEmail = $email;
  97. return $this;
  98. }
  99. /**
  100. * set mail from
  101. * @param string $name
  102. * @param string $email
  103. * @return $this
  104. */
  105. public function setFrom($name, $email)
  106. {
  107. $this->fromName = $name;
  108. $this->fromEmail = $email;
  109. return $this;
  110. }
  111. /**
  112. * set mail fake from
  113. * @param string $name
  114. * @param string $email
  115. * @return $this
  116. */
  117. public function setFakeFrom($name, $email)
  118. {
  119. $this->fakeFromName = $name;
  120. $this->fakeFromEmail = $email;
  121. return $this;
  122. }
  123. /**
  124. * add mail receiver
  125. * @param string $name
  126. * @param string $email
  127. * @return $this
  128. */
  129. public function addTo($name, $email)
  130. {
  131. $this->to[$email] = $name;
  132. return $this;
  133. }
  134. /**
  135. * add cc mail receiver
  136. * @param string $name
  137. * @param string $email
  138. * @return $this
  139. */
  140. public function addCc($name, $email)
  141. {
  142. $this->cc[$email] = $name;
  143. return $this;
  144. }
  145. /**
  146. * add bcc mail receiver
  147. * @param string $name
  148. * @param string $email
  149. * @return $this
  150. */
  151. public function addBcc($name, $email)
  152. {
  153. $this->bcc[$email] = $name;
  154. return $this;
  155. }
  156. /**
  157. * set mail subject
  158. * @param string $subject
  159. * @return $this
  160. */
  161. public function setSubject($subject)
  162. {
  163. $this->subject = $subject;
  164. return $this;
  165. }
  166. /**
  167. * set mail body
  168. * @param string $body
  169. * @return $this
  170. */
  171. public function setBody($body)
  172. {
  173. $this->body = $body;
  174. return $this;
  175. }
  176. /**
  177. * add mail attachment
  178. * @param $name
  179. * @param $path
  180. * @return $this
  181. */
  182. public function addAttachment($name, $path)
  183. {
  184. $this->attachment[$name] = $path;
  185. return $this;
  186. }
  187. /**
  188. * @return string
  189. */
  190. public function getFromName()
  191. {
  192. return $this->fromName;
  193. }
  194. /**
  195. * @return string
  196. */
  197. public function getFromEmail()
  198. {
  199. return $this->fromEmail;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getFakeFromName()
  205. {
  206. return $this->fakeFromName;
  207. }
  208. /**
  209. * @return string
  210. */
  211. public function getFakeFromEmail()
  212. {
  213. return $this->fakeFromEmail;
  214. }
  215. /**
  216. * @return mixed
  217. */
  218. public function getTo()
  219. {
  220. return $this->to;
  221. }
  222. /**
  223. * @return mixed
  224. */
  225. public function getCc()
  226. {
  227. return $this->cc;
  228. }
  229. /**
  230. * @return mixed
  231. */
  232. public function getBcc()
  233. {
  234. return $this->bcc;
  235. }
  236. /**
  237. * @return mixed
  238. */
  239. public function getSubject()
  240. {
  241. return $this->subject;
  242. }
  243. /**
  244. * @return mixed
  245. */
  246. public function getBody()
  247. {
  248. return $this->body;
  249. }
  250. /**
  251. * @return array
  252. */
  253. public function getAttachment()
  254. {
  255. return $this->attachment;
  256. }
  257. /**
  258. * Create mail header
  259. * @return $this
  260. */
  261. protected function createHeader()
  262. {
  263. $this->header['Date'] = date('r');
  264. $fromName = "";
  265. $fromEmail = $this->fromEmail;
  266. if(!empty($this->fromName)){
  267. $fromName = sprintf("=?utf-8?B?%s?= ", base64_encode($this->fromName));
  268. }
  269. if(!empty($this->fakeFromEmail)){
  270. if(!empty($this->fakeFromName)){
  271. $fromName = sprintf("=?utf-8?B?%s?= ", base64_encode($this->fakeFromName));
  272. }
  273. $fromEmail = $this->fakeFromEmail;
  274. }
  275. $this->header['Return-Path'] = $fromEmail;
  276. $this->header['From'] = $fromName . "<" . $fromEmail .">";
  277. $this->header['To'] = '';
  278. foreach ($this->to as $toEmail => $toName) {
  279. if(!empty($toName)){
  280. $toName = sprintf("=?utf-8?B?%s?= ", base64_encode($toName));
  281. }
  282. $this->header['To'] .= $toName . "<" . $toEmail . ">, ";
  283. }
  284. $this->header['To'] = substr($this->header['To'], 0, -2);
  285. $this->header['Cc'] = '';
  286. foreach ($this->cc as $toEmail => $toName) {
  287. if(!empty($toName)){
  288. $toName = sprintf("=?utf-8?B?%s?= ", base64_encode($toName));
  289. }
  290. $this->header['Cc'] .= $toName . "<" . $toEmail . ">, ";
  291. }
  292. $this->header['Cc'] = substr($this->header['Cc'], 0, -2);
  293. $this->header['Bcc'] = '';
  294. foreach ($this->bcc as $toEmail => $toName) {
  295. if(!empty($toName)){
  296. $toName = sprintf("=?utf-8?B?%s?= ", base64_encode($toName));
  297. }
  298. $this->header['Bcc'] .= $toName . "<" . $toEmail . ">, ";
  299. }
  300. $this->header['Bcc'] = substr($this->header['Bcc'], 0, -2);
  301. $replyToName = "";
  302. if(!empty($this->replyToName)){
  303. $replyToName = sprintf("=?utf-8?B?%s?= ", base64_encode($this->replyToName));
  304. }
  305. $this->header['Reply-To'] = $replyToName . "<" . $this->replyToEmail . ">";
  306. if(empty($this->subject)){
  307. $subject = '';
  308. }else{
  309. $subject = sprintf("=?utf-8?B?%s?= ", base64_encode($this->subject));
  310. }
  311. $this->header['Subject'] = $subject;
  312. $this->header['Message-ID'] = '<' . md5(uniqid()) . '@' . $this->fromEmail . '>';
  313. $this->header['X-Priority'] = '3';
  314. $this->header['X-Mailer'] = 'Mailer (https://github.com/txthinking/Mailer)';
  315. $this->header['MIME-Version'] = '1.0';
  316. if (!empty($this->attachment)){
  317. $this->boundaryMixed = md5(md5(time().'TxMailer').uniqid());
  318. $this->header['Content-Type'] = "multipart/mixed; \r\n\tboundary=\"" . $this->boundaryMixed . "\"";
  319. }
  320. $this->boundaryAlternative = md5(md5(time().'TXMailer').uniqid());
  321. return $this;
  322. }
  323. /**
  324. * @brief createBody create body
  325. *
  326. * @return string
  327. */
  328. protected function createBody()
  329. {
  330. $in = "";
  331. $in .= "Content-Type: multipart/alternative; boundary=\"$this->boundaryAlternative\"" . $this->CRLF;
  332. $in .= $this->CRLF;
  333. $in .= "--" . $this->boundaryAlternative . $this->CRLF;
  334. $in .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->CRLF;
  335. $in .= "Content-Transfer-Encoding: base64" . $this->CRLF;
  336. $in .= $this->CRLF;
  337. $in .= chunk_split(base64_encode($this->body)) . $this->CRLF;
  338. $in .= $this->CRLF;
  339. $in .= "--" . $this->boundaryAlternative . $this->CRLF;
  340. $in .= "Content-Type: text/html; charset=\"" . $this->charset ."\"" . $this->CRLF;
  341. $in .= "Content-Transfer-Encoding: base64" . $this->CRLF;
  342. $in .= $this->CRLF;
  343. $in .= chunk_split(base64_encode($this->body)) . $this->CRLF;
  344. $in .= $this->CRLF;
  345. $in .= "--" . $this->boundaryAlternative . "--" . $this->CRLF;
  346. return $in;
  347. }
  348. /**
  349. * @brief createBodyWithAttachment create body with attachment
  350. *
  351. * @return string
  352. */
  353. protected function createBodyWithAttachment()
  354. {
  355. $in = "";
  356. $in .= $this->CRLF;
  357. $in .= $this->CRLF;
  358. $in .= '--' . $this->boundaryMixed . $this->CRLF;
  359. $in .= "Content-Type: multipart/alternative; boundary=\"$this->boundaryAlternative\"" . $this->CRLF;
  360. $in .= $this->CRLF;
  361. $in .= "--" . $this->boundaryAlternative . $this->CRLF;
  362. $in .= "Content-Type: text/plain; charset=\"" . $this->charset . "\"" . $this->CRLF;
  363. $in .= "Content-Transfer-Encoding: base64" . $this->CRLF;
  364. $in .= $this->CRLF;
  365. $in .= chunk_split(base64_encode($this->body)) . $this->CRLF;
  366. $in .= $this->CRLF;
  367. $in .= "--" . $this->boundaryAlternative . $this->CRLF;
  368. $in .= "Content-Type: text/html; charset=\"" . $this->charset ."\"" . $this->CRLF;
  369. $in .= "Content-Transfer-Encoding: base64" . $this->CRLF;
  370. $in .= $this->CRLF;
  371. $in .= chunk_split(base64_encode($this->body)) . $this->CRLF;
  372. $in .= $this->CRLF;
  373. $in .= "--" . $this->boundaryAlternative . "--" . $this->CRLF;
  374. foreach ($this->attachment as $name => $path){
  375. $in .= $this->CRLF;
  376. $in .= '--' . $this->boundaryMixed . $this->CRLF;
  377. $in .= "Content-Type: application/octet-stream; name=\"". $name ."\"" . $this->CRLF;
  378. $in .= "Content-Transfer-Encoding: base64" . $this->CRLF;
  379. $in .= "Content-Disposition: attachment; filename=\"" . $name . "\"" . $this->CRLF;
  380. $in .= $this->CRLF;
  381. $in .= chunk_split(base64_encode(file_get_contents($path))) . $this->CRLF;
  382. }
  383. $in .= $this->CRLF;
  384. $in .= $this->CRLF;
  385. $in .= '--' . $this->boundaryMixed . '--' . $this->CRLF;
  386. return $in;
  387. }
  388. public function toString()
  389. {
  390. $in = '';
  391. $this->createHeader();
  392. foreach ($this->header as $key => $value) {
  393. $in .= $key . ': ' . $value . $this->CRLF;
  394. }
  395. if (empty($this->attachment)) {
  396. $in .= $this->createBody();
  397. } else {
  398. $in .= $this->createBodyWithAttachment();
  399. }
  400. $in .= $this->CRLF . $this->CRLF . "." . $this->CRLF;
  401. return $in;
  402. }
  403. }