Mail.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. /**
  3. * Creates a new Mail object.
  4. *
  5. * Template File:
  6. * In the template file a custom comment <!--STAPLE-EMAIL-BODY--> this is where the body of
  7. * the email is replaced.
  8. *
  9. *
  10. * @author Ironpilot
  11. * @copyright Copywrite (c) 2011, STAPLE CODE
  12. *
  13. * This file is part of the STAPLE Framework.
  14. *
  15. * The STAPLE Framework is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Lesser General Public License as published by the
  17. * Free Software Foundation, either version 3 of the License, or (at your option)
  18. * any later version.
  19. *
  20. * The STAPLE Framework is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  22. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  23. * more details.
  24. *
  25. * You should have received a copy of the GNU Lesser General Public License
  26. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. class Staple_Mail
  30. {
  31. const EMAIL_BODY_FIELD = '<!--STAPLE-EMAIL-BODY-->';
  32. /**
  33. * Whether or not to send email as HTML. Defaults to true.
  34. * @var boolean
  35. */
  36. protected $html = true;
  37. /**
  38. * Array of To addresses
  39. * @var array[string]
  40. */
  41. protected $to = array();
  42. /**
  43. * Array of Carbon Copied addresses.
  44. * @var array[string]
  45. */
  46. protected $cc = array();
  47. /**
  48. * Array of Blind Carbon Copied addresses.
  49. * @var array[string]
  50. */
  51. protected $bcc = array();
  52. /**
  53. * The From field.
  54. * @var string
  55. */
  56. protected $from;
  57. /**
  58. * The reply-to field. (Optional) Only required if you want the email/text to reply to a
  59. * different address than specified in the from field.
  60. * @var string
  61. */
  62. protected $replyto;
  63. /**
  64. * Subject of the email/text
  65. * @var string
  66. */
  67. protected $subject;
  68. /**
  69. *
  70. * The Body of the email/text.
  71. * @var string
  72. */
  73. protected $body;
  74. /**
  75. *
  76. * String listing the location of the email template file.
  77. * @var string
  78. */
  79. protected $template;
  80. /**
  81. * Stores callback functions to be processed after sending email.
  82. * @var array
  83. */
  84. protected $callbacks = array();
  85. protected $lastEmailStatus;
  86. /**
  87. *
  88. * Default constructor. Accepts optional values for To, From, CC, and BCC.
  89. * @param string | array $to
  90. * @param string $from
  91. * @param array $cc
  92. * @param array $bcc
  93. */
  94. public function __construct($to = NULL, $from = NULL, array $cc = array(), array $bcc = array())
  95. {
  96. //Load the ini settings
  97. $settings = Staple_Config::get('email');
  98. if($settings['from'] != '')
  99. {
  100. $this->setFrom($settings['from']);
  101. }
  102. if($settings['bcc'] != '')
  103. {
  104. $this->addBcc($settings['bcc']);
  105. }
  106. if($settings['html'] == '0')
  107. {
  108. $this->sendAsHtml(false);
  109. }
  110. if($settings['server'] != '')
  111. {
  112. $this->setServer($settings['server']);
  113. }
  114. if(array_key_exists('template', $settings))
  115. if($settings['template'] != '')
  116. $this->setTemplate($settings['template']);
  117. //Load any Tos
  118. if(isset($to))
  119. {
  120. if(is_array($to))
  121. {
  122. foreach($to as $email)
  123. {
  124. $this->addTo($email);
  125. }
  126. }
  127. else
  128. {
  129. $this->addTo($to);
  130. }
  131. }
  132. //Load the From
  133. if(isset($from))
  134. {
  135. $this->setFrom($from);
  136. }
  137. //Load any CCs
  138. if(isset($cc))
  139. {
  140. if(is_array($cc))
  141. {
  142. $this->setCc($cc);
  143. }
  144. else
  145. {
  146. $this->setCc(array($cc));
  147. }
  148. }
  149. //Load any BCCs
  150. if(isset($bcc))
  151. {
  152. if(is_array($bcc))
  153. {
  154. $this->setBcc($bcc);
  155. }
  156. else
  157. {
  158. $this->setBcc(array($bcc));
  159. }
  160. }
  161. }
  162. /**
  163. * Factory method to create an Mail object on the fly.
  164. * @param string | array $to
  165. * @param string $from
  166. * @param array $cc
  167. * @param array $bcc
  168. * @return Staple_Mail
  169. */
  170. public static function Create($to = NULL, $from = NULL, array $cc = array(), array $bcc = array())
  171. {
  172. return new self($to, $from, $cc);
  173. }
  174. /**
  175. * Sends an email. Optional parameters for To, Subject, Body, From, CC, and BCC.
  176. * @param string | array $to
  177. * @param string $subject
  178. * @param string $body
  179. * @param string $from
  180. * @param array $cc
  181. * @param array $bcc
  182. * @return boolean
  183. */
  184. public function Email($to = NULL, $subject = NULL, $body = NULL, $from = NULL, array $cc = array(), array $bcc = array())
  185. {
  186. //Check for new To addresses;
  187. if(isset($to))
  188. {
  189. if(is_array($to))
  190. {
  191. $this->setTo($to);
  192. }
  193. else
  194. {
  195. $this->setTo(array($to));
  196. }
  197. }
  198. //Check for a new Subject
  199. if(isset($subject))
  200. {
  201. $this->setSubject($subject);
  202. }
  203. //Check for a new Body
  204. if(isset($body))
  205. {
  206. $this->setBody($body);
  207. }
  208. //Check for a new From
  209. if(isset($from))
  210. {
  211. $this->setFrom($from);
  212. }
  213. //Check for new CC addresses
  214. if(isset($cc))
  215. {
  216. if(is_array($cc))
  217. {
  218. if(count($cc) > 0)
  219. {
  220. $this->setCc($cc);
  221. }
  222. }
  223. else
  224. {
  225. $this->setCc(array($cc));
  226. }
  227. }
  228. //Check for new BCC addresses
  229. if(isset($bcc))
  230. {
  231. if(is_array($bcc))
  232. {
  233. if(count($bcc) > 0)
  234. {
  235. $this->setBcc($bcc);
  236. }
  237. }
  238. else
  239. {
  240. $this->setBcc(array($bcc));
  241. }
  242. }
  243. //Check that all required fields have been completed before attempting to send.
  244. if($this->checkMailRequiredFields())
  245. {
  246. $toList = implode(', ',$this->to);
  247. //Start the Headers and specify who the email is to.
  248. $headers = "To: $toList\r\n";
  249. // Enable HTML emailing.
  250. if($this->html === true)
  251. {
  252. // To send HTML mail, the Content-type header must be set
  253. $headers .= 'MIME-Version: 1.0' . "\r\n";
  254. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  255. }
  256. // Set the from and reply to headers for the email.
  257. $headers .= "From: {$this->from}\r\n";
  258. if(isset($this->replyto))
  259. {
  260. $headers .= "Reply-To: {$this->replyto}\r\n";
  261. }
  262. // Carbon Copy an email to specified email.
  263. if(count($this->cc) > 0)
  264. {
  265. $ccList = implode(', ',$this->cc);
  266. $headers .= "CC: $ccList\r\n";
  267. }
  268. // Blind Carbon Copy an email to specified email.
  269. if(count($this->bcc) > 0)
  270. {
  271. $bccList = implode(', ',$this->bcc);
  272. $headers .= "Bcc: $bccList\r\n";
  273. }
  274. $headers .= 'X-Mailer: PHP/' . phpversion();
  275. //Body Windows Fix
  276. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
  277. $fixedbody = str_replace("\n.", "\n..", $this->getBody());
  278. else
  279. $fixedbody = $this->getBody();
  280. //Base64 encode the email subject
  281. $subject64 = '=?UTF-8?B?'.base64_encode($this->getSubject()).'?=';
  282. //Send Mail
  283. $success = mail($toList,$subject64,$fixedbody,$headers);
  284. $this->setLastEmailStatus($success);
  285. //Process any callback functions that might exist.
  286. $this->processCallbacks();
  287. return $success;
  288. }
  289. return false;
  290. }
  291. /**
  292. * Sends a text message.
  293. * @param array | string $to
  294. * @param string $subject
  295. * @param string $body
  296. * @param string $from
  297. * @return boolean
  298. */
  299. public function Text($to = NULL, $subject = NULL, $body = NULL, $from = NULL)
  300. {
  301. //Check for new To addresses;
  302. if(isset($to))
  303. {
  304. if(is_array($to))
  305. {
  306. $this->setTo($to);
  307. }
  308. else
  309. {
  310. $this->setTo(array($to));
  311. }
  312. }
  313. //Check for a new Subject
  314. if(isset($subject))
  315. {
  316. $this->setSubject($subject);
  317. }
  318. //Check for a new Body
  319. if(isset($body))
  320. {
  321. $this->setBody($body);
  322. }
  323. //Check for a new From
  324. if(isset($from))
  325. {
  326. $this->setFrom($from);
  327. }
  328. //Check that all required fields have been completed before attempting to send.
  329. if($this->checkMailRequiredFields())
  330. {
  331. $toList = implode(', ',$this->to);
  332. //Start the Headers and specify who the email is to.
  333. $headers = "To: $toList\r\n";
  334. // Set the from header for the text.
  335. $headers .= "From: {$this->from}\r\n";
  336. $headers .= 'X-Mailer: PHP/' . phpversion();
  337. //Send Mail
  338. //@todo make the program split these into 160 character emails.
  339. $success = mail($toList,$this->getSubject(),$this->getBody(),$headers);
  340. $this->setLastEmailStatus($success);
  341. //Process any callback functions that might exist.
  342. $this->processCallbacks();
  343. return $success;
  344. }
  345. return false;
  346. }
  347. /**
  348. * An alias of the Email() function. This function calls Email() sending no parameters.
  349. * @see Staple_Mail::Email()
  350. */
  351. public function Send()
  352. {
  353. return $this->Email();
  354. }
  355. /**
  356. * Add a single email address to the To list.
  357. * @param string $to
  358. * @return Staple_Mail
  359. */
  360. public function addTo($to)
  361. {
  362. if($this->checkEmailFormat($to))
  363. {
  364. if(!in_array($to, $this->to))
  365. {
  366. array_push($this->to, $to);
  367. }
  368. }
  369. return $this;
  370. }
  371. /**
  372. * Add a single email address to the CC list.
  373. * @param string $to
  374. * @return Staple_Mail
  375. */
  376. public function addCc($to)
  377. {
  378. if($this->checkEmailFormat($to))
  379. {
  380. array_push($this->cc, $to);
  381. }
  382. return $this;
  383. }
  384. /**
  385. * Add a single email address to the BCC list.
  386. * @param string $to
  387. * @return Staple_Mail
  388. */
  389. public function addBcc($to)
  390. {
  391. if($this->checkEmailFormat($to))
  392. {
  393. array_push($this->bcc, $to);
  394. }
  395. return $this;
  396. }
  397. /**
  398. * @return array[string] $to
  399. */
  400. public function getTo()
  401. {
  402. return $this->to;
  403. }
  404. /**
  405. * @param array $to
  406. * @return Staple_Mail
  407. */
  408. public function setTo(array $to)
  409. {
  410. $this->to = array();
  411. foreach($to as $email)
  412. {
  413. if($this->checkEmailFormat($email))
  414. {
  415. array_push($this->to, $email);
  416. }
  417. }
  418. return $this;
  419. }
  420. /**
  421. * @return the $cc
  422. */
  423. public function getCc()
  424. {
  425. return $this->cc;
  426. }
  427. /**
  428. * @param array $cc
  429. * @return Staple_Mail
  430. */
  431. public function setCc(array $cc)
  432. {
  433. $this->cc = array();
  434. foreach($cc as $email)
  435. {
  436. if($this->checkEmailFormat($email))
  437. {
  438. array_push($this->cc, $email);
  439. }
  440. }
  441. return $this;
  442. }
  443. /**
  444. * @return the $from
  445. */
  446. public function getFrom()
  447. {
  448. return $this->from;
  449. }
  450. /**
  451. * @param string $from
  452. * @return Staple_Mail
  453. */
  454. public function setFrom($from)
  455. {
  456. if($this->checkEmailFormat($from))
  457. {
  458. $this->from = $from;
  459. }
  460. return $this;
  461. }
  462. /**
  463. * @return the $replyto
  464. */
  465. public function getReplyto()
  466. {
  467. return $this->replyto;
  468. }
  469. /**
  470. * @param string $replyto
  471. * @return Staple_Mail
  472. */
  473. public function setReplyto($replyto)
  474. {
  475. if($this->checkEmailFormat($replyto))
  476. {
  477. $this->replyto = $replyto;
  478. }
  479. return $this;
  480. }
  481. /**
  482. * @return the $subject
  483. */
  484. public function getSubject()
  485. {
  486. return $this->subject;
  487. }
  488. /**
  489. * @param string $subject
  490. * @return Staple_Mail
  491. */
  492. public function setSubject($subject)
  493. {
  494. $this->subject = $subject;
  495. return $this;
  496. }
  497. /**
  498. * @return the $body
  499. */
  500. public function getBody()
  501. {
  502. if(isset($this->template))
  503. {
  504. //Check that the template file exists.
  505. if(file_exists($this->template))
  506. {
  507. $templateFile = file_get_contents($this->template);
  508. $bodyStr = self::EMAIL_BODY_FIELD;
  509. if(strpos($templateFile,$bodyStr) !== false)
  510. {
  511. return str_replace($bodyStr, $this->body, $templateFile);
  512. }
  513. else
  514. {
  515. throw new Exception('Invalid Template File:'.$templateFile, Staple_Error::EMAIL_ERROR);
  516. }
  517. }
  518. else
  519. {
  520. //Disregard a missing template
  521. return $this->body;
  522. }
  523. }
  524. else
  525. {
  526. return $this->body;
  527. }
  528. }
  529. /**
  530. * @param string $body
  531. * @return Staple_Mail
  532. */
  533. public function setBody($body)
  534. {
  535. $this->body = $body;
  536. return $this;
  537. }
  538. /**
  539. * @return the $bcc
  540. */
  541. public function getBcc()
  542. {
  543. return $this->bcc;
  544. }
  545. /**
  546. * @param array $bcc
  547. * @return Staple_Mail
  548. */
  549. public function setBcc(array $bcc)
  550. {
  551. foreach($bcc as $email)
  552. {
  553. if($this->checkEmailFormat($email))
  554. {
  555. array_push($this->bcc, $email);
  556. }
  557. }
  558. return $this;
  559. }
  560. /**
  561. * Sets the SMTP server to connect to.
  562. * @param unknown_type $smtp
  563. * @return Staple_Mail
  564. */
  565. public function setServer($smtp)
  566. {
  567. ini_set('SMTP', $smtp);
  568. return $this;
  569. }
  570. /**
  571. * @return the $lastEmailStatus
  572. */
  573. public function getLastEmailStatus()
  574. {
  575. return $this->lastEmailStatus;
  576. }
  577. /**
  578. * @param field_type $lastEmailStatus
  579. */
  580. public function setLastEmailStatus($lastEmailStatus)
  581. {
  582. $this->lastEmailStatus = $lastEmailStatus;
  583. return $this;
  584. }
  585. /**
  586. * @return the $template
  587. */
  588. public function getTemplate()
  589. {
  590. return $this->template;
  591. }
  592. /**
  593. * @param string $template
  594. */
  595. public function setTemplate($template)
  596. {
  597. if(file_exists($template))
  598. {
  599. $this->template = $template;
  600. return $this;
  601. }
  602. else
  603. {
  604. return false;
  605. }
  606. }
  607. /**
  608. * Find out if HTML is enabled for this email.
  609. * @return the $html
  610. */
  611. public function isHtmlEnabled()
  612. {
  613. return $this->html;
  614. }
  615. /**
  616. * Turn HTML on or off for current message.
  617. * @param boolean $html
  618. * @return Staple_Mail
  619. */
  620. public function sendAsHtml($html)
  621. {
  622. $this->html = (bool)$html;
  623. return $this;
  624. }
  625. protected function processCallbacks()
  626. {
  627. foreach($this->callbacks as $func)
  628. {
  629. call_user_func($func,$this);
  630. }
  631. }
  632. public function addSendCallback($callback)
  633. {
  634. $this->callbacks[] = $callback;
  635. }
  636. /**
  637. * Checks that the required fields are completed in the object before attempting to send.
  638. * @return boolean
  639. */
  640. protected function checkMailRequiredFields()
  641. {
  642. $errors = 0;
  643. if(count($this->to) < 1)
  644. {
  645. $errors++;
  646. }
  647. if(!isset($this->from))
  648. {
  649. $errors++;
  650. }
  651. if(!isset($this->subject))
  652. {
  653. $errors++;
  654. }
  655. if(!isset($this->body))
  656. {
  657. $errors++;
  658. }
  659. if($errors < 1)
  660. {
  661. return true;
  662. }
  663. else
  664. {
  665. return false;
  666. }
  667. }
  668. /**
  669. * Checks for a valid email address format.
  670. * @param string $email
  671. * @return boolean
  672. */
  673. public static function checkEmailFormat($email)
  674. {
  675. if(preg_match('/\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+/',$email) == 1)
  676. {
  677. return true;
  678. }
  679. return false;
  680. }
  681. /**
  682. *
  683. * Checks the configuration file that all the keys are available.
  684. * @param array $config
  685. * @return boolean
  686. */
  687. protected function checkConfig($config)
  688. {
  689. $keys = array('html','from','bcc','server');
  690. foreach($keys as $value)
  691. {
  692. if(!array_key_exists($value, $config))
  693. {
  694. return false;
  695. }
  696. }
  697. return true;
  698. }
  699. }
  700. ?>