mailbox, messageiterator and message class connected
This commit is contained in:
parent
bf6fb8c3ed
commit
f7faa6541d
|
@ -15,261 +15,268 @@ use Clivern\Imap\Core\Exception\ConnectionError;
|
|||
class Connection
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $server;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $server;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $port;
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $port;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $email;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $email;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $password;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $password;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $flag;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $flag;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $folder;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $folder;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $stream;
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $stream;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $timeout_type;
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $timeout_type;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $timeout;
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $timeout;
|
||||
|
||||
/**
|
||||
* Class Constructor
|
||||
*
|
||||
* @param string $server
|
||||
* @param string $port
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @param string $flag
|
||||
* @param string $folder
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($server, $port, $email, $password, $flag = "/ssl", $folder = "INBOX")
|
||||
{
|
||||
$this->server = $server;
|
||||
$this->port = $port;
|
||||
$this->email = $email;
|
||||
$this->password = $password;
|
||||
$this->flag = $flag;
|
||||
$this->folder = $folder;
|
||||
}
|
||||
/**
|
||||
* Class Constructor
|
||||
*
|
||||
* @param string $server
|
||||
* @param string $port
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
* @param string $flag
|
||||
* @param string $folder
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($server, $port, $email, $password, $flag = "/ssl", $folder = "INBOX")
|
||||
{
|
||||
$this->server = $server;
|
||||
$this->port = $port;
|
||||
$this->email = $email;
|
||||
$this->password = $password;
|
||||
$this->flag = $flag;
|
||||
$this->folder = $folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to IMAP Email
|
||||
*
|
||||
* @return Connection
|
||||
* @throws ConnectionError
|
||||
*/
|
||||
public function connect()
|
||||
{
|
||||
try {
|
||||
$this->stream = imap_open("{" . $this->server . ":" . $this->port . $this->flag . "}" . $this->folder, $this->email, $this->password);
|
||||
} catch (\Exception $e) {
|
||||
throw new ConnectionError("Error! Connecting to Imap Email.");
|
||||
}
|
||||
/**
|
||||
* Connect to IMAP Email
|
||||
*
|
||||
* @return Connection
|
||||
* @throws ConnectionError
|
||||
*/
|
||||
public function connect()
|
||||
{
|
||||
try {
|
||||
$this->stream = imap_open("{" . $this->server . ":" . $this->port . $this->flag . "}" . $this->folder, $this->email, $this->password);
|
||||
} catch (\Exception $e) {
|
||||
throw new ConnectionError("Error! Connecting to Imap Email.");
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reconnect($folder)
|
||||
{
|
||||
try {
|
||||
imap_reopen($this->stream, "{" . $this->server . ":" . $this->port . $this->flag . "}" . $folder);
|
||||
} catch (\Exception $e) {
|
||||
throw new ConnectionError("Error! Connecting to Imap Email.");
|
||||
}
|
||||
}
|
||||
public function reconnect($folder = "INBOX")
|
||||
{
|
||||
try {
|
||||
imap_reopen($this->stream, "{" . $this->server . ":" . $this->port . $this->flag . "}" . $folder);
|
||||
} catch (\Exception $e) {
|
||||
throw new ConnectionError("Error! Connecting to Imap Email.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Timeout
|
||||
*
|
||||
* @param string $timeout_type it may be IMAP_OPENTIMEOUT, IMAP_READTIMEOUT, IMAP_WRITETIMEOUT, or IMAP_CLOSETIMEOUT
|
||||
* @param integer $timeout time in seconds or -1
|
||||
* @return void
|
||||
*/
|
||||
public function setTimeout($timeout_type, $timeout)
|
||||
{
|
||||
$this->timeout_type = $timeout_type;
|
||||
$this->timeout = $timeout;
|
||||
public function survive($folder = "INBOX")
|
||||
{
|
||||
if( !$this->ping() || ($this->folder != $folder) ) {
|
||||
$this->reconnect($folder);
|
||||
}
|
||||
}
|
||||
|
||||
return (boolean) imap_timeout($timeout_type, $timeout);
|
||||
}
|
||||
/**
|
||||
* Set Timeout
|
||||
*
|
||||
* @param string $timeout_type it may be IMAP_OPENTIMEOUT, IMAP_READTIMEOUT, IMAP_WRITETIMEOUT, or IMAP_CLOSETIMEOUT
|
||||
* @param integer $timeout time in seconds or -1
|
||||
* @return void
|
||||
*/
|
||||
public function setTimeout($timeout_type, $timeout)
|
||||
{
|
||||
$this->timeout_type = $timeout_type;
|
||||
$this->timeout = $timeout;
|
||||
|
||||
/**
|
||||
* Get Stream
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStream()
|
||||
{
|
||||
return $this->stream;
|
||||
}
|
||||
return (boolean) imap_timeout($timeout_type, $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getServer()
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
/**
|
||||
* Get Stream
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStream()
|
||||
{
|
||||
return $this->stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check Connection
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function checkConnection()
|
||||
{
|
||||
return (!is_null($this->stream) && imap_ping($this->stream));
|
||||
}
|
||||
/**
|
||||
* Get Server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getServer()
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Quota
|
||||
*
|
||||
* @param string $folder
|
||||
* @return array
|
||||
*/
|
||||
public function getQuota($folder = 'INBOX')
|
||||
{
|
||||
$data = imap_get_quotaroot($this->stream, $folder);
|
||||
/**
|
||||
* Check Connection
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function checkConnection()
|
||||
{
|
||||
return (!is_null($this->stream) && imap_ping($this->stream));
|
||||
}
|
||||
|
||||
return [
|
||||
'usage' => (isset($data['usage'])) ? $data['usage'] : false,
|
||||
'limit' => (isset($data['limit'])) ? $data['limit'] : false
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Get Quota
|
||||
*
|
||||
* @param string $folder
|
||||
* @return array
|
||||
*/
|
||||
public function getQuota($folder = 'INBOX')
|
||||
{
|
||||
$data = imap_get_quotaroot($this->stream, $folder);
|
||||
|
||||
/**
|
||||
* Get Status
|
||||
*
|
||||
* @param string $folder
|
||||
* @param string $flag
|
||||
* @return array
|
||||
*/
|
||||
public function getStatus($folder = 'INBOX', $flag = SA_ALL)
|
||||
{
|
||||
$data = imap_status($this->stream, "{" . $this->server . "}" . $folder, $flag);
|
||||
return [
|
||||
'usage' => (isset($data['usage'])) ? $data['usage'] : false,
|
||||
'limit' => (isset($data['limit'])) ? $data['limit'] : false
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'flags' => (isset($data->flags)) ? $data->flags : false,
|
||||
'messages' => (isset($data->messages)) ? $data->messages : false,
|
||||
'recent' => (isset($data->recent)) ? $data->recent : false,
|
||||
'unseen' => (isset($data->unseen)) ? $data->unseen : false,
|
||||
'uidnext' => (isset($data->uidnext)) ? $data->uidnext : false,
|
||||
'uidvalidity' => (isset($data->uidvalidity)) ? $data->uidvalidity : false
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Get Status
|
||||
*
|
||||
* @param string $folder
|
||||
* @param string $flag
|
||||
* @return array
|
||||
*/
|
||||
public function getStatus($folder = 'INBOX', $flag = SA_ALL)
|
||||
{
|
||||
$data = imap_status($this->stream, "{" . $this->server . "}" . $folder, $flag);
|
||||
|
||||
/**
|
||||
* Check MailBox Data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
$data = imap_check($this->stream);
|
||||
return [
|
||||
'flags' => (isset($data->flags)) ? $data->flags : false,
|
||||
'messages' => (isset($data->messages)) ? $data->messages : false,
|
||||
'recent' => (isset($data->recent)) ? $data->recent : false,
|
||||
'unseen' => (isset($data->unseen)) ? $data->unseen : false,
|
||||
'uidnext' => (isset($data->uidnext)) ? $data->uidnext : false,
|
||||
'uidvalidity' => (isset($data->uidvalidity)) ? $data->uidvalidity : false
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'date' => (isset($data->Date)) ? $data->Date : false,
|
||||
'driver' => (isset($data->Driver)) ? $data->Driver : false,
|
||||
'mailbox' => (isset($data->Mailbox)) ? $data->Mailbox : false,
|
||||
'nmsgs' => (isset($data->Nmsgs)) ? $data->Nmsgs : false,
|
||||
'recent' => (isset($data->Recent)) ? $data->Recent : false
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Check MailBox Data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function check()
|
||||
{
|
||||
$data = imap_check($this->stream);
|
||||
|
||||
/**
|
||||
* Ping Connection
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function ping()
|
||||
{
|
||||
return (boolean) imap_ping($this->stream);
|
||||
}
|
||||
return [
|
||||
'date' => (isset($data->Date)) ? $data->Date : false,
|
||||
'driver' => (isset($data->Driver)) ? $data->Driver : false,
|
||||
'mailbox' => (isset($data->Mailbox)) ? $data->Mailbox : false,
|
||||
'nmsgs' => (isset($data->Nmsgs)) ? $data->Nmsgs : false,
|
||||
'recent' => (isset($data->Recent)) ? $data->Recent : false
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Errors
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors()
|
||||
{
|
||||
$errors = imap_errors();
|
||||
/**
|
||||
* Ping Connection
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function ping()
|
||||
{
|
||||
return (boolean) imap_ping($this->stream);
|
||||
}
|
||||
|
||||
return (is_array($errors)) ? $errors : [];
|
||||
}
|
||||
/**
|
||||
* Get Errors
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors()
|
||||
{
|
||||
$errors = imap_errors();
|
||||
|
||||
/**
|
||||
* Get Alerts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAlerts()
|
||||
{
|
||||
$alerts = imap_alerts();
|
||||
return (is_array($errors)) ? $errors : [];
|
||||
}
|
||||
|
||||
return (is_array($alerts)) ? $alerts : [];
|
||||
}
|
||||
/**
|
||||
* Get Alerts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAlerts()
|
||||
{
|
||||
$alerts = imap_alerts();
|
||||
|
||||
/**
|
||||
* Get Last Error
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
$error = imap_last_error();
|
||||
return (is_array($alerts)) ? $alerts : [];
|
||||
}
|
||||
|
||||
return !(empty($error)) ? $error : '';
|
||||
}
|
||||
/**
|
||||
* Get Last Error
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
$error = imap_last_error();
|
||||
|
||||
/**
|
||||
* Disconnect
|
||||
*
|
||||
* @param integer $flag
|
||||
* @return boolean
|
||||
*/
|
||||
public function disconnect($flag = 0)
|
||||
{
|
||||
if( !is_null($this->stream) && imap_ping($this->stream) ){
|
||||
if( imap_close($this->stream, $flag) ){
|
||||
$this->stream = null;
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return !(empty($error)) ? $error : '';
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Disconnect
|
||||
*
|
||||
* @param integer $flag
|
||||
* @return boolean
|
||||
*/
|
||||
public function disconnect($flag = 0)
|
||||
{
|
||||
if( !is_null($this->stream) && imap_ping($this->stream) ){
|
||||
if( imap_close($this->stream, $flag) ){
|
||||
$this->stream = null;
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author clivern <hello@clivern.com>
|
||||
*/
|
||||
|
||||
namespace Clivern\Imap\Core;
|
||||
|
||||
/**
|
||||
* Folder Class
|
||||
*
|
||||
* @package Clivern\Imap\Core
|
||||
*/
|
||||
class Folder
|
||||
{
|
||||
#~
|
||||
}
|
|
@ -14,203 +14,203 @@ use Clivern\Imap\Core\Connection;
|
|||
*/
|
||||
class Message
|
||||
{
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
protected $connection;
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $uid;
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $uid;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $msg_number;
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $msg_number;
|
||||
|
||||
/**
|
||||
* Message Constructor
|
||||
*
|
||||
* @param Connection $connection
|
||||
*/
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
/**
|
||||
* Message Constructor
|
||||
*
|
||||
* @param Connection $connection
|
||||
*/
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Message Number
|
||||
*
|
||||
* @param integer $id
|
||||
* @return Message
|
||||
*/
|
||||
public function setMsgNo($msg_number)
|
||||
{
|
||||
$this->msg_number = $msg_number;
|
||||
/**
|
||||
* Set Message Number
|
||||
*
|
||||
* @param integer $id
|
||||
* @return Message
|
||||
*/
|
||||
public function setMsgNo($msg_number)
|
||||
{
|
||||
$this->msg_number = $msg_number;
|
||||
|
||||
return $this;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Message Number
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getMsgNo()
|
||||
{
|
||||
return $this->msg_number;
|
||||
}
|
||||
/**
|
||||
* Get Message Number
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getMsgNo()
|
||||
{
|
||||
return $this->msg_number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set UID
|
||||
*
|
||||
* @param integer $uid
|
||||
* @return Message
|
||||
*/
|
||||
public function setUid($uid)
|
||||
{
|
||||
$this->uid = $uid;
|
||||
/**
|
||||
* Set UID
|
||||
*
|
||||
* @param integer $uid
|
||||
* @return Message
|
||||
*/
|
||||
public function setUid($uid)
|
||||
{
|
||||
$this->uid = $uid;
|
||||
|
||||
return $this;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get UID
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getUid()
|
||||
{
|
||||
return $this->uid;
|
||||
}
|
||||
/**
|
||||
* Get UID
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getUid()
|
||||
{
|
||||
return $this->uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Config Message Number & UID
|
||||
*
|
||||
* @return Message
|
||||
*/
|
||||
public function config()
|
||||
{
|
||||
if( !this->msg_number && $this->uid ){
|
||||
$this->msg_number = imap_msgno($this->connection->getStream(), $this->uid);
|
||||
}
|
||||
/**
|
||||
* Config Message Number & UID
|
||||
*
|
||||
* @return Message
|
||||
*/
|
||||
public function config()
|
||||
{
|
||||
if( !$this->msg_number && $this->uid ){
|
||||
$this->msg_number = imap_msgno($this->connection->getStream(), $this->uid);
|
||||
}
|
||||
|
||||
if( !this->uid && $this->msg_number ){
|
||||
$this->uid = imap_uid($this->connection->getStream(), $this->msg_number);
|
||||
}
|
||||
if( !$this->uid && $this->msg_number ){
|
||||
$this->uid = imap_uid($this->connection->getStream(), $this->msg_number);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Body
|
||||
*
|
||||
* @param integer $options
|
||||
* @return string
|
||||
*/
|
||||
public function getBody($options = 0)
|
||||
{
|
||||
$body = imap_body($this->connection->getStream(), $this->msg_number, $options);
|
||||
/**
|
||||
* Get Body
|
||||
*
|
||||
* @param integer $options
|
||||
* @return string
|
||||
*/
|
||||
public function getBody($options = 0)
|
||||
{
|
||||
$body = imap_body($this->connection->getStream(), $this->msg_number, $options);
|
||||
|
||||
return $body;
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse Address List
|
||||
*
|
||||
* @param string $address_string
|
||||
* @return array
|
||||
*/
|
||||
public function parseAddressList($address_string, $default_host = "example.com")
|
||||
{
|
||||
$address_array = imap_rfc822_parse_adrlist($address_string, $default_host);
|
||||
$address_list = [];
|
||||
/**
|
||||
* Parse Address List
|
||||
*
|
||||
* @param string $address_string
|
||||
* @return array
|
||||
*/
|
||||
public function parseAddressList($address_string, $default_host = "example.com")
|
||||
{
|
||||
$address_array = imap_rfc822_parse_adrlist($address_string, $default_host);
|
||||
$address_list = [];
|
||||
|
||||
foreach ($address_array as $id => $val) {
|
||||
$address_list[] = [
|
||||
'mailbox' => $val->mailbox,
|
||||
'host' => $val->host,
|
||||
'personal' => $val->personal,
|
||||
'adl' => $val->adl
|
||||
];
|
||||
}
|
||||
foreach ($address_array as $id => $val) {
|
||||
$address_list[] = [
|
||||
'mailbox' => $val->mailbox,
|
||||
'host' => $val->host,
|
||||
'personal' => $val->personal,
|
||||
'adl' => $val->adl
|
||||
];
|
||||
}
|
||||
|
||||
return $address_list;
|
||||
}
|
||||
return $address_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch Body
|
||||
*
|
||||
* @param mixed $section
|
||||
* @param mixed $options
|
||||
* @return string
|
||||
*/
|
||||
public function fetchBody($section = 0, $options = 0)
|
||||
{
|
||||
return imap_fetchbody($this->connection->getStream(), $this->msg_number, $section, $options);
|
||||
}
|
||||
/**
|
||||
* Fetch Body
|
||||
*
|
||||
* @param mixed $section
|
||||
* @param mixed $options
|
||||
* @return string
|
||||
*/
|
||||
public function fetchBody($section = 0, $options = 0)
|
||||
{
|
||||
return imap_fetchbody($this->connection->getStream(), $this->msg_number, $section, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch Overview
|
||||
*
|
||||
* @param boolean $sequence
|
||||
* @param mixed $options
|
||||
* @return array
|
||||
*/
|
||||
public function fetchOverview($sequence = false, $options = 0)
|
||||
{
|
||||
if( $sequence == false ){
|
||||
$overview = imap_fetch_overview($this->connection->getStream(), $this->msg_number, $options);
|
||||
}else{
|
||||
$overview = imap_fetch_overview($this->connection->getStream(), $sequence, $options);
|
||||
}
|
||||
/**
|
||||
* Fetch Overview
|
||||
*
|
||||
* @param boolean $sequence
|
||||
* @param mixed $options
|
||||
* @return array
|
||||
*/
|
||||
public function fetchOverview($sequence = false, $options = 0)
|
||||
{
|
||||
if( $sequence == false ){
|
||||
$overview = imap_fetch_overview($this->connection->getStream(), $this->msg_number, $options);
|
||||
}else{
|
||||
$overview = imap_fetch_overview($this->connection->getStream(), $sequence, $options);
|
||||
}
|
||||
|
||||
$items_overview = [];
|
||||
$items_overview = [];
|
||||
|
||||
foreach ($overview as $key => $item_overview) {
|
||||
$items_overview[] = [
|
||||
'subject' => (isset($item_overview->subject)) ? $item_overview->subject : false,
|
||||
'from' => (isset($item_overview->from)) ? $item_overview->from : false,
|
||||
'to' => (isset($item_overview->to)) ? $item_overview->to : false,
|
||||
'date' => (isset($item_overview->date)) ? $item_overview->date : false,
|
||||
'message_id' => (isset($item_overview->message_id)) ? $item_overview->message_id : false,
|
||||
'size' => (isset($item_overview->size)) ? $item_overview->size : false,
|
||||
'uid' => (isset($item_overview->uid)) ? $item_overview->uid : false,
|
||||
'msgno' => (isset($item_overview->msgno)) ? $item_overview->msgno : false,
|
||||
'recent' => (isset($item_overview->recent)) ? $item_overview->recent : false,
|
||||
'flagged' => (isset($item_overview->flagged)) ? $item_overview->flagged : false,
|
||||
'answered' => (isset($item_overview->answered)) ? $item_overview->answered : false,
|
||||
'deleted' => (isset($item_overview->deleted)) ? $item_overview->deleted : false,
|
||||
'seen' => (isset($item_overview->seen)) ? $item_overview->seen : false,
|
||||
'draft' => (isset($item_overview->draft)) ? $item_overview->draft : false,
|
||||
'udate' => (isset($item_overview->udate)) ? $item_overview->udate : false
|
||||
];
|
||||
}
|
||||
foreach ($overview as $key => $item_overview) {
|
||||
$items_overview[] = [
|
||||
'subject' => (isset($item_overview->subject)) ? $item_overview->subject : false,
|
||||
'from' => (isset($item_overview->from)) ? $item_overview->from : false,
|
||||
'to' => (isset($item_overview->to)) ? $item_overview->to : false,
|
||||
'date' => (isset($item_overview->date)) ? $item_overview->date : false,
|
||||
'message_id' => (isset($item_overview->message_id)) ? $item_overview->message_id : false,
|
||||
'size' => (isset($item_overview->size)) ? $item_overview->size : false,
|
||||
'uid' => (isset($item_overview->uid)) ? $item_overview->uid : false,
|
||||
'msgno' => (isset($item_overview->msgno)) ? $item_overview->msgno : false,
|
||||
'recent' => (isset($item_overview->recent)) ? $item_overview->recent : false,
|
||||
'flagged' => (isset($item_overview->flagged)) ? $item_overview->flagged : false,
|
||||
'answered' => (isset($item_overview->answered)) ? $item_overview->answered : false,
|
||||
'deleted' => (isset($item_overview->deleted)) ? $item_overview->deleted : false,
|
||||
'seen' => (isset($item_overview->seen)) ? $item_overview->seen : false,
|
||||
'draft' => (isset($item_overview->draft)) ? $item_overview->draft : false,
|
||||
'udate' => (isset($item_overview->udate)) ? $item_overview->udate : false
|
||||
];
|
||||
}
|
||||
|
||||
return $items_overview;
|
||||
}
|
||||
return $items_overview;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch Header
|
||||
*
|
||||
* @param mixed $options
|
||||
* @return string
|
||||
*/
|
||||
public function fetchHeader($options = 0)
|
||||
{
|
||||
return imap_fetchheader($this->connection->getStream(), $this->msg_number, $options);
|
||||
}
|
||||
/**
|
||||
* Fetch Header
|
||||
*
|
||||
* @param mixed $options
|
||||
* @return string
|
||||
*/
|
||||
public function fetchHeader($options = 0)
|
||||
{
|
||||
return imap_fetchheader($this->connection->getStream(), $this->msg_number, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch Structure
|
||||
*
|
||||
* @param mixed $options
|
||||
* @return stdClass
|
||||
*/
|
||||
public function fetchStructure($options = 0)
|
||||
{
|
||||
$structure = imap_fetchstructure($connection->getStream(), $this->msg_number, $options);
|
||||
return $structure;
|
||||
}
|
||||
/**
|
||||
* Fetch Structure
|
||||
*
|
||||
* @param mixed $options
|
||||
* @return stdClass
|
||||
*/
|
||||
public function fetchStructure($options = 0)
|
||||
{
|
||||
$structure = imap_fetchstructure($connection->getStream(), $this->msg_number, $options);
|
||||
return $structure;
|
||||
}
|
||||
}
|
|
@ -6,61 +6,38 @@
|
|||
namespace Clivern\Imap\Core;
|
||||
|
||||
use Clivern\Imap\Core\Message;
|
||||
use Clivern\Imap\Core\Connection;
|
||||
|
||||
/**
|
||||
* Message Iterator Class
|
||||
*
|
||||
* @package Clivern\Imap\Core
|
||||
*/
|
||||
class MessageIterator
|
||||
class MessageIterator extends \ArrayIterator
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $messages = [];
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* Add Message
|
||||
*
|
||||
* @param Message $message
|
||||
* @return void
|
||||
*/
|
||||
public function add(Message $message)
|
||||
{
|
||||
$this->messages[$message->getId()] = $message;
|
||||
}
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Connection $connection
|
||||
* @param array $message_numbers
|
||||
*/
|
||||
public function __construct(Connection $connection, array $message_numbers)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
parent::__construct($message_numbers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Message Exists
|
||||
*
|
||||
* @param integer $message_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($message_id)
|
||||
{
|
||||
return (boolean) isset($this->messages[$message_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Message
|
||||
*
|
||||
* @param integer $message_id
|
||||
* @return Message
|
||||
*/
|
||||
public function get($message_id)
|
||||
{
|
||||
return $this->messages[$message_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove Message
|
||||
*
|
||||
* @param integer $message_id
|
||||
* @return boolean
|
||||
*/
|
||||
public function remove($message_id)
|
||||
{
|
||||
return (boolean) unset($this->messages[$message_id]);
|
||||
}
|
||||
/**
|
||||
* Get current message
|
||||
*
|
||||
* @return Message
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
$message = new Message($this->connection);
|
||||
return $message->setUid(parent::current())->config();
|
||||
}
|
||||
}
|
53
src/Core/Search.php
Normal file
53
src/Core/Search.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* @author clivern <hello@clivern.com>
|
||||
*/
|
||||
|
||||
namespace Clivern\Imap\Core;
|
||||
|
||||
/**
|
||||
* Search Class
|
||||
*
|
||||
* @package Clivern\Imap\Core
|
||||
*/
|
||||
class Search
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $conditions = [];
|
||||
|
||||
/**
|
||||
* Add Condition
|
||||
*
|
||||
* @param string $condition
|
||||
* @return Search
|
||||
*/
|
||||
public function addCondition($condition)
|
||||
{
|
||||
$this->conditions[] = $condition;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Conditions
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getConditions()
|
||||
{
|
||||
return $this->conditions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Conditions Query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return (!empty($this->conditions)) ? implode(" ", $this->conditions) : "";
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @author clivern <hello@clivern.com>
|
||||
*/
|
||||
|
||||
namespace Clivern\Imap\Core;
|
||||
|
||||
/**
|
||||
* Tools Class
|
||||
*
|
||||
* @package Clivern\Imap\Core
|
||||
*/
|
||||
class Tools
|
||||
{
|
||||
#~
|
||||
}
|
143
src/MailBox.php
143
src/MailBox.php
|
@ -6,9 +6,9 @@
|
|||
namespace Clivern\Imap;
|
||||
|
||||
use Clivern\Imap\Core\Connection;
|
||||
use Clivern\Imap\Core\Folder;
|
||||
use Clivern\Imap\Core\MessageIterator;
|
||||
use Clivern\Imap\Core\Tools;
|
||||
use Clivern\Imap\Core\Message;
|
||||
use Clivern\Imap\Core\Search;
|
||||
|
||||
/**
|
||||
* MailBox Class
|
||||
|
@ -18,56 +18,107 @@ use Clivern\Imap\Core\Tools;
|
|||
class MailBox
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
protected $connection;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $folder;
|
||||
|
||||
/**
|
||||
* @var Folder
|
||||
*/
|
||||
protected $folder;
|
||||
/**
|
||||
* @var Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var MessageIterator
|
||||
*/
|
||||
protected $message_iterator;
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Connection $connection IMAP connection
|
||||
*/
|
||||
public function __construct(Connection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var Tools
|
||||
*/
|
||||
protected $tools;
|
||||
/**
|
||||
* Set Folder Name
|
||||
*
|
||||
* @param string $folder
|
||||
* @return MailBox
|
||||
*/
|
||||
public function setFolder($folder)
|
||||
{
|
||||
$this->folder = $folder;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* MailBox Constructor
|
||||
*
|
||||
* @param Connection $connection
|
||||
* @param Folder $folder
|
||||
* @param MessageIterator $message_iterator
|
||||
* @param Tools $tools
|
||||
*/
|
||||
public function __construct(Connection $connection, Folder $folder, MessageIterator $message_iterator, Tools $tools)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->folder = $folder;
|
||||
$this->message_iterator = $message_iterator;
|
||||
$this->tools = $tools
|
||||
}
|
||||
/**
|
||||
* Get Folder name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFolder()
|
||||
{
|
||||
return $this->folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Message Iterator
|
||||
*
|
||||
* @return MessageIterator
|
||||
*/
|
||||
public function getMessageIterator()
|
||||
{
|
||||
return $this->message_iterator;
|
||||
}
|
||||
/**
|
||||
* Get number of messages in this mailbox
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
$this->connection->survive($this->folder);
|
||||
return imap_num_msg($this->connection->getStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get message ids
|
||||
*
|
||||
* @param Search $search
|
||||
* @return MessageIterator|Message[]
|
||||
*/
|
||||
public function getMessages(Search $search = null)
|
||||
{
|
||||
$this->connection->survive($this->folder);
|
||||
$query = ($search) ? (string) $search : 'ALL';
|
||||
$message_numbers = imap_search($this->connection->getStream(), $query, \SE_UID);
|
||||
|
||||
if (false == $message_numbers) {
|
||||
// imap_search can also return false
|
||||
$message_numbers = [];
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
return "test";
|
||||
}
|
||||
return new MessageIterator($this->connection, $message_numbers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a message by message number or uid
|
||||
*
|
||||
* @param int|boolean $message_number
|
||||
* @param int|boolean $message_uid
|
||||
* @return Message
|
||||
*/
|
||||
public function getMessage($message_number = false, $message_uid = false)
|
||||
{
|
||||
$this->connection->survive($this->folder);
|
||||
|
||||
$message = new Message($this->connection);
|
||||
|
||||
if( $message_number == false ){
|
||||
return $message->setUid($message_uid)->config();
|
||||
}else{
|
||||
return $message->setMsgNo($message_number)->config();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all messages marked for deletion
|
||||
*
|
||||
* @return Mailbox
|
||||
*/
|
||||
public function expunge()
|
||||
{
|
||||
$this->connection->survive($this->folder);
|
||||
imap_expunge($this->connection->getStream());
|
||||
}
|
||||
}
|
|
@ -5,12 +5,12 @@ use PHPUnit\Framework\TestCase;
|
|||
|
||||
class ConnectionTest extends TestCase
|
||||
{
|
||||
public function testConnect()
|
||||
{
|
||||
$email = getenv("TEST_EMAIL");
|
||||
$password = getenv("TEST_EMAIL_PASS");
|
||||
$connection = new \Clivern\Imap\Core\Connection("imap.gmail.com", "993", $email, $password, "/ssl", "INBOX");
|
||||
$this->assertTrue($connection->connect()->checkConnection());
|
||||
$this->assertTrue($connection->connect()->disconnect());
|
||||
}
|
||||
public function testConnect()
|
||||
{
|
||||
$email = getenv("TEST_EMAIL");
|
||||
$password = getenv("TEST_EMAIL_PASS");
|
||||
$connection = new \Clivern\Imap\Core\Connection("imap.gmail.com", "993", $email, $password, "/ssl", "INBOX");
|
||||
$this->assertTrue($connection->connect()->checkConnection());
|
||||
$this->assertTrue($connection->connect()->disconnect());
|
||||
}
|
||||
}
|
19
tests/Core/SearchTest.php
Normal file
19
tests/Core/SearchTest.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
namespace Tests\Core;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SearchTest extends TestCase
|
||||
{
|
||||
public function testConditionBuilder()
|
||||
{
|
||||
$search = new \Clivern\Imap\Core\Search();
|
||||
$search->addCondition('RECENT')->addCondition('BCC "string"')->addCondition('NEW');
|
||||
$this->assertEquals($search->getConditions(), [
|
||||
'RECENT',
|
||||
'BCC "string"',
|
||||
'NEW'
|
||||
]);
|
||||
$this->assertEquals((string) $search, 'RECENT BCC "string" NEW');
|
||||
}
|
||||
}
|
|
@ -14,8 +14,8 @@ use PHPUnit\Framework\TestCase;
|
|||
*/
|
||||
class MailBoxTest extends TestCase
|
||||
{
|
||||
public function testGet()
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
public function testGet()
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue