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.
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: Pop3.php 9099 2008-03-30 19:35:47Z thomas $
25
* @see Zend_Mail_Storage_Abstract
27
require_once 'Zend/Mail/Storage/Abstract.php';
30
* @see Zend_Mail_Protocol_Pop3
32
require_once 'Zend/Mail/Protocol/Pop3.php';
35
* @see Zend_Mail_Message
37
require_once 'Zend/Mail/Message.php';
44
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
45
* @license http://framework.zend.com/license/new-bsd New BSD License
47
class Zend_Mail_Storage_Pop3 extends Zend_Mail_Storage_Abstract
51
* @var null|Zend_Mail_Protocol_Pop3
57
* Count messages all messages in current box
59
* @return int number of messages
60
* @throws Zend_Mail_Storage_Exception
61
* @throws Zend_Mail_Protocol_Exception
63
public function countMessages()
65
$this->_protocol->status($count, $null);
70
* get a list of messages with number and size
72
* @param int $id number of message
73
* @return int|array size of given message of list with all messages as array(num => size)
74
* @throws Zend_Mail_Protocol_Exception
76
public function getSize($id = 0)
78
$id = $id ? $id : null;
79
return $this->_protocol->getList($id);
85
* @param int $id number of message
86
* @return Zend_Mail_Message
87
* @throws Zend_Mail_Protocol_Exception
89
public function getMessage($id)
92
$message = $this->_protocol->top($id, $bodyLines, true);
94
return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $message,
95
'noToplines' => $bodyLines < 1));
99
* Get raw header of message or part
101
* @param int $id number of message
102
* @param null|array|string $part path to part or null for messsage header
103
* @param int $topLines include this many lines with header (after an empty line)
104
* @return string raw header
105
* @throws Zend_Mail_Protocol_Exception
106
* @throws Zend_Mail_Storage_Exception
108
public function getRawHeader($id, $part = null, $topLines = 0)
110
if ($part !== null) {
113
* @see Zend_Mail_Storage_Exception
115
require_once 'Zend/Mail/Storage/Exception.php';
116
throw new Zend_Mail_Storage_Exception('not implemented');
119
return $this->_protocol->top($id, 0, true);
123
* Get raw content of message or part
125
* @param int $id number of message
126
* @param null|array|string $part path to part or null for messsage content
127
* @return string raw content
128
* @throws Zend_Mail_Protocol_Exception
129
* @throws Zend_Mail_Storage_Exception
131
public function getRawContent($id, $part = null)
133
if ($part !== null) {
136
* @see Zend_Mail_Storage_Exception
138
require_once 'Zend/Mail/Storage/Exception.php';
139
throw new Zend_Mail_Storage_Exception('not implemented');
142
$content = $this->_protocol->retrieve($id);
143
// TODO: find a way to avoid decoding the headers
144
Zend_Mime_Decode::splitMessage($content, $null, $body);
149
* create instance with parameters
150
* Supported paramters are
151
* - host hostname or ip address of POP3 server
153
* - password password for user 'username' [optional, default = '']
154
* - port port for POP3 server [optional, default = 110]
155
* - ssl 'SSL' or 'TLS' for secure sockets
157
* @param $params array mail reader specific parameters
158
* @throws Zend_Mail_Storage_Exception
159
* @throws Zend_Mail_Protocol_Exception
161
public function __construct($params)
163
if (is_array($params)) {
164
$params = (object)$params;
167
$this->_has['fetchPart'] = false;
168
$this->_has['top'] = null;
169
$this->_has['uniqueid'] = null;
171
if ($params instanceof Zend_Mail_Protocol_Pop3) {
172
$this->_protocol = $params;
176
if (!isset($params->user)) {
178
* @see Zend_Mail_Storage_Exception
180
require_once 'Zend/Mail/Storage/Exception.php';
181
throw new Zend_Mail_Storage_Exception('need at least user in params');
184
$host = isset($params->host) ? $params->host : 'localhost';
185
$password = isset($params->password) ? $params->password : '';
186
$port = isset($params->port) ? $params->port : null;
187
$ssl = isset($params->ssl) ? $params->ssl : false;
189
$this->_protocol = new Zend_Mail_Protocol_Pop3();
190
$this->_protocol->connect($host, $port, $ssl);
191
$this->_protocol->login($params->user, $password);
195
* Close resource for mail lib. If you need to control, when the resource
196
* is closed. Otherwise the destructor would call this.
200
public function close()
202
$this->_protocol->logout();
206
* Keep the server busy.
209
* @throws Zend_Mail_Protocol_Exception
211
public function noop()
213
return $this->_protocol->noop();
217
* Remove a message from server. If you're doing that from a web enviroment
218
* you should be careful and use a uniqueid as parameter if possible to
219
* identify the message.
221
* @param int $id number of message
223
* @throws Zend_Mail_Protocol_Exception
225
public function removeMessage($id)
227
$this->_protocol->delete($id);
231
* get unique id for one or all messages
233
* if storage does not support unique ids it's the same as the message number
235
* @param int|null $id message number
236
* @return array|string message number for given message or all messages as array
237
* @throws Zend_Mail_Storage_Exception
239
public function getUniqueId($id = null)
241
if (!$this->hasUniqueid) {
245
$count = $this->countMessages();
249
$range = range(1, $count);
250
return array_combine($range, $range);
253
return $this->_protocol->uniqueid($id);
257
* get a message number from a unique id
259
* I.e. if you have a webmailer that supports deleting messages you should use unique ids
260
* as parameter and use this method to translate it to message number right before calling removeMessage()
262
* @param string $id unique id
263
* @return int message number
264
* @throws Zend_Mail_Storage_Exception
266
public function getNumberByUniqueId($id)
268
if (!$this->hasUniqueid) {
272
$ids = $this->getUniqueId();
273
foreach ($ids as $k => $v) {
280
* @see Zend_Mail_Storage_Exception
282
require_once 'Zend/Mail/Storage/Exception.php';
283
throw new Zend_Mail_Storage_Exception('unique id not found');
287
* Special handling for hasTop and hasUniqueid. The headers of the first message is
288
* retrieved if Top wasn't needed/tried yet.
290
* @see Zend_Mail_Storage_Abstract:__get()
293
* @throws Zend_Mail_Storage_Exception
295
public function __get($var)
297
$result = parent::__get($var);
298
if ($result !== null) {
302
if (strtolower($var) == 'hastop') {
303
if ($this->_protocol->hasTop === null) {
304
// need to make a real call, because not all server are honest in their capas
306
$this->_protocol->top(1, 0, false);
307
} catch(Zend_Mail_Exception $e) {
311
$this->_has['top'] = $this->_protocol->hasTop;
312
return $this->_protocol->hasTop;
315
if (strtolower($var) == 'hasuniqueid') {
318
$id = $this->_protocol->uniqueid(1);
319
} catch(Zend_Mail_Exception $e) {
322
$this->_has['uniqueid'] = $id ? true : false;
323
return $this->_has['uniqueid'];