Sendmail.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ C9l6ZbI/sNmayfjnE/QUHQOM/TiDKqY8bLUYW9Xh3D8=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.2
  11. */
  12. /**
  13. * mailer transport class - using sendmail program
  14. */
  15. namespace Cube\Mail\Transport;
  16. class Sendmail extends AbstractTransport
  17. {
  18. /**
  19. *
  20. * path of the sendmail program
  21. *
  22. * @var string
  23. */
  24. protected $_path = '/usr/sbin/sendmail';
  25. /**
  26. *
  27. * class constructor
  28. *
  29. * @param array $options
  30. */
  31. public function __construct(array $options = null)
  32. {
  33. parent::__construct($options);
  34. }
  35. /**
  36. *
  37. * get the path of the sendmail application
  38. *
  39. * @return string
  40. */
  41. public function getPath()
  42. {
  43. return $this->_path;
  44. }
  45. /**
  46. *
  47. * set the path of the sendmail application
  48. *
  49. * @param string $path
  50. * @return \Cube\Mail\Transport\Sendmail
  51. */
  52. public function setPath($path)
  53. {
  54. $this->_path = $path;
  55. return $this;
  56. }
  57. /**
  58. *
  59. * send mail method
  60. *
  61. * @throws \RuntimeException
  62. * @return bool
  63. */
  64. public function send()
  65. {
  66. $result = false;
  67. $mail = $this->getMail();
  68. $mailHeader = $mail->createHeader();
  69. $mailBody = $mail->getBody();
  70. $from = $mail->getFrom();
  71. $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->_path), escapeshellarg($from['address']));
  72. foreach ($mail->getTo() as $to) {
  73. if (!@$mail = popen($sendmail, 'w')) {
  74. throw new \RuntimeException(sprintf(
  75. "Could not execute sendmail program, path given: '%s'.", $this->_path));
  76. }
  77. fputs($mail, "To: " . $to['address'] . "\n");
  78. fputs($mail, $mailHeader . "\n");
  79. fputs($mail, $mailBody . "\n");
  80. $result = pclose($mail);
  81. }
  82. $result = ($result == 0) ? true : false;
  83. return $result;
  84. }
  85. }