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

and*_*rah 23 php gmail imap-open

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

目标:

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

到目前为止我做了什么:

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

我做不到的事:

  • 搜集已存档的lastContactDate的电子邮件(我是收件箱= 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);
?>
Run Code Online (Sandbox Code Playgroud)

and*_*rah 12

问题解决了!

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

    /* connect to gmail */
$gmailhostname = '{imap.gmail.com:993/imap/ssl}[Gmail]/All Mail';
Run Code Online (Sandbox Code Playgroud)

特别

[Gmail]/All Mail
Run Code Online (Sandbox Code Playgroud)

在这里找到语法: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, '*'));
Run Code Online (Sandbox Code Playgroud)

print_r按名称列出了我帐户中的所有文件夹.我们发现"All Mail",在我的情况下 - 22,000+,在php.net上找到了一段代码示例,其中包含语法,插入中提琴和中提琴!

感谢mmmshuddup用于清理我的代码,特别是Ben用于巨大的研究工作和领先的解决方案.

这很有趣.


Ben*_*Ben 6

我从来没有使用过imap函数,但是通过手册查看,问题可能是你的imap_search函数返回的是简单的消息序列号而不是UID,我猜这是唯一的消息ID?

也许有人可以帮助你更好我只是尝试一些事情.

尝试将imap_search函数更改为:

   $emails = imap_search($conn,'FROM "'.$findemail.'"', SE_UID);
Run Code Online (Sandbox Code Playgroud)

你的fetch函数对这些:

  $overview = imap_fetch_overview($conn,$email_number, FT_UID);
  $message = imap_fetchbody($conn,$email_number,2, FT_UID);
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,你可以尝试的另一件事就是将fetch_overview设置为其中之一:

$overview = imap_fetch_overview($conn,"1:{$email_number}",0);
// Or Maybe:
$overview = imap_fetch_overview($conn,"{$email_number}:{$email_number}",0);
Run Code Online (Sandbox Code Playgroud)

这告诉它从1获取消息,无论是我认为的$ email_number,还是一系列消息ID而不是唯一消息ID.不过不确定.

我认为rsort()不会使用UID方法,因此如果使用该方法,则必须找到不同的方式对它们进行排序.您可能必须获取所有匹配电子邮件标头的数组,并按此方式排序.

对不起,我没有更多帮助,以前从未使用过imap,但祝你好运!

编辑:手册页对此非常奇怪,但看起来imap_sort函数也有搜索条件,所以从理论上讲,你可以这样做:

  $emails = imap_sort($conn, SORTARRIVAL, 0, SE_UID, 'FROM "'.$findemail.'"');
  // and then grab the first one:
  $emails = array_slice($emails,0,1);

  //And then further down use these two with the UID param
   $overview = imap_fetch_overview($conn,$email_number, FT_UID);
   $message = imap_fetchbody($conn,$email_number,2, FT_UID);
Run Code Online (Sandbox Code Playgroud)

如果您仍未收到存档中的消息,您可能会看到以下答案:

PHP imap_search未检测到gmail收件箱中的所有邮件

再次编辑

哇,这比我想的还要多......这已成为有史以来最长的答案......

根据您的要求,如果您只需要在存档文件夹中查找消息,我相信您需要重新打开连接并在搜索之前连接到该特定文件夹,例如:

imap_reopen($conn, "{$gmailhostname}Archive") or die(implode(", ", imap_errors()));
//You can find out what folders are available with this command:
print_r(imap_list($conn, $gmailhostname, '*'));
Run Code Online (Sandbox Code Playgroud)

如果您需要搜索所有文件夹...这比我看到的更难:您需要遍历要搜索的每个电子邮件收件箱,或者找到使用此方法的方法:

http://code.google.com/apis/gmail/imap/#x-gm-raw

我想你需要一个自定义的imap处理程序,或ZEND. php中的自定义IMAP命令

这正式是我能找到的所有信息.