Sendmail.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Transport
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Mail_Transport_Abstract
  24. */
  25. require_once 'Zend/Mail/Transport/Abstract.php';
  26. /**
  27. * Class for sending eMails via the PHP internal mail() function
  28. *
  29. * @category Zend
  30. * @package Zend_Mail
  31. * @subpackage Transport
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Mail_Transport_Sendmail extends Zend_Mail_Transport_Abstract
  36. {
  37. /**
  38. * Subject
  39. * @var string
  40. * @access public
  41. */
  42. public $subject = null;
  43. /**
  44. * Config options for sendmail parameters
  45. *
  46. * @var string
  47. */
  48. public $parameters;
  49. /**
  50. * EOL character string
  51. * @var string
  52. * @access public
  53. */
  54. public $EOL = PHP_EOL;
  55. /**
  56. * error information
  57. * @var string
  58. */
  59. protected $_errstr;
  60. /**
  61. * Constructor.
  62. *
  63. * @param string $parameters OPTIONAL (Default: null)
  64. * @return void
  65. */
  66. public function __construct($parameters = null)
  67. {
  68. $this->parameters = $parameters;
  69. }
  70. /**
  71. * Send mail using PHP native mail()
  72. *
  73. * @access public
  74. * @return void
  75. * @throws Zend_Mail_Transport_Exception on mail() failure
  76. */
  77. public function _sendMail()
  78. {
  79. set_error_handler(array($this, '_handleMailErrors'));
  80. if ($this->parameters === null) {
  81. $result = mail(
  82. $this->recipients,
  83. $this->_mail->getSubject(),
  84. $this->body,
  85. $this->header);
  86. } else {
  87. $result = mail(
  88. $this->recipients,
  89. $this->_mail->getSubject(),
  90. $this->body,
  91. $this->header,
  92. $this->parameters);
  93. }
  94. restore_error_handler();
  95. if ($this->_errstr !== null || !$result) {
  96. /**
  97. * @see Zend_Mail_Transport_Exception
  98. */
  99. require_once 'Zend/Mail/Transport/Exception.php';
  100. throw new Zend_Mail_Transport_Exception('Unable to send mail. ' . $this->_errstr);
  101. }
  102. }
  103. /**
  104. * Format and fix headers
  105. *
  106. * mail() uses its $to and $subject arguments to set the To: and Subject:
  107. * headers, respectively. This method strips those out as a sanity check to
  108. * prevent duplicate header entries.
  109. *
  110. * @access protected
  111. * @param array $headers
  112. * @return void
  113. * @throws Zend_Mail_Transport_Exception
  114. */
  115. protected function _prepareHeaders($headers)
  116. {
  117. if (!$this->_mail) {
  118. /**
  119. * @see Zend_Mail_Transport_Exception
  120. */
  121. require_once 'Zend/Mail/Transport/Exception.php';
  122. throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
  123. }
  124. // mail() uses its $to parameter to set the To: header, and the $subject
  125. // parameter to set the Subject: header. We need to strip them out.
  126. if (0 === strpos(PHP_OS, 'WIN')) {
  127. // If the current recipients list is empty, throw an error
  128. if (empty($this->recipients)) {
  129. /**
  130. * @see Zend_Mail_Transport_Exception
  131. */
  132. require_once 'Zend/Mail/Transport/Exception.php';
  133. throw new Zend_Mail_Transport_Exception('Missing To addresses');
  134. }
  135. } else {
  136. // All others, simply grab the recipients and unset the To: header
  137. if (!isset($headers['To'])) {
  138. /**
  139. * @see Zend_Mail_Transport_Exception
  140. */
  141. require_once 'Zend/Mail/Transport/Exception.php';
  142. throw new Zend_Mail_Transport_Exception('Missing To header');
  143. }
  144. unset($headers['To']['append']);
  145. $this->recipients = implode(',', $headers['To']);
  146. }
  147. // Remove recipient header
  148. unset($headers['To']);
  149. // Remove subject header, if present
  150. if (isset($headers['Subject'])) {
  151. unset($headers['Subject']);
  152. }
  153. // Prepare headers
  154. parent::_prepareHeaders($headers);
  155. // Fix issue with empty blank line ontop when using Sendmail Trnasport
  156. $this->header = rtrim($this->header);
  157. }
  158. /**
  159. * Temporary error handler for PHP native mail().
  160. *
  161. * @param int $errno
  162. * @param string $errstr
  163. * @param string $errfile
  164. * @param string $errline
  165. * @param array $errcontext
  166. * @return true
  167. */
  168. public function _handleMailErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
  169. {
  170. $this->_errstr = $errstr;
  171. return true;
  172. }
  173. }