使用 PHP 从 Gmail 获取所有邮件

Getting all mails from Gmail using PHP

我的PHP代码:

<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'my gmail id';
$password = 'my gmail password';

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

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {

    /* begin output var */
    $output = '';

    /* put the newest emails on top */
    rsort($emails);

    /* for every email... */
    foreach($emails as $email_number) {

        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);

        /* output the email header information */
        $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
        $output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
        $output.= '<span class="from">'.$overview[0]->from.'</span>';
        $output.= '<span class="date">on '.$overview[0]->date.'</span>';
        $output.= '</div>';

        /* output the email body */
        $output.= '<div class="body">'.$message.'</div>';
    }

    echo $output;
} 

/* close the connection */
imap_close($inbox);
?>

问题是,首先我收到这样的错误 无法连接到 Gmail:登录失败次数过多,在我从 $hostname 中删除 /SSL 之后我的错误如 无法连接到 Gmail:[关闭] IMAP 连接断开(服务器响应)

我做了所有与连接相关的事情,比如 enable pop3 and Imap, Unlock Google帐户 并且打开安全性较低的应用程序

但是,我找不到实际问题是什么?

那是行不通的,因为我在 imap_search 中使用了 ALL,因此,它会收到大量邮件并且无法传递大量邮件。在我根据给出限制的搜索条件更改代码后,效果很好

    ALL - return all messages matching the rest of the criteria
    ANSWERED - match messages with the \ANSWERED flag set
    BCC "string" - match messages with "string" in the Bcc: field
    BEFORE "date" - match messages with Date: before "date"
    BODY "string" - match messages with "string" in the body of the message
    CC "string" - match messages with "string" in the Cc: field
    DELETED - match deleted messages
    FLAGGED - match messages with the \FLAGGED (sometimes referred to as Important or Urgent) flag set
    FROM "string" - match messages with "string" in the From: field
    KEYWORD "string" - match messages with "string" as a keyword
    NEW - match new messages
    OLD - match old messages
    ON "date" - match messages with Date: matching "date"
    RECENT - match messages with the \RECENT flag set
    SEEN - match messages that have been read (the \SEEN flag is set)
    SINCE "date" - match messages with Date: after "date"
    SUBJECT "string" - match messages with "string" in the Subject:
    TEXT "string" - match messages with text "string"
    TO "string" - match messages with "string" in the To:
    UNANSWERED - match messages that have not been answered
    UNDELETED - match messages that are not deleted
    UNFLAGGED - match messages that are not flagged
    UNKEYWORD "string" - match messages that do not have the keyword "string"
    UNSEEN - match messages which have not been read yet

PHP Code

$sinceDate = "31 December 2015";
$beforeDate = "06 January 2016"; 
$emails = imap_search($inbox,'SINCE "'.$sinceDate.'" BEFORE"'.$beforeDate.'"' );

在我使用 IMAP 下载目录中的附件后,它在 Downloading attachments to directory with IMAP in PHP, randomly works

上得到了很好的回答