TYPO3  7.6
PopBeforeSmtpPlugin.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
17 {
19  private $_connection;
20 
22  private $_host;
23 
25  private $_port;
26 
28  private $_crypto;
29 
31  private $_username;
32 
34  private $_password;
35 
37  private $_socket;
38 
40  private $_timeout = 10;
41 
43  private $_transport;
44 
52  public function __construct($host, $port = 110, $crypto = null)
53  {
54  $this->_host = $host;
55  $this->_port = $port;
56  $this->_crypto = $crypto;
57  }
58 
68  public static function newInstance($host, $port = 110, $crypto = null)
69  {
70  return new self($host, $port, $crypto);
71  }
72 
80  public function setConnection(Swift_Plugins_Pop_Pop3Connection $connection)
81  {
82  $this->_connection = $connection;
83 
84  return $this;
85  }
86 
92  public function bindSmtp(Swift_Transport $smtp)
93  {
94  $this->_transport = $smtp;
95  }
96 
104  public function setTimeout($timeout)
105  {
106  $this->_timeout = (int) $timeout;
107 
108  return $this;
109  }
110 
118  public function setUsername($username)
119  {
120  $this->_username = $username;
121 
122  return $this;
123  }
124 
132  public function setPassword($password)
133  {
134  $this->_password = $password;
135 
136  return $this;
137  }
138 
144  public function connect()
145  {
146  if (isset($this->_connection)) {
147  $this->_connection->connect();
148  } else {
149  if (!isset($this->_socket)) {
150  if (!$socket = fsockopen(
151  $this->_getHostString(), $this->_port, $errno, $errstr, $this->_timeout)) {
153  sprintf('Failed to connect to POP3 host [%s]: %s', $this->_host, $errstr)
154  );
155  }
156  $this->_socket = $socket;
157 
158  if (false === $greeting = fgets($this->_socket)) {
160  sprintf('Failed to connect to POP3 host [%s]', trim($greeting))
161  );
162  }
163 
164  $this->_assertOk($greeting);
165 
166  if ($this->_username) {
167  $this->_command(sprintf("USER %s\r\n", $this->_username));
168  $this->_command(sprintf("PASS %s\r\n", $this->_password));
169  }
170  }
171  }
172  }
173 
177  public function disconnect()
178  {
179  if (isset($this->_connection)) {
180  $this->_connection->disconnect();
181  } else {
182  $this->_command("QUIT\r\n");
183  if (!fclose($this->_socket)) {
185  sprintf('POP3 host [%s] connection could not be stopped', $this->_host)
186  );
187  }
188  $this->_socket = null;
189  }
190  }
191 
198  {
199  if (isset($this->_transport)) {
200  if ($this->_transport !== $evt->getTransport()) {
201  return;
202  }
203  }
204 
205  $this->connect();
206  $this->disconnect();
207  }
208 
213  {
214  }
215 
220  {
221  }
222 
227  {
228  }
229 
230  private function _command($command)
231  {
232  if (!fwrite($this->_socket, $command)) {
234  sprintf('Failed to write command [%s] to POP3 host', trim($command))
235  );
236  }
237 
238  if (false === $response = fgets($this->_socket)) {
240  sprintf('Failed to read from POP3 host after command [%s]', trim($command))
241  );
242  }
243 
244  $this->_assertOk($response);
245 
246  return $response;
247  }
248 
249  private function _assertOk($response)
250  {
251  if (substr($response, 0, 3) != '+OK') {
253  sprintf('POP3 command failed [%s]', trim($response))
254  );
255  }
256  }
257 
258  private function _getHostString()
259  {
260  $host = $this->_host;
261  switch (strtolower($this->_crypto)) {
262  case 'ssl':
263  $host = 'ssl://'.$host;
264  break;
265 
266  case 'tls':
267  $host = 'tls://'.$host;
268  break;
269  }
270 
271  return $host;
272  }
273 }