1
0

class.simple.mail.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. /**
  3. * Simple Mail
  4. *
  5. * A simple PHP wrapper class for sending email using the mail() method.
  6. *
  7. * PHP version > 5.2
  8. *
  9. * LICENSE: This source file is subject to the MIT license, which is
  10. * available through the world-wide-web at the following URI:
  11. * http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt
  12. *
  13. * @category SimpleMail
  14. * @package SimpleMail
  15. * @author Eoghan O'Brien <eoghan@eoghanobrien.com>
  16. * @copyright 2009 - 2017 Eoghan O'Brien
  17. * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT
  18. * @version 1.7.1
  19. * @link http://github.com/eoghanobrien/php-simple-mail
  20. */
  21. /**
  22. * Simple Mail class.
  23. *
  24. * @category SimpleMail
  25. * @package SimpleMail
  26. * @author Eoghan O'Brien <eoghan@eoghanobrien.com>
  27. * @copyright 2009 - 2017 Eoghan O'Brien
  28. * @license http://github.com/eoghanobrien/php-simple-mail/LICENCE.txt MIT
  29. * @version 1.7.1
  30. * @link http://github.com/eoghanobrien/php-simple-mail
  31. */
  32. class SimpleMail
  33. {
  34. /**
  35. * @var int $_wrap
  36. */
  37. protected $_wrap = 78;
  38. /**
  39. * @var array $_to
  40. */
  41. protected $_to = array();
  42. /**
  43. * @var string $_subject
  44. */
  45. protected $_subject;
  46. /**
  47. * @var string $_message
  48. */
  49. protected $_message;
  50. /**
  51. * @var array $_headers
  52. */
  53. protected $_headers = array();
  54. /**
  55. * @var string $_parameters
  56. */
  57. protected $_params;
  58. /**
  59. * @var array $_attachments
  60. */
  61. protected $_attachments = array();
  62. /**
  63. * @var string $_uid
  64. */
  65. protected $_uid;
  66. /**
  67. * Named constructor.
  68. *
  69. * @return static
  70. */
  71. public static function make()
  72. {
  73. return new SimpleMail();
  74. }
  75. /**
  76. * __construct
  77. *
  78. * Resets the class properties.
  79. */
  80. public function __construct()
  81. {
  82. $this->reset();
  83. }
  84. /**
  85. * reset
  86. *
  87. * Resets all properties to initial state.
  88. *
  89. * @return self
  90. */
  91. public function reset()
  92. {
  93. $this->_to = array();
  94. $this->_headers = array();
  95. $this->_subject = null;
  96. $this->_message = null;
  97. $this->_wrap = 78;
  98. $this->_params = null;
  99. $this->_attachments = array();
  100. $this->_uid = $this->getUniqueId();
  101. return $this;
  102. }
  103. /**
  104. * setTo
  105. *
  106. * @param string $email The email address to send to.
  107. * @param string $name The name of the person to send to.
  108. *
  109. * @return self
  110. */
  111. public function setTo($email, $name)
  112. {
  113. $this->_to[] = $this->formatHeader((string) $email, (string) $name);
  114. return $this;
  115. }
  116. /**
  117. * getTo
  118. *
  119. * Return an array of formatted To addresses.
  120. *
  121. * @return array
  122. */
  123. public function getTo()
  124. {
  125. return $this->_to;
  126. }
  127. /**
  128. * setFrom
  129. *
  130. * @param string $email The email to send as from.
  131. * @param string $name The name to send as from.
  132. *
  133. * @return self
  134. */
  135. public function setFrom($email, $name)
  136. {
  137. $this->addMailHeader('From', (string) $email, (string) $name);
  138. return $this;
  139. }
  140. /**
  141. * setCc
  142. *
  143. * @param array $pairs An array of name => email pairs.
  144. *
  145. * @return self
  146. */
  147. public function setCc(array $pairs)
  148. {
  149. return $this->addMailHeaders('Cc', $pairs);
  150. }
  151. /**
  152. * setBcc
  153. *
  154. * @param array $pairs An array of name => email pairs.
  155. *
  156. * @return self
  157. */
  158. public function setBcc(array $pairs)
  159. {
  160. return $this->addMailHeaders('Bcc', $pairs);
  161. }
  162. /**
  163. * setReplyTo
  164. *
  165. * @param string $email
  166. * @param string $name
  167. *
  168. * @return self
  169. */
  170. public function setReplyTo($email, $name = null)
  171. {
  172. return $this->addMailHeader('Reply-To', $email, $name);
  173. }
  174. /**
  175. * setHtml
  176. *
  177. * @return self
  178. */
  179. public function setHtml()
  180. {
  181. return $this->addGenericHeader(
  182. 'Content-Type', 'text/html; charset="utf-8"'
  183. );
  184. }
  185. /**
  186. * setSubject
  187. *
  188. * @param string $subject The email subject
  189. *
  190. * @return self
  191. */
  192. public function setSubject($subject)
  193. {
  194. $this->_subject = $this->encodeUtf8(
  195. $this->filterOther((string) $subject)
  196. );
  197. return $this;
  198. }
  199. /**
  200. * getSubject function.
  201. *
  202. * @return string
  203. */
  204. public function getSubject()
  205. {
  206. return $this->_subject;
  207. }
  208. /**
  209. * setMessage
  210. *
  211. * @param string $message The message to send.
  212. *
  213. * @return self
  214. */
  215. public function setMessage($message)
  216. {
  217. $this->_message = str_replace("\n.", "\n..", (string) $message);
  218. return $this;
  219. }
  220. /**
  221. * getMessage
  222. *
  223. * @return string
  224. */
  225. public function getMessage()
  226. {
  227. return $this->_message;
  228. }
  229. /**
  230. * addAttachment
  231. *
  232. * @param string $path The file path to the attachment.
  233. * @param string $filename The filename of the attachment when emailed.
  234. * @param null $data
  235. *
  236. * @return self
  237. */
  238. public function addAttachment($path, $filename = null, $data = null)
  239. {
  240. $filename = empty($filename) ? basename($path) : $filename;
  241. $filename = $this->encodeUtf8($this->filterOther((string) $filename));
  242. $data = empty($data) ? $this->getAttachmentData($path) : $data;
  243. $this->_attachments[] = array(
  244. 'path' => $path,
  245. 'file' => $filename,
  246. 'data' => chunk_split(base64_encode($data))
  247. );
  248. return $this;
  249. }
  250. /**
  251. * getAttachmentData
  252. *
  253. * @param string $path The path to the attachment file.
  254. *
  255. * @return string
  256. */
  257. public function getAttachmentData($path)
  258. {
  259. $filesize = filesize($path);
  260. $handle = fopen($path, "r");
  261. $attachment = fread($handle, $filesize);
  262. fclose($handle);
  263. return $attachment;
  264. }
  265. /**
  266. * addMailHeader
  267. *
  268. * @param string $header The header to add.
  269. * @param string $email The email to add.
  270. * @param string $name The name to add.
  271. *
  272. * @return self
  273. */
  274. public function addMailHeader($header, $email, $name = null)
  275. {
  276. $address = $this->formatHeader((string) $email, (string) $name);
  277. $this->_headers[] = sprintf('%s: %s', (string) $header, $address);
  278. return $this;
  279. }
  280. /**
  281. * addMailHeaders
  282. *
  283. * @param string $header The header to add.
  284. * @param array $pairs An array of name => email pairs.
  285. *
  286. * @return self
  287. */
  288. public function addMailHeaders($header, array $pairs)
  289. {
  290. if (count($pairs) === 0) {
  291. throw new InvalidArgumentException(
  292. 'You must pass at least one name => email pair.'
  293. );
  294. }
  295. $addresses = array();
  296. foreach ($pairs as $name => $email) {
  297. $name = is_numeric($name) ? null : $name;
  298. $addresses[] = $this->formatHeader($email, $name);
  299. }
  300. $this->addGenericHeader($header, implode(',', $addresses));
  301. return $this;
  302. }
  303. /**
  304. * addGenericHeader
  305. *
  306. * @param string $header The generic header to add.
  307. * @param mixed $value The value of the header.
  308. *
  309. * @return self
  310. */
  311. public function addGenericHeader($header, $value)
  312. {
  313. $this->_headers[] = sprintf(
  314. '%s: %s',
  315. (string) $header,
  316. (string) $value
  317. );
  318. return $this;
  319. }
  320. /**
  321. * getHeaders
  322. *
  323. * Return the headers registered so far as an array.
  324. *
  325. * @return array
  326. */
  327. public function getHeaders()
  328. {
  329. return $this->_headers;
  330. }
  331. /**
  332. * setAdditionalParameters
  333. *
  334. * Such as "-fyouremail@yourserver.com
  335. *
  336. * @param string $additionalParameters The addition mail parameter.
  337. *
  338. * @return self
  339. */
  340. public function setParameters($additionalParameters)
  341. {
  342. $this->_params = (string) $additionalParameters;
  343. return $this;
  344. }
  345. /**
  346. * getAdditionalParameters
  347. *
  348. * @return string
  349. */
  350. public function getParameters()
  351. {
  352. return $this->_params;
  353. }
  354. /**
  355. * setWrap
  356. *
  357. * @param int $wrap The number of characters at which the message will wrap.
  358. *
  359. * @return self
  360. */
  361. public function setWrap($wrap = 78)
  362. {
  363. $wrap = (int) $wrap;
  364. if ($wrap < 1) {
  365. $wrap = 78;
  366. }
  367. $this->_wrap = $wrap;
  368. return $this;
  369. }
  370. /**
  371. * getWrap
  372. *
  373. * @return int
  374. */
  375. public function getWrap()
  376. {
  377. return $this->_wrap;
  378. }
  379. /**
  380. * hasAttachments
  381. *
  382. * Checks if the email has any registered attachments.
  383. *
  384. * @return bool
  385. */
  386. public function hasAttachments()
  387. {
  388. return !empty($this->_attachments);
  389. }
  390. /**
  391. * assembleAttachment
  392. *
  393. * @return string
  394. */
  395. public function assembleAttachmentHeaders()
  396. {
  397. $head = array();
  398. $head[] = "MIME-Version: 1.0";
  399. $head[] = "Content-Type: multipart/mixed; boundary=\"{$this->_uid}\"";
  400. return join(PHP_EOL, $head);
  401. }
  402. /**
  403. * assembleAttachmentBody
  404. *
  405. * @return string
  406. */
  407. public function assembleAttachmentBody()
  408. {
  409. $body = array();
  410. $body[] = "This is a multi-part message in MIME format.";
  411. $body[] = "--{$this->_uid}";
  412. $body[] = "Content-Type: text/html; charset=\"utf-8\"";
  413. $body[] = "Content-Transfer-Encoding: quoted-printable";
  414. $body[] = "";
  415. $body[] = quoted_printable_encode($this->_message);
  416. $body[] = "";
  417. $body[] = "--{$this->_uid}";
  418. foreach ($this->_attachments as $attachment) {
  419. $body[] = $this->getAttachmentMimeTemplate($attachment);
  420. }
  421. return implode(PHP_EOL, $body) . '--';
  422. }
  423. /**
  424. * getAttachmentMimeTemplate
  425. *
  426. * @param array $attachment An array containing 'file' and 'data' keys.
  427. *
  428. * @return string
  429. */
  430. public function getAttachmentMimeTemplate($attachment)
  431. {
  432. $file = $attachment['file'];
  433. $data = $attachment['data'];
  434. $head = array();
  435. $head[] = "Content-Type: application/octet-stream; name=\"{$file}\"";
  436. $head[] = "Content-Transfer-Encoding: base64";
  437. $head[] = "Content-Disposition: attachment; filename=\"{$file}\"";
  438. $head[] = "";
  439. $head[] = $data;
  440. $head[] = "";
  441. $head[] = "--{$this->_uid}";
  442. return implode(PHP_EOL, $head);
  443. }
  444. /**
  445. * send
  446. *
  447. * @return boolean
  448. * @throws \RuntimeException on no 'To: ' address to send to.
  449. */
  450. public function send()
  451. {
  452. $to = $this->getToForSend();
  453. $headers = $this->getHeadersForSend();
  454. if (empty($to)) {
  455. throw new \RuntimeException(
  456. 'Unable to send, no To address has been set.'
  457. );
  458. }
  459. if ($this->hasAttachments()) {
  460. $message = $this->assembleAttachmentBody();
  461. $headers .= PHP_EOL . $this->assembleAttachmentHeaders();
  462. } else {
  463. $message = $this->getWrapMessage();
  464. }
  465. return mail($to, $this->_subject, $message, $headers, $this->_params);
  466. }
  467. /**
  468. * debug
  469. *
  470. * @return string
  471. */
  472. public function debug()
  473. {
  474. return '<pre>' . print_r($this, true) . '</pre>';
  475. }
  476. /**
  477. * magic __toString function
  478. *
  479. * @return string
  480. */
  481. public function __toString()
  482. {
  483. return print_r($this, true);
  484. }
  485. /**
  486. * formatHeader
  487. *
  488. * Formats a display address for emails according to RFC2822 e.g.
  489. * Name <address@domain.tld>
  490. *
  491. * @param string $email The email address.
  492. * @param string $name The display name.
  493. *
  494. * @return string
  495. */
  496. public function formatHeader($email, $name = null)
  497. {
  498. $email = $this->filterEmail((string) $email);
  499. if (empty($name)) {
  500. return $email;
  501. }
  502. $name = $this->encodeUtf8($this->filterName((string) $name));
  503. return sprintf('"%s" <%s>', $name, $email);
  504. }
  505. /**
  506. * encodeUtf8
  507. *
  508. * @param string $value The value to encode.
  509. *
  510. * @return string
  511. */
  512. public function encodeUtf8($value)
  513. {
  514. $value = trim($value);
  515. if (preg_match('/(\s)/', $value)) {
  516. return $this->encodeUtf8Words($value);
  517. }
  518. return $this->encodeUtf8Word($value);
  519. }
  520. /**
  521. * encodeUtf8Word
  522. *
  523. * @param string $value The word to encode.
  524. *
  525. * @return string
  526. */
  527. public function encodeUtf8Word($value)
  528. {
  529. return sprintf('=?UTF-8?B?%s?=', base64_encode($value));
  530. }
  531. /**
  532. * encodeUtf8Words
  533. *
  534. * @param string $value The words to encode.
  535. *
  536. * @return string
  537. */
  538. public function encodeUtf8Words($value)
  539. {
  540. $words = explode(' ', $value);
  541. $encoded = array();
  542. foreach ($words as $word) {
  543. $encoded[] = $this->encodeUtf8Word($word);
  544. }
  545. return join($this->encodeUtf8Word(' '), $encoded);
  546. }
  547. /**
  548. * filterEmail
  549. *
  550. * Removes any carriage return, line feed, tab, double quote, comma
  551. * and angle bracket characters before sanitizing the email address.
  552. *
  553. * @param string $email The email to filter.
  554. *
  555. * @return string
  556. */
  557. public function filterEmail($email)
  558. {
  559. $rule = array(
  560. "\r" => '',
  561. "\n" => '',
  562. "\t" => '',
  563. '"' => '',
  564. ',' => '',
  565. '<' => '',
  566. '>' => ''
  567. );
  568. $email = strtr($email, $rule);
  569. $email = filter_var($email, FILTER_SANITIZE_EMAIL);
  570. return $email;
  571. }
  572. /**
  573. * filterName
  574. *
  575. * Removes any carriage return, line feed or tab characters. Replaces
  576. * double quotes with single quotes and angle brackets with square
  577. * brackets, before sanitizing the string and stripping out html tags.
  578. *
  579. * @param string $name The name to filter.
  580. *
  581. * @return string
  582. */
  583. public function filterName($name)
  584. {
  585. $rule = array(
  586. "\r" => '',
  587. "\n" => '',
  588. "\t" => '',
  589. '"' => "'",
  590. '<' => '[',
  591. '>' => ']',
  592. );
  593. $filtered = filter_var(
  594. $name,
  595. FILTER_SANITIZE_STRING,
  596. FILTER_FLAG_NO_ENCODE_QUOTES
  597. );
  598. return trim(strtr($filtered, $rule));
  599. }
  600. /**
  601. * filterOther
  602. *
  603. * Removes ASCII control characters including any carriage return, line
  604. * feed or tab characters.
  605. *
  606. * @param string $data The data to filter.
  607. *
  608. * @return string
  609. */
  610. public function filterOther($data)
  611. {
  612. return filter_var($data, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
  613. }
  614. /**
  615. * getHeadersForSend
  616. *
  617. * @return string
  618. */
  619. public function getHeadersForSend()
  620. {
  621. if (empty($this->_headers)) {
  622. return '';
  623. }
  624. return join(PHP_EOL, $this->_headers);
  625. }
  626. /**
  627. * getToForSend
  628. *
  629. * @return string
  630. */
  631. public function getToForSend()
  632. {
  633. if (empty($this->_to)) {
  634. return '';
  635. }
  636. return join(', ', $this->_to);
  637. }
  638. /**
  639. * getUniqueId
  640. *
  641. * @return string
  642. */
  643. public function getUniqueId()
  644. {
  645. return md5(uniqid(time()));
  646. }
  647. /**
  648. * getWrapMessage
  649. *
  650. * @return string
  651. */
  652. public function getWrapMessage()
  653. {
  654. return wordwrap($this->_message, $this->_wrap);
  655. }
  656. }