PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Mail/Transport/Sendmail.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 220 lines | 81 code | 25 blank | 114 comment | 13 complexity | a5156fcb68f059f9125a4d04268eb003 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Sendmail.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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|array|Zend_Config $parameters OPTIONAL (Default: null)
  64. * @return void
  65. */
  66. public function __construct($parameters = null)
  67. {
  68. if ($parameters instanceof Zend_Config) {
  69. $parameters = $parameters->toArray();
  70. }
  71. if (is_array($parameters)) {
  72. $parameters = implode(' ', $parameters);
  73. }
  74. $this->parameters = $parameters;
  75. }
  76. /**
  77. * Send mail using PHP native mail()
  78. *
  79. * @access public
  80. * @return void
  81. * @throws Zend_Mail_Transport_Exception if parameters is set
  82. * but not a string
  83. * @throws Zend_Mail_Transport_Exception on mail() failure
  84. */
  85. public function _sendMail()
  86. {
  87. if ($this->parameters === null) {
  88. set_error_handler(array($this, '_handleMailErrors'));
  89. $result = mail(
  90. $this->recipients,
  91. $this->_mail->getSubject(),
  92. $this->body,
  93. $this->header);
  94. restore_error_handler();
  95. } else {
  96. if(!is_string($this->parameters)) {
  97. /**
  98. * @see Zend_Mail_Transport_Exception
  99. *
  100. * Exception is thrown here because
  101. * $parameters is a public property
  102. */
  103. require_once 'Zend/Mail/Transport/Exception.php';
  104. throw new Zend_Mail_Transport_Exception(
  105. 'Parameters were set but are not a string'
  106. );
  107. }
  108. set_error_handler(array($this, '_handleMailErrors'));
  109. $result = mail(
  110. $this->recipients,
  111. $this->_mail->getSubject(),
  112. $this->body,
  113. $this->header,
  114. $this->parameters);
  115. restore_error_handler();
  116. }
  117. if ($this->_errstr !== null || !$result) {
  118. /**
  119. * @see Zend_Mail_Transport_Exception
  120. */
  121. require_once 'Zend/Mail/Transport/Exception.php';
  122. throw new Zend_Mail_Transport_Exception('Unable to send mail. ' . $this->_errstr);
  123. }
  124. }
  125. /**
  126. * Format and fix headers
  127. *
  128. * mail() uses its $to and $subject arguments to set the To: and Subject:
  129. * headers, respectively. This method strips those out as a sanity check to
  130. * prevent duplicate header entries.
  131. *
  132. * @access protected
  133. * @param array $headers
  134. * @return void
  135. * @throws Zend_Mail_Transport_Exception
  136. */
  137. protected function _prepareHeaders($headers)
  138. {
  139. if (!$this->_mail) {
  140. /**
  141. * @see Zend_Mail_Transport_Exception
  142. */
  143. require_once 'Zend/Mail/Transport/Exception.php';
  144. throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
  145. }
  146. // mail() uses its $to parameter to set the To: header, and the $subject
  147. // parameter to set the Subject: header. We need to strip them out.
  148. if (0 === strpos(PHP_OS, 'WIN')) {
  149. // If the current recipients list is empty, throw an error
  150. if (empty($this->recipients)) {
  151. /**
  152. * @see Zend_Mail_Transport_Exception
  153. */
  154. require_once 'Zend/Mail/Transport/Exception.php';
  155. throw new Zend_Mail_Transport_Exception('Missing To addresses');
  156. }
  157. } else {
  158. // All others, simply grab the recipients and unset the To: header
  159. if (!isset($headers['To'])) {
  160. /**
  161. * @see Zend_Mail_Transport_Exception
  162. */
  163. require_once 'Zend/Mail/Transport/Exception.php';
  164. throw new Zend_Mail_Transport_Exception('Missing To header');
  165. }
  166. unset($headers['To']['append']);
  167. $this->recipients = implode(',', $headers['To']);
  168. }
  169. // Remove recipient header
  170. unset($headers['To']);
  171. // Remove subject header, if present
  172. if (isset($headers['Subject'])) {
  173. unset($headers['Subject']);
  174. }
  175. // Prepare headers
  176. parent::_prepareHeaders($headers);
  177. // Fix issue with empty blank line ontop when using Sendmail Trnasport
  178. $this->header = rtrim($this->header);
  179. }
  180. /**
  181. * Temporary error handler for PHP native mail().
  182. *
  183. * @param int $errno
  184. * @param string $errstr
  185. * @param string $errfile
  186. * @param string $errline
  187. * @param array $errcontext
  188. * @return true
  189. */
  190. public function _handleMailErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
  191. {
  192. $this->_errstr = $errstr;
  193. return true;
  194. }
  195. }