如何通过 PHP IMAP 从 Gmail 服务器读取电子邮件

Hay*_*yan 3 php email gmail imap

我正在尝试使用 PHP IMAP 读取来自 Gmail 服务器的所有电子邮件。

我有以下配置:

$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = 'email';
$password = 'pass';

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

var_dump($inbox);exit;
Run Code Online (Sandbox Code Playgroud)

我收到 PHP 警告和通知:

imap_open():无法打开流 {imap.gmail.com:995/imap/ssl/novalidate-cert}INBOX

未知:无法连接到 imap.gmail.com,993:第 0 行未知中超时 (errflg=2)

请帮忙

Abh*_*nav 5

set_time_limit(4000); \n\n// Connect to gmail\n$imapPath = \'{imap.gmail.com:993/imap/ssl}INBOX\';\n$username = \'your_email_id@gmail.com\';\n$password = \'your_gmail_password\';\n\n// try to connect \n$inbox = imap_open($imapPath,$username,$password) or die(\'Cannot connect to Gmail: \' . imap_last_error());\n\n   /* ALL - return all messages matching the rest of the criteria\n    ANSWERED - match messages with the \\\\ANSWERED flag set\n    BCC "string" - match messages with "string" in the Bcc: field\n    BEFORE "date" - match messages with Date: before "date"\n    BODY "string" - match messages with "string" in the body of the message\n    CC "string" - match messages with "string" in the Cc: field\n    DELETED - match deleted messages\n    FLAGGED - match messages with the \\\\FLAGGED (sometimes referred to as Important or Urgent) flag set\n    FROM "string" - match messages with "string" in the From: field\n    KEYWORD "string" - match messages with "string" as a keyword\n    NEW - match new messages\n    OLD - match old messages\n    ON "date" - match messages with Date: matching "date"\n    RECENT - match messages with the \\\\RECENT flag set\n    SEEN - match messages that have been read (the \\\\SEEN flag is set)\n    SINCE "date" - match messages with Date: after "date"\n    SUBJECT "string" - match messages with "string" in the Subject:\n    TEXT "string" - match messages with text "string"\n    TO "string" - match messages with "string" in the To:\n    UNANSWERED - match messages that have not been answered\n    UNDELETED - match messages that are not deleted\n    UNFLAGGED - match messages that are not flagged\n    UNKEYWORD "string" - match messages that do not have the keyword "string"\n    UNSEEN - match messages which have not been read yet*/\n\n// search and get unseen emails, function will return email ids\n$emails = imap_search($inbox,\'UNSEEN\');\n\n$output = \'\';\n\nforeach($emails as $mail) {\n\n    $headerInfo = imap_headerinfo($inbox,$mail);\n\n    $output .= $headerInfo->subject.\'<br/>\';\n    $output .= $headerInfo->toaddress.\'<br/>\';\n    $output .= $headerInfo->date.\'<br/>\';\n    $output .= $headerInfo->fromaddress.\'<br/>\';\n    $output .= $headerInfo->reply_toaddress.\'<br/>\';\n\n    $emailStructure = imap_fetchstructure($inbox,$mail);\n\n    if(!isset($emailStructure->parts)) {\n         $output .= imap_body($inbox, $mail, FT_PEEK); \n    } else {\n        //    \n    }\n   echo $output;\n   $output = \'\';\n}\n\n// colse the connection\nimap_expunge($inbox);\nimap_close($inbox);\n
Run Code Online (Sandbox Code Playgroud)\n\n

要使用它,您需要安装并启用php5-imap

\n\n

您可以使用以下命令安装 PHP5 IMAP 模块:

\n\n
apt-get install php5-imap\n
Run Code Online (Sandbox Code Playgroud)\n\n

然而,它\xe2\x80\x99s默认情况下未启用,因此启用它:

\n\n
php5enmod imap\n
Run Code Online (Sandbox Code Playgroud)\n\n

要查看更改,请重新启动 Apache

\n\n
service apache2 restart\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于装有 Xampp7 及更高版本的 Windows,

\n\n

转到php.ini,\n删除分号,

\n\n
;extension=imap\n
Run Code Online (Sandbox Code Playgroud)\n