PopBeforeSmtpPlugin.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Makes sure a connection to a POP3 host has been established prior to connecting to SMTP.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_Plugins_PopBeforeSmtpPlugin implements Swift_Events_TransportChangeListener, Swift_Plugins_Pop_Pop3Connection
  15. {
  16. /** A delegate connection to use (mostly a test hook) */
  17. private $_connection;
  18. /** Hostname of the POP3 server */
  19. private $_host;
  20. /** Port number to connect on */
  21. private $_port;
  22. /** Encryption type to use (if any) */
  23. private $_crypto;
  24. /** Username to use (if any) */
  25. private $_username;
  26. /** Password to use (if any) */
  27. private $_password;
  28. /** Established connection via TCP socket */
  29. private $_socket;
  30. /** Connect timeout in seconds */
  31. private $_timeout = 10;
  32. /** SMTP Transport to bind to */
  33. private $_transport;
  34. /**
  35. * Create a new PopBeforeSmtpPlugin for $host and $port.
  36. *
  37. * @param string $host
  38. * @param int $port
  39. * @param string $crypto as "tls" or "ssl"
  40. */
  41. public function __construct($host, $port = 110, $crypto = null)
  42. {
  43. $this->_host = $host;
  44. $this->_port = $port;
  45. $this->_crypto = $crypto;
  46. }
  47. /**
  48. * Create a new PopBeforeSmtpPlugin for $host and $port.
  49. *
  50. * @param string $host
  51. * @param int $port
  52. * @param string $crypto as "tls" or "ssl"
  53. *
  54. * @return Swift_Plugins_PopBeforeSmtpPlugin
  55. */
  56. public static function newInstance($host, $port = 110, $crypto = null)
  57. {
  58. return new self($host, $port, $crypto);
  59. }
  60. /**
  61. * Set a Pop3Connection to delegate to instead of connecting directly.
  62. *
  63. * @param Swift_Plugins_Pop_Pop3Connection $connection
  64. *
  65. * @return Swift_Plugins_PopBeforeSmtpPlugin
  66. */
  67. public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
  68. {
  69. $this->_connection = $connection;
  70. return $this;
  71. }
  72. /**
  73. * Bind this plugin to a specific SMTP transport instance.
  74. *
  75. * @param Swift_Transport
  76. */
  77. public function bindSmtp(Swift_Transport $smtp)
  78. {
  79. $this->_transport = $smtp;
  80. }
  81. /**
  82. * Set the connection timeout in seconds (default 10).
  83. *
  84. * @param int $timeout
  85. *
  86. * @return Swift_Plugins_PopBeforeSmtpPlugin
  87. */
  88. public function setTimeout($timeout)
  89. {
  90. $this->_timeout = (int) $timeout;
  91. return $this;
  92. }
  93. /**
  94. * Set the username to use when connecting (if needed).
  95. *
  96. * @param string $username
  97. *
  98. * @return Swift_Plugins_PopBeforeSmtpPlugin
  99. */
  100. public function setUsername($username)
  101. {
  102. $this->_username = $username;
  103. return $this;
  104. }
  105. /**
  106. * Set the password to use when connecting (if needed).
  107. *
  108. * @param string $password
  109. *
  110. * @return Swift_Plugins_PopBeforeSmtpPlugin
  111. */
  112. public function setPassword($password)
  113. {
  114. $this->_password = $password;
  115. return $this;
  116. }
  117. /**
  118. * Connect to the POP3 host and authenticate.
  119. *
  120. * @throws Swift_Plugins_Pop_Pop3Exception if connection fails
  121. */
  122. public function connect()
  123. {
  124. if (isset($this->_connection)) {
  125. $this->_connection->connect();
  126. } else {
  127. if (!isset($this->_socket)) {
  128. if (!$socket = fsockopen(
  129. $this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout))
  130. {
  131. throw new Swift_Plugins_Pop_Pop3Exception(
  132. sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr)
  133. );
  134. }
  135. $this->_socket = $socket;
  136. if (false === $greeting = fgets($this->_socket)) {
  137. throw new Swift_Plugins_Pop_Pop3Exception(
  138. sprintf('Failed to connect to POP3 host [%s]', trim($greeting))
  139. );
  140. }
  141. $this->_assertOk($greeting);
  142. if ($this->_username) {
  143. $this->_command(sprintf("USER %s\r\n", $this->_username));
  144. $this->_command(sprintf("PASS %s\r\n", $this->_password));
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * Disconnect from the POP3 host.
  151. */
  152. public function disconnect()
  153. {
  154. if (isset($this->_connection)) {
  155. $this->_connection->disconnect();
  156. } else {
  157. $this->_command("QUIT\r\n");
  158. if (!fclose($this->_socket)) {
  159. throw new Swift_Plugins_Pop_Pop3Exception(
  160. sprintf('POP3 host [%s] connection could not be stopped', $this->_host)
  161. );
  162. }
  163. $this->_socket = null;
  164. }
  165. }
  166. /**
  167. * Invoked just before a Transport is started.
  168. *
  169. * @param Swift_Events_TransportChangeEvent $evt
  170. */
  171. public function beforeTransportStarted(Swift_Events_TransportChangeEvent $evt)
  172. {
  173. if (isset($this->_transport)) {
  174. if ($this->_transport !== $evt->getTransport()) {
  175. return;
  176. }
  177. }
  178. $this->connect();
  179. $this->disconnect();
  180. }
  181. /**
  182. * Not used.
  183. */
  184. public function transportStarted(Swift_Events_TransportChangeEvent $evt)
  185. {
  186. }
  187. /**
  188. * Not used.
  189. */
  190. public function beforeTransportStopped(Swift_Events_TransportChangeEvent $evt)
  191. {
  192. }
  193. /**
  194. * Not used.
  195. */
  196. public function transportStopped(Swift_Events_TransportChangeEvent $evt)
  197. {
  198. }
  199. private function _command($command)
  200. {
  201. if (!fwrite($this->_socket, $command)) {
  202. throw new Swift_Plugins_Pop_Pop3Exception(
  203. sprintf('Failed to write command [%s] to POP3 host', trim($command))
  204. );
  205. }
  206. if (false === $response = fgets($this->_socket)) {
  207. throw new Swift_Plugins_Pop_Pop3Exception(
  208. sprintf('Failed to read from POP3 host after command [%s]', trim($command))
  209. );
  210. }
  211. $this->_assertOk($response);
  212. return $response;
  213. }
  214. private function _assertOk($response)
  215. {
  216. if (substr($response, 0, 3) != '+OK') {
  217. throw new Swift_Plugins_Pop_Pop3Exception(
  218. sprintf('POP3 command failed [%s]', trim($response))
  219. );
  220. }
  221. }
  222. private function _getHostString()
  223. {
  224. $host = $this->_host;
  225. switch (strtolower($this->_crypto)) {
  226. case 'ssl':
  227. $host = 'ssl://' . $host;
  228. break;
  229. case 'tls':
  230. $host = 'tls://' . $host;
  231. break;
  232. }
  233. return $host;
  234. }
  235. }