'; $output.= ''.$overview[0]->subject.' '; $-6ren">
gpt4 book ai didi

php - 使用 PHP 从 Gmail 获取所有邮件

转载 作者:可可西里 更新时间:2023-10-31 23:02:54 26 4
gpt4 key购买 nike

我的 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: [CLOSED] IMAP 连接断开(服务器响应) 的错误。

我做了所有与连接相关的事情,比如启用 pop3Imap解锁 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 上做出了很好的回答

关于php - 使用 PHP 从 Gmail 获取所有邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34449422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com