Smtp.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_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-2008 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. public function __construct($host = '127.0.0.1', Array $config = array())
  97. {
  98. if (isset($config['name'])) {
  99. $this->_name = $config['name'];
  100. }
  101. if (isset($config['port'])) {
  102. $this->_port = $config['port'];
  103. }
  104. if (isset($config['auth'])) {
  105. $this->_auth = $config['auth'];
  106. }
  107. $this->_host = $host;
  108. $this->_config = $config;
  109. }
  110. /**
  111. * Class destructor to ensure all open connections are closed
  112. *
  113. * @return void
  114. */
  115. public function __destruct()
  116. {
  117. if ($this->_connection instanceof Zend_Mail_Protocol_Smtp) {
  118. try {
  119. $this->_connection->quit();
  120. } catch (Zend_Mail_Protocol_Exception $e) {
  121. // ignore
  122. }
  123. $this->_connection->disconnect();
  124. }
  125. }
  126. /**
  127. * Sets the connection protocol instance
  128. *
  129. * @param Zend_Mail_Protocol_Abstract $client
  130. *
  131. * @return void
  132. */
  133. public function setConnection(Zend_Mail_Protocol_Abstract $connection)
  134. {
  135. $this->_connection = $connection;
  136. }
  137. /**
  138. * Gets the connection protocol instance
  139. *
  140. * @return Zend_Mail_Protocol|null
  141. */
  142. public function getConnection()
  143. {
  144. return $this->_connection;
  145. }
  146. /**
  147. * Send an email via the SMTP connection protocol
  148. *
  149. * The connection via the protocol adapter is made just-in-time to allow a
  150. * developer to add a custom adapter if required before mail is sent.
  151. *
  152. * @return void
  153. */
  154. public function _sendMail()
  155. {
  156. // If sending multiple messages per session use existing adapter
  157. if (!($this->_connection instanceof Zend_Mail_Protocol_Smtp)) {
  158. // Check if authentication is required and determine required class
  159. $connectionClass = 'Zend_Mail_Protocol_Smtp';
  160. if ($this->_auth) {
  161. $connectionClass .= '_Auth_' . ucwords($this->_auth);
  162. }
  163. if (!class_exists($connectionClass)) {
  164. require_once 'Zend/Loader.php';
  165. Zend_Loader::loadClass($connectionClass);
  166. }
  167. $this->setConnection(new $connectionClass($this->_host, $this->_port, $this->_config));
  168. $this->_connection->connect();
  169. $this->_connection->helo($this->_name);
  170. } else {
  171. // Reset connection to ensure reliable transaction
  172. $this->_connection->rset();
  173. }
  174. // Set mail return path from sender email address
  175. $this->_connection->mail($this->_mail->getReturnPath());
  176. // Set recipient forward paths
  177. foreach ($this->_mail->getRecipients() as $recipient) {
  178. $this->_connection->rcpt($recipient);
  179. }
  180. // Issue DATA command to client
  181. $this->_connection->data($this->header . Zend_Mime::LINEEND . $this->body);
  182. }
  183. /**
  184. * Format and fix headers
  185. *
  186. * Some SMTP servers do not strip BCC headers. Most clients do it themselves as do we.
  187. *
  188. * @access protected
  189. * @param array $headers
  190. * @return void
  191. * @throws Zend_Transport_Exception
  192. */
  193. protected function _prepareHeaders($headers)
  194. {
  195. if (!$this->_mail) {
  196. /**
  197. * @see Zend_Mail_Transport_Exception
  198. */
  199. require_once 'Zend/Mail/Transport/Exception.php';
  200. throw new Zend_Mail_Transport_Exception('_prepareHeaders requires a registered Zend_Mail object');
  201. }
  202. unset($headers['Bcc']);
  203. // Prepare headers
  204. parent::_prepareHeaders($headers);
  205. }
  206. }