使用PHP,如何搜索Gmail的存档电子邮件 [英] Using PHP, How to search through Gmail's archived emails

查看:106
本文介绍了使用PHP,如何搜索Gmail的存档电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次使用PHP编程,有一些帮助,需要多一点。



目标:



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



我到目前为止所做的:


  • 使用imap连接到Gmail(仅限收件箱) 打印日期和时间该人的姓名和时间戳。



我无法做的事:


  • 已删除lastContactDate的已删除电子邮件(我是一个收件箱= 0的人)



备注:




  • 代码很粗糙,但很实用。 PHP应该被分离到不同的页面上,但这是第一次尝试。预先感谢任何帮助!

  • 爱编程,顺便说一句。


研究:
$ b跳过了一次@ edw519跳舞$ b



迄今使用的代码:

  / *连接到gmail * / 
$ gmailhostname ='{imap.gmail.com:993/imap/ssl }';
$ gmailusername =___@gmail.com;
$ gmailpassword =___;

/ *尝试连接* /
$康恩= imap_open($ gmailhostname,$ gmailusername,$ gmailpassword)或死亡(无法连接到Gmail: imap_last_error());

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

/ *抓取电子邮件* /
$ emails = imap_search($ conn,'FROM''。$ findemail。'');
$ b $ *如果电子邮件被返回,则循环遍历每个... * /
if($ emails){
/ * begin输出var * /
$输出='';
/ *将最新的电子邮件放在顶部* /
rsort($ emails);

/ * 5封电子邮件... * /
$ emails = array_slice($ emails,0,1);

foreach($ emails as $ email_number){
/ *获取特定于此电子邮件的信息* /
$ overview = imap_fetch_overview($ conn,$ email_number,0);
$ message = imap_fetchbody($ conn,$ email_number,2);

/ *输出邮件头信息* /
/ *
$ output。='< div class =toggler'。($ overview [0] - > '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 SET用户lastContactDate = $概述[0] - >日期。 WHERE电子邮件= $ findemail。)或死亡(mysql_error());

/ *输出电子邮件正文* /
/ * $ output。='< div class =body>'。$ message。'< / div>'; * /
}
echo $ output;


*关闭连接* /
imap_close($ conn);
?>


解决方案

问题解决了!

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

  / *连接到gmail * / 
$ gmailhostname ='{imap.gmail。 com:993 / imap / ssl} [Gmail] / All Mail';

具体来说

  [Gmail] /所有邮件

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



但是如果没有Ben的史诗般的解决方案,就不可能实现这个目标。大部分是这个位:

  //你可以找到这个命令可用的文件夹:
print_r(imap_list($ conn,$ gmailhostname,'*'));

print_r按名称列出我帐户中的所有文件夹。我们发现所有邮件,在我的情况下 - 22,000+,在php.net上找到了一段代码,并附上了语法,插入它和中提琴!

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



这很有趣。


(First time programming in PHP. Had some help. Need a bit more.)

Goal:

Pull the lastContactDate from a given email address from my gmail account. Hoping to answer the question, "When was the last time I contacted [Person]"

What I've done so far:

  • Connected to gmail using imap (inbox only)
  • Grabbed the date and time
  • Printed the person's name and timestamp.

What I can't do:

  • Scour emails for lastContactDate that have been archived (I'm an inbox=0 guy)

Notes:

  • The code is rough, but functional. The php should really be separated onto different pages, but this is first attempt. Thanks in advance for any help!
  • Loving programming, btw. I did a little @edw519 dance more than once the last two days.

Research:

Code used thus far:

    /* 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);
?>

解决方案

Problem solved!

Here's the solution. Using the original code above, we only modified the location in which our program searches. Instead of INBOX, it's:

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

Specifically

[Gmail]/All Mail

Found the syntax here: http://php.net/manual/en/function.imap-delete.php

But would not have been possible without Ben's epic solution below.. In large part for this bit:

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

print_r listed all the folders in my account by name. We spotted "All Mail", in my case - 22,000+, found a sample piece of code on php.net with the syntax, plugged it in and viola!

Thanks to mmmshuddup for cleaning my code and especially Ben for the enormous researching effort and leading solutions.

This is fun as hell.

这篇关于使用PHP,如何搜索Gmail的存档电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆