gpt4 book ai didi

php - 使用 PHP,如何搜索 Gmail 的存档电子邮件

转载 作者:IT王子 更新时间:2023-10-28 23:48:05 26 4
gpt4 key购买 nike

(第一次用 PHP 编程。有一些帮助。需要更多。)

目标:

从我的 gmail 帐户的给定电子邮件地址中提取 lastContactDate。希望回答这个问题,“我最后一次联系 [Person] 是什么时候”

到目前为止我做了什么:

  • 使用 imap 连接到 gmail(仅限收件箱)
  • 获取日期和时间
  • 打印了此人的姓名和时间戳。

我不能做什么:

  • 搜索已归档的 lastContactDate 电子邮件(我是 inbox=0 的人)

注意事项:

  • 代码很粗糙,但很实用。 php 确实应该分开到不同的页面上,但这是第一次尝试。提前感谢您的帮助!
  • 热爱编程,顺便说一句。在过去的两天里,我不止一次跳了一次@edw519 的小舞。

研究:

目前使用的代码:

    /* connect to gmail */
$gmailhostname = '{imap.gmail.com:993/imap/ssl}';
$gmailusername = "___@gmail.com";
$gmailpassword = "___";

/* try to connect */
$conn = imap_open($gmailhostname,$gmailusername,$gmailpassword) or die('Cannot connect to Gmail: ' . imap_last_error());

$query = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($query))
{
$findemail = $row["email"];

/* grab emails */
$emails = imap_search($conn,'FROM "'.$findemail.'"');

/* if emails are returned, cycle through each... */
if ($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);

/* for 5 emails... */
$emails = array_slice($emails,0,1);

foreach ($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($conn,$email_number,0);
$message = imap_fetchbody($conn,$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="from">'.$overview[0]->from.'</span> ';
$output.= '<span class="date">on '.$overview[0]->date.'</span> <br /><br />';
mysql_query("UPDATE users SET lastContactDate = '".$overview[0]->date."' WHERE email = '".$findemail."'") or die(mysql_error());

/* output the email body */
/* $output.= '<div class="body">'.$message.'</div>'; */
}
echo $output;
}
}
/* close the connection */
imap_close($conn);
?>

最佳答案

问题解决了!

这是解决方案。使用上面的原始代码,我们只修改了程序搜索的位置。而不是收件箱,它是:

    /* connect to gmail */
$gmailhostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/All Mail';

具体

[Gmail]/All Mail

在这里找到语法:http://php.net/manual/en/function.imap-delete.php

但如果没有下面 Ben 的史诗般的解决方案,这将是不可能的。在很大程度上是因为这一点:

    //You can find out what folders are available with this command:
print_r(imap_list($conn, $gmailhostname, '*'));

print_r 按名称列出了我帐户中的所有文件夹。我们发现了“所有邮件”,在我的例子中 - 22,000+,在 php.net 上找到了一段带有语法的示例代码,将其插入并中提琴!

感谢 mmmshuddup 清理我的代码,特别感谢 Ben 所做的大量研究工作和领先的解决方案。

这太有趣了。

关于php - 使用 PHP,如何搜索 Gmail 的存档电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8175045/

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