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
38 changes: 33 additions & 5 deletions src/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.
*/

namespace Eden\Mail;

/**
Expand Down Expand Up @@ -60,6 +59,11 @@ class Imap extends Base
*/
protected $password = null;

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

/**
* @var int $tag The tag number
*/
Expand Down Expand Up @@ -109,22 +113,25 @@ class Imap extends Base
* @param int|null $port The IMAP port
* @param bool $ssl Whether to use SSL
* @param bool $tls Whether to use TLS
* @param *string|null $xoauth2token The oauth2token
*/
public function __construct(
$host,
$user,
$pass,
$port = null,
$ssl = false,
$tls = false
$tls = false,
$xoauth2token = null
) {
Argument::i()
->test(1, 'string')
->test(2, 'string')
->test(3, 'string')
->test(4, 'int', 'null')
->test(5, 'bool')
->test(6, 'bool');
->test(6, 'bool')
->test(7, 'string', 'null');

if (is_null($port)) {
$port = $ssl ? 993 : 143;
Expand All @@ -136,6 +143,7 @@ public function __construct(
$this->port = $port;
$this->ssl = $ssl;
$this->tls = $tls;
$this->xoauth2token = $xoauth2token;
}

/**
Expand Down Expand Up @@ -202,7 +210,14 @@ public function connect($timeout = self::TIMEOUT, $test = false)
}

//login
$result = $this->call('LOGIN', $this->escape($this->username, $this->password));
if ($this->xoauth2token) {
$access_token=$this->xoauth2token;
$auth = "user={$this->username}\1auth=Bearer {$access_token}\1\1";
$auth = base64_encode($auth);
$result = $this->call('AUTHENTICATE XOAUTH2', $auth);
} else {
$result = $this->call('LOGIN', $this->escape($this->username, $this->password));
}

if (!is_array($result) || strpos(implode(' ', $result), 'OK') === false) {
$this->disconnect();
Expand Down Expand Up @@ -1150,7 +1165,7 @@ private function getEmailResponse($command, $parameters = array(), $first = fals
}

//if there is a tag it means we are at the end
if (strpos($line, 'TAG'.$this->tag) !== false) {
if (strpos($line, 'TAG'.$this->tag) === 0) {
//if email details are not empty and the last line is just a )
if (!empty($email) && strpos(trim($email[count($email) -1]), ')') === 0) {
//take it out because that is not part of the details
Expand Down Expand Up @@ -1340,6 +1355,19 @@ private function getParts($content, array $parts = array())
if (isset($extra['name'])) {
//add to parts
$parts['attachment'][$extra['name']][$type] = $body;
} elseif (isset($head['content-disposition']) && strpos($head['content-disposition'],'attachment')===0) {
//add to parts
//split the content type
$filename=null;
if (is_string($head['content-disposition'])&&strpos($head['content-disposition'],'filename=')!==false) {
if(preg_match("/filename=([^;]*)/",$head['content-disposition'],$matches)) {
$filename=$matches[1];
}
}
if($filename)
$parts['attachment'][$filename][$type] = $body;
else
$parts['attachment'][][$type] = $body;
} else {
//it's just a regular body
//add to parts
Expand Down
8 changes: 5 additions & 3 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@ class Index extends Base
* @param int|null $port The IMAP port
* @param bool $ssl Whether to use SSL
* @param bool $tls Whether to use TLS
* @param *string|null $xoauth2token The oauth2token
*
* @return Eden\Mail\Imap
*/
public function imap($host, $user, $pass, $port = null, $ssl = false, $tls = false)
public function imap($host, $user, $pass, $port = null, $ssl = false, $tls = false, $xoauth2token = null)
{
Argument::i()
->test(1, 'string')
->test(2, 'string')
->test(3, 'string')
->test(4, 'int', 'null')
->test(5, 'bool')
->test(6, 'bool');
->test(6, 'bool')
->test(7, 'string', 'null');

return Imap::i($host, $user, $pass, $port, $ssl, $tls);
return Imap::i($host, $user, $pass, $port, $ssl, $tls, $xoauth2token);
}

/**
Expand Down