Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "eden/mail",
"name": "leonardoweslei/eden-mail",
"type": "library",
"description": "Eden POP3, IMAP and SMTP component",
"keywords": ["eden", "library"],
Expand Down
112 changes: 84 additions & 28 deletions src/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,72 +29,72 @@ class Imap extends Base
* @const string NO_SUBJECT Default subject
*/
const NO_SUBJECT = '(no subject)';

/**
* @var string $host The IMAP Host
*/
protected $host = null;

/**
* @var string|null $port The IMAP port
*/
protected $port = null;

/**
* @var bool $ssl Whether to use SSL
*/
protected $ssl = false;

/**
* @var bool $tls Whether to use TLS
*/
protected $tls = false;

/**
* @var string|null $username The mailbox user name
*/
protected $username = null;

/**
* @var string|null $password The mailbox password
*/
protected $password = null;

/**
* @var int $tag The tag number
*/
protected $tag = 0;

/**
* @var int $total The total main in mailbox
*/
protected $total = 0;

/**
* @var int $next for pagination
*/
protected $next = 0;

/**
* @var string|null $buffer Mail body
*/
protected $buffer = null;

/**
* @var [RESOURCE] $socket The socket connection
*/
protected $socket = null;

/**
* @var string|null $mailbox The mailbox name
*/
protected $mailbox = null;

/**
* @var array $mailboxes The list of mailboxes
*/
protected $mailboxes = array();

/**
* @var bool $debugging If true outputs the logs
*/
Expand Down Expand Up @@ -371,7 +371,18 @@ public function getMailboxes()
continue;
}

$mailboxes[] = $line[count($line)-2];
$mailbox = trim($line[count($line) - 2]);

if ($mailbox == "/" || $mailbox == "") {
$mailbox = $line[count($line) - 1];
}

//Fix mailbox name encoded with utf7
$mailbox = ImapUtf7::decode(trim($mailbox));
//Decoding utf8 string result
$mailbox = utf8_decode($mailbox);

$mailboxes[] = $mailbox;
}

return $mailboxes;
Expand Down Expand Up @@ -462,17 +473,16 @@ public function remove($uid)

return $this;
}

/**
* Remove an email from a mailbox
*
* @return Eden\Mail\Imap
*/
public function expunge()
{

$this->call('expunge');

return $this;
}

Expand Down Expand Up @@ -673,7 +683,16 @@ public function setActiveMailbox($mailbox)
if (strpos($line, 'EXISTS') !== false) {
list($star, $this->total, $type) = explode(' ', $line, 3);
} else if (strpos($line, 'UIDNEXT') !== false) {
list($star, $ok, $next, $this->next, $type) = explode(' ', $line, 5);
$explode = explode(' ', $line, 5);
// the list function expects 5 items
if (count($explode) < 5) {
array_push($explode, "The next unique identifier value");
list($star, $ok, $next, $this->next, $type) = $explode;
}
else {
list($star, $ok, $next, $this->next, $type) = explode(' ', $line,
5);
}
$this->next = substr($this->next, 0, -1);
}

Expand Down Expand Up @@ -740,7 +759,11 @@ protected function receive($sentTag)
$start = time();

while (time() < ($start + self::TIMEOUT)) {
list($receivedTag, $line) = explode(' ', $this->getLine(), 2);
$explode = explode(' ', $this->getLine(), 2);
if (count($explode) < 2) {
array_push($explode, "OK []\r\n");
}
list($receivedTag, $line) = $explode;
$this->buffer[] = trim($receivedTag . ' ' . $line);
if ($receivedTag == 'TAG'.$sentTag) {
return $this->buffer;
Expand Down Expand Up @@ -890,7 +913,12 @@ private function getEmailFormat($email, $uniqueId = null, array $flags = array()
}
}

$sender['email'] = $headers1->from[0]->mailbox . '@' . $headers1->from[0]->host;
if (isset($headers1->from[0]) && is_object($headers1->from[0])) {
$sender['email'] = $headers1->from[0]->mailbox . '@' . $headers1->from[0]->host;
}
else {
$sender['email'] = 'unknown@localhost.localdomain';
}

//set the to
if (isset($headers1->to)) {
Expand Down Expand Up @@ -1059,6 +1087,16 @@ private function getEmailResponse($command, $parameters = array(), $first = fals
//if the line starts with a fetch
//it means it's the end of getting an email
if (strpos($line, 'FETCH') !== false && strpos($line, 'TAG'.$this->tag) === false) {
$flags = [];
/**
* simple regex to match the FETCH line
* used to skip over a $line that hasn't got to do with the FETCH or TAG line
*/
preg_match('/^\* [0-9]+/', $line, $arr);
// no match, there isn't sufficient information to parse the email. Skipping.
// potential exception: undefined $flags
if (empty($arr)) continue;

//if there is email data
if (!empty($email)) {
//create the email format and add it to emails
Expand Down Expand Up @@ -1359,7 +1397,12 @@ function imap_rfc822_parse_headers($header)
$headers->to = $headers->cc = $headers->bcc = array();

preg_match('#Message\-(ID|id|Id)\:([^\n]*)#', $header, $ID);
$headers->ID = trim($ID[2]);
if (isset($ID[2])){
$headers->ID = trim($ID[2]);
}
else {
$headers->ID = 'Message-ID: <unknown>';
}
unset($ID);

preg_match('#\nTo\:([^\n]*)#', $header, $to);
Expand All @@ -1372,7 +1415,12 @@ function imap_rfc822_parse_headers($header)

$headers->from = array(new \stdClass());
preg_match('#\nFrom\:([^\n]*)#', $header, $from);
$headers->from[0] = imap_rfc822_parse_headers_decode(trim($from[1]));
if (isset($from[1])) {
$headers->from[0] = imap_rfc822_parse_headers_decode(trim($from[1]));
}
else {
$headers->from[0] = 'unknown@localhost.localdomain';
}

preg_match('#\nCc\:([^\n]*)#', $header, $cc);
if (isset($cc[1])) {
Expand All @@ -1391,15 +1439,23 @@ function imap_rfc822_parse_headers($header)
}

preg_match('#\nSubject\:([^\n]*)#', $header, $subject);
$headers->subject = trim($subject[1]);
unset($subject);
if (isset($subject[1])) {
$headers->subject = trim($subject[1]);
unset($subject);
}

preg_match('#\nDate\:([^\n]*)#', $header, $date);
$date = substr(trim($date[0]), 6);
if (isset($date[0])) {
$date = substr(trim($date[0]), 6);


$date = preg_replace('/\(.*\)/', '', $date);
$date = preg_replace('/\(.*\)/', '', $date);

$headers->date = trim($date);
$headers->date = trim($date);
}
else {
$headers->date = date('d-m-Y H:i:s');
}
unset($date);

foreach ($ccs as $k => $cc) {
Expand Down
Loading