/web/concrete/libraries/3rdparty/Zend/Mail/Transport/Smtp.php

https://github.com/shin2/concrete5 · PHP · 243 lines · 78 code · 33 blank · 132 comment · 9 complexity · 31eb38b7827b1d52fa47d3981d137d86 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: Smtp.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Mime
  24. */
  25. require_once 'Zend/Mime.php';
  26. /**
  27. * @see Zend_Mail_Protocol_Smtp
  28. */
  29. require_once 'Zend/Mail/Protocol/Smtp.php';
  30. /**
  31. * @see Zend_Mail_Transport_Abstract
  32. */
  33. require_once 'Zend/Mail/Transport/Abstract.php';
  34. /**
  35. * SMTP connection object
  36. *
  37. * Loads an instance of Zend_Mail_Protocol_Smtp and forwards smtp transactions
  38. *
  39. * @category Zend
  40. * @package Zend_Mail
  41. * @subpackage Transport
  42. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. */
  45. class Zend_Mail_Transport_Smtp extends Zend_Mail_Transport_Abstract
  46. {
  47. /**
  48. * EOL character string used by transport
  49. * @var string
  50. * @access public
  51. */
  52. public $EOL = "\n";
  53. /**
  54. * Remote smtp hostname or i.p.
  55. *
  56. * @var string
  57. */
  58. protected $_host;
  59. /**
  60. * Port number
  61. *
  62. * @var integer|null
  63. */
  64. protected $_port;
  65. /**
  66. * Local client hostname or i.p.
  67. *
  68. * @var string
  69. */
  70. protected $_name = 'localhost';
  71. /**
  72. * Authentication type OPTIONAL
  73. *
  74. * @var string
  75. */
  76. protected $_auth;
  77. /**
  78. * Config options for authentication
  79. *
  80. * @var array
  81. */
  82. protected $_config;
  83. /**
  84. * Instance of Zend_Mail_Protocol_Smtp
  85. *
  86. * @var Zend_Mail_Protocol_Smtp
  87. */
  88. protected $_connection;
  89. /**
  90. * Constructor.
  91. *
  92. * @param string $host OPTIONAL (Default: 127.0.0.1)
  93. * @param array|null $config OPTIONAL (Default: null)
  94. * @return void
  95. *
  96. * @todo Someone please make this compatible
  97. * with the SendMail transport class.
  98. */
  99. public function __construct($host = '127.0.0.1', Array $config = array())
  100. {
  101. if (isset($config['name'])) {
  102. $this->_name = $config['name'];
  103. }
  104. if (isset($config['port'])) {
  105. $this->_port = $config['port'];
  106. }
  107. if (isset($config['auth'])) {
  108. $this->_auth = $config['auth'];
  109. }
  110. $this->_host = $host;
  111. $this->_config = $config;
  112. }
  113. /**
  114. * Class destructor to ensure all open connections are closed
  115. *
  116. * @return void
  117. */
  118. public function __destruct()
  119. {
  120. if ($this->_connection instanceof Zend_Mail_Protocol_Smtp) {
  121. try {
  122. $this->_connection->quit();
  123. } catch (Zend_Mail_Protocol_Exception $e) {
  124. // ignore
  125. }
  126. $this->_connection->disconnect();
  127. }
  128. }
  129. /**
  130. * Sets the connection protocol instance
  131. *
  132. * @param Zend_Mail_Protocol_Abstract $client
  133. *
  134. * @return void
  135. */
  136. public function setConnection(Zend_Mail_Protocol_Abstract $connection)
  137. {
  138. $this->_connection = $connection;
  139. }
  140. /**
  141. * Gets the connection protocol instance
  142. *
  143. * @return Zend_Mail_Protocol|null
  144. */
  145. public function getConnection()
  146. {
  147. return $this->_connection;
  148. }
  149. /**
  150. * Send an email via the SMTP connection protocol
  151. *
  152. * The connection via the protocol adapter is made just-in-time to allow a
  153. * developer to add a custom adapter if required before mail is sent.
  154. *
  155. * @return void
  156. * @todo Rename this to sendMail, it's a public method...
  157. */
  158. public function _sendMail()
  159. {
  160. // If sending multiple messages per session use existing adapter
  161. if (!($this->_connection instanceof Zend_Mail_Protocol_Smtp)) {
  162. // Check if authentication is required and determine required class
  163. $connectionClass = 'Zend_Mail_Protocol_Smtp';
  164. if ($this->_auth) {
  165. $connectionClass .= '_Auth_' . ucwords($this->_auth);
  166. }
  167. if (!class_exists($connectionClass)) {
  168. require_once 'Zend/Loader.php';
  169. Zend_Loader::loadClass($connectionClass);
  170. }
  171. $this->setConnection(new $connectionClass($this->_host, $this->_port, $this->_config));
  172. $this->_connection->connect();
  173. $this->_connection->helo($this->_name);
  174. } else {
  175. // Reset connection to ensure reliable transaction
  176. $this->_connection->rset();
  177. }
  178. // Set sender email address
  179. $this->_connection->mail($this->_mail->getReturnPath());
  180. // Set recipient forward paths
  181. foreach ($this->_mail->getRecipients() as $recipient) {
  182. $this->_connection->rcpt($recipient);
  183. }
  184. // Issue DATA command to client
  185. $this->_connection->data($this->header . Zend_Mime::LINEEND . $this->body);
  186. }
  187. /**
  188. * Format and fix headers
  189. *
  190. * Some SMTP servers do not strip BCC headers. Most clients do it themselves as do we.
  191. *
  192. * @access protected
  193. * @param array $headers
  194. * @return void
  195. * @throws Zend_Transport_Exception
  196. */
  197. protected function _prepareHeaders($headers)
  198. {
  199. if (!$this->_mail) {
  200. /**
  201. * @see Zend_Mail_Transport_Exception
  202. */
  203. require_once 'Zend/Mail/Transport/Exception.php';
  204. throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
  205. }
  206. unset($headers['Bcc']);
  207. // Prepare headers
  208. parent::_prepareHeaders($headers);
  209. }
  210. }