作者 邓超

imap

@@ -62,16 +62,16 @@ class Imap { @@ -62,16 +62,16 @@ class Imap {
62 // 打开连接 62 // 打开连接
63 try { 63 try {
64 $this->client->open(); 64 $this->client->open();
65 - }catch (\Throwable $e){  
66 - return (new Login($this));  
67 - }  
68 65
  66 + //有的服务器是强制要求设置id属性 已知 163 有的服务器没有id命令 这里就不处理结果了
  67 + $this->client->request('ID ("name" "测试本地 Client" "version" "1" "os" "测试本地" "os-version" "1.0")');
69 68
70 - //有的服务器是强制要求设置id属性 已知 163 有的服务器没有id命令 这里就不处理结果了  
71 - $this->client->request('ID ("name" "测试本地 Client" "version" "1" "os" "测试本地" "os-version" "1.0")'); 69 + // 登录
  70 + return (new Login($this))->exec();
72 71
73 - // 登录  
74 - return (new Login($this))->exec(); 72 + }catch (\Throwable $e){
  73 + return (new Login($this))->setMsg($e->getMessage());
  74 + }
75 75
76 } 76 }
77 77
@@ -21,11 +21,19 @@ class ImapClient { @@ -21,11 +21,19 @@ class ImapClient {
21 */ 21 */
22 public int $tagNum = 0; 22 public int $tagNum = 0;
23 23
  24 + /**
  25 + * 是否是非阻塞模式
  26 + * @var bool
  27 + */
  28 + public $isBlocking = false;
  29 +
24 public function __construct(string $host){ 30 public function __construct(string $host){
25 $this->host = $host; 31 $this->host = $host;
26 $this->socket = false; 32 $this->socket = false;
27 } 33 }
28 34
  35 +
  36 +
29 public function open(){ 37 public function open(){
30 $content = stream_context_create([ 38 $content = stream_context_create([
31 'ssl' => [ 39 'ssl' => [
@@ -45,6 +53,9 @@ class ImapClient { @@ -45,6 +53,9 @@ class ImapClient {
45 $content 53 $content
46 ); 54 );
47 55
  56 + // 设置为非阻塞模式
  57 + $this->isBlocking = stream_set_blocking($this->socket, false);
  58 +
48 if($error){ 59 if($error){
49 throw new \Exception($error); 60 throw new \Exception($error);
50 } 61 }
@@ -90,7 +101,30 @@ class ImapClient { @@ -90,7 +101,30 @@ class ImapClient {
90 * @time 2024/9/13 15:49 101 * @time 2024/9/13 15:49
91 */ 102 */
92 protected function readLine(){ 103 protected function readLine(){
93 - $str = fgets($this->socket,2048); 104 + $i = 50; // 非阻塞模式下,只等待5秒,没返回数据就返回
  105 + $str = false;
  106 + while ($i>0){
  107 + $str = fgets($this->socket,2048);
  108 + // 非阻塞模式
  109 + if($this->isBlocking && $str === false){
  110 + $i--;
  111 + // 兼容下swoole
  112 + if(function_exists('go') && \co::getCid()>0){
  113 + \co::sleep(0.1);
  114 + }else{
  115 + // 等待 100 毫秒
  116 + usleep(100000);
  117 + }
  118 +
  119 + }else{
  120 + break;
  121 + }
  122 + }
  123 + // 超时了,异常
  124 + if(!$i && $str===false){
  125 + throw new \Exception('read time out');
  126 + }
  127 +
94 if($this->debug){ 128 if($this->debug){
95 echo 'read: '.$str; 129 echo 'read: '.$str;
96 } 130 }
@@ -24,6 +24,16 @@ class Login extends Request{ @@ -24,6 +24,16 @@ class Login extends Request{
24 } 24 }
25 25
26 26
  27 + /**
  28 + * @param string $msg
  29 + * @return $this
  30 + * @author:dc
  31 + * @time 2025/3/14 10:16
  32 + */
  33 + public function setMsg(string $msg){
  34 + $this->message = $msg;
  35 + return $this;
  36 + }
27 37
28 38
29 } 39 }