3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mail\Protocol
;
12 use Zend\Stdlib\ErrorHandler
;
17 * Default timeout in seconds for initiating session
19 const TIMEOUT_CONNECTION
= 30;
22 * saves if server supports top
25 public $hasTop = null;
34 * greeting timestamp for apop
42 * @param string $host hostname or IP address of POP3 server, if given connect() is called
43 * @param int|null $port port of POP3 server, null for default (110 or 995 for ssl)
44 * @param bool|string $ssl use ssl? 'SSL', 'TLS' or false
46 public function __construct($host = '', $port = null, $ssl = false)
49 $this->connect($host, $port, $ssl);
56 public function __destruct()
62 * Open connection to POP3 server
64 * @param string $host hostname or IP address of POP3 server
65 * @param int|null $port of POP3 server, default is 110 (995 for ssl)
66 * @param string|bool $ssl use 'SSL', 'TLS' or false
67 * @throws Exception\RuntimeException
68 * @return string welcome message
70 public function connect($host, $port = null, $ssl = false)
75 $ssl = strtolower($ssl);
80 $host = 'ssl://' . $host;
87 // break intentionally omitted
94 ErrorHandler
::start();
95 $this->socket
= fsockopen($host, $port, $errno, $errstr, self
::TIMEOUT_CONNECTION
);
96 $error = ErrorHandler
::stop();
98 throw new Exception\
RuntimeException(sprintf(
99 'cannot connect to host %s',
100 ($error ?
sprintf('; error = %s (errno = %d )', $error->getMessage(), $error->getCode()) : '')
104 $welcome = $this->readResponse();
106 strtok($welcome, '<');
107 $this->timestamp
= strtok('>');
108 if (!strpos($this->timestamp
, '@')) {
109 $this->timestamp
= null;
111 $this->timestamp
= '<' . $this->timestamp
. '>';
115 $this->request('STLS');
116 $result = stream_socket_enable_crypto($this->socket
, true, STREAM_CRYPTO_METHOD_TLS_CLIENT
);
118 throw new Exception\
RuntimeException('cannot enable TLS');
128 * @param string $request your request without newline
129 * @throws Exception\RuntimeException
131 public function sendRequest($request)
133 ErrorHandler
::start();
134 $result = fputs($this->socket
, $request . "\r\n");
135 $error = ErrorHandler
::stop();
137 throw new Exception\
RuntimeException('send failed - connection closed?', 0, $error);
144 * @param bool $multiline response has multiple lines and should be read until "<nl>.<nl>"
145 * @throws Exception\RuntimeException
146 * @return string response
148 public function readResponse($multiline = false)
150 ErrorHandler
::start();
151 $result = fgets($this->socket
);
152 $error = ErrorHandler
::stop();
153 if (!is_string($result)) {
154 throw new Exception\
RuntimeException('read failed - connection closed?', 0, $error);
157 $result = trim($result);
158 if (strpos($result, ' ')) {
159 list($status, $message) = explode(' ', $result, 2);
165 if ($status != '+OK') {
166 throw new Exception\
RuntimeException('last request failed');
171 $line = fgets($this->socket
);
172 while ($line && rtrim($line, "\r\n") != '.') {
173 if ($line[0] == '.') {
174 $line = substr($line, 1);
177 $line = fgets($this->socket
);
185 * Send request and get response
188 * @see readResponse()
189 * @param string $request request
190 * @param bool $multiline multiline response?
191 * @return string result from readResponse()
193 public function request($request, $multiline = false)
195 $this->sendRequest($request);
196 return $this->readResponse($multiline);
200 * End communication with POP3 server (also closes socket)
202 public function logout()
206 $this->request('QUIT');
207 } catch (Exception\ExceptionInterface
$e) {
208 // ignore error - we're closing the socket anyway
211 fclose($this->socket
);
212 $this->socket
= null;
218 * Get capabilities from POP3 server
220 * @return array list of capabilities
222 public function capa()
224 $result = $this->request('CAPA', true);
225 return explode("\n", $result);
230 * Login to POP3 server. Can use APOP
232 * @param string $user username
233 * @param string $password password
234 * @param bool $tryApop should APOP be tried?
236 public function login($user, $password, $tryApop = true)
238 if ($tryApop && $this->timestamp
) {
240 $this->request("APOP $user " . md5($this->timestamp
. $password));
242 } catch (Exception\ExceptionInterface
$e) {
247 $this->request("USER $user");
248 $this->request("PASS $password");
253 * Make STAT call for message count and size sum
255 * @param int $messages out parameter with count of messages
256 * @param int $octets out parameter with size in octets of messages
258 public function status(&$messages, &$octets)
262 $result = $this->request('STAT');
264 list($messages, $octets) = explode(' ', $result);
269 * Make LIST call for size of message(s)
271 * @param int|null $msgno number of message, null for all
272 * @return int|array size of given message or list with array(num => size)
274 public function getList($msgno = null)
276 if ($msgno !== null) {
277 $result = $this->request("LIST $msgno");
279 list(, $result) = explode(' ', $result);
280 return (int) $result;
283 $result = $this->request('LIST', true);
285 $line = strtok($result, "\n");
287 list($no, $size) = explode(' ', trim($line));
288 $messages[(int) $no] = (int) $size;
289 $line = strtok("\n");
297 * Make UIDL call for getting a uniqueid
299 * @param int|null $msgno number of message, null for all
300 * @return string|array uniqueid of message or list with array(num => uniqueid)
302 public function uniqueid($msgno = null)
304 if ($msgno !== null) {
305 $result = $this->request("UIDL $msgno");
307 list(, $result) = explode(' ', $result);
311 $result = $this->request('UIDL', true);
313 $result = explode("\n", $result);
315 foreach ($result as $line) {
319 list($no, $id) = explode(' ', trim($line), 2);
320 $messages[(int) $no] = $id;
328 * Make TOP call for getting headers and maybe some body lines
329 * This method also sets hasTop - before it it's not known if top is supported
331 * The fallback makes normal RETR call, which retrieves the whole message. Additional
332 * lines are not removed.
334 * @param int $msgno number of message
335 * @param int $lines number of wanted body lines (empty line is inserted after header lines)
336 * @param bool $fallback fallback with full retrieve if top is not supported
337 * @throws Exception\RuntimeException
338 * @throws Exception\ExceptionInterface
339 * @return string message headers with wanted body lines
341 public function top($msgno, $lines = 0, $fallback = false)
343 if ($this->hasTop
=== false) {
345 return $this->retrieve($msgno);
347 throw new Exception\
RuntimeException('top not supported and no fallback wanted');
350 $this->hasTop
= true;
352 $lines = (!$lines ||
$lines < 1) ?
0 : (int) $lines;
355 $result = $this->request("TOP $msgno $lines", true);
356 } catch (Exception\ExceptionInterface
$e) {
357 $this->hasTop
= false;
359 $result = $this->retrieve($msgno);
369 * Make a RETR call for retrieving a full message with headers and body
371 * @param int $msgno message number
372 * @return string message
374 public function retrieve($msgno)
376 $result = $this->request("RETR $msgno", true);
381 * Make a NOOP call, maybe needed for keeping the server happy
383 public function noop()
385 $this->request('NOOP');
389 * Make a DELE count to remove a message
393 public function delete($msgno)
395 $this->request("DELE $msgno");
399 * Make RSET call, which rollbacks delete requests
401 public function undelete()
403 $this->request('RSET');