diff --git a/src/Imap.php b/src/Imap.php index 2449bb2..98722a4 100644 --- a/src/Imap.php +++ b/src/Imap.php @@ -6,7 +6,6 @@ * Copyright and license information can be found at LICENSE.txt * distributed with this package. */ - namespace Eden\Mail; /** @@ -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 */ @@ -109,6 +113,7 @@ 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, @@ -116,7 +121,8 @@ public function __construct( $pass, $port = null, $ssl = false, - $tls = false + $tls = false, + $xoauth2token = null ) { Argument::i() ->test(1, 'string') @@ -124,7 +130,8 @@ public function __construct( ->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; @@ -136,6 +143,7 @@ public function __construct( $this->port = $port; $this->ssl = $ssl; $this->tls = $tls; + $this->xoauth2token = $xoauth2token; } /** @@ -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(); @@ -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 @@ -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 diff --git a/src/Index.php b/src/Index.php index b71f019..546a434 100644 --- a/src/Index.php +++ b/src/Index.php @@ -33,10 +33,11 @@ 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') @@ -44,9 +45,10 @@ public function imap($host, $user, $pass, $port = null, $ssl = false, $tls = fal ->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); } /**