Smtp.php 5.8 KB

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