Sendmail.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2008 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-2008 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. * Constructor.
  57. *
  58. * @param string $parameters OPTIONAL (Default: null)
  59. * @return void
  60. */
  61. public function __construct($parameters = null)
  62. {
  63. $this->parameters = $parameters;
  64. }
  65. /**
  66. * Send mail using PHP native mail()
  67. *
  68. * @access public
  69. * @return void
  70. * @throws Zend_Mail_Transport_Exception on mail() failure
  71. */
  72. public function _sendMail()
  73. {
  74. if ($this->parameters === null) {
  75. $result = mail(
  76. $this->recipients,
  77. $this->_mail->getSubject(),
  78. $this->body,
  79. $this->header);
  80. } else {
  81. $result = mail(
  82. $this->recipients,
  83. $this->_mail->getSubject(),
  84. $this->body,
  85. $this->header,
  86. $this->parameters);
  87. }
  88. if (!$result) {
  89. /**
  90. * @see Zend_Mail_Transport_Exception
  91. */
  92. require_once 'Zend/Mail/Transport/Exception.php';
  93. throw new Zend_Mail_Transport_Exception('Unable to send mail');
  94. }
  95. }
  96. /**
  97. * Format and fix headers
  98. *
  99. * mail() uses its $to and $subject arguments to set the To: and Subject:
  100. * headers, respectively. This method strips those out as a sanity check to
  101. * prevent duplicate header entries.
  102. *
  103. * @access protected
  104. * @param array $headers
  105. * @return void
  106. * @throws Zend_Mail_Transport_Exception
  107. */
  108. protected function _prepareHeaders($headers)
  109. {
  110. if (!$this->_mail) {
  111. /**
  112. * @see Zend_Mail_Transport_Exception
  113. */
  114. require_once 'Zend/Mail/Transport/Exception.php';
  115. throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
  116. }
  117. // mail() uses its $to parameter to set the To: header, and the $subject
  118. // parameter to set the Subject: header. We need to strip them out.
  119. if (0 === strpos(PHP_OS, 'WIN')) {
  120. // If the current recipients list is empty, throw an error
  121. if (empty($this->recipients)) {
  122. /**
  123. * @see Zend_Mail_Transport_Exception
  124. */
  125. require_once 'Zend/Mail/Transport/Exception.php';
  126. throw new Zend_Mail_Transport_Exception('Missing To addresses');
  127. }
  128. } else {
  129. // All others, simply grab the recipients and unset the To: header
  130. if (!isset($headers['To'])) {
  131. /**
  132. * @see Zend_Mail_Transport_Exception
  133. */
  134. require_once 'Zend/Mail/Transport/Exception.php';
  135. throw new Zend_Mail_Transport_Exception('Missing To header');
  136. }
  137. unset($headers['To']['append']);
  138. $this->recipients = implode(',', $headers['To']);
  139. }
  140. // Remove recipient header
  141. unset($headers['To']);
  142. // Remove subject header, if present
  143. if (isset($headers['Subject'])) {
  144. unset($headers['Subject']);
  145. }
  146. // Prepare headers
  147. parent::_prepareHeaders($headers);
  148. }
  149. }