【问题标题】:Check Gmail IMAP via PHP for new messages in a loop通过 PHP 循环检查 Gmail IMAP 中的新邮件
【发布时间】:2011-02-05 17:02:14
【问题描述】:

我正在研究一个应用程序,以根据到达 Gmail 的新 IMAP 电子邮件触发 PHP 脚本。知道新电子邮件已到达 Gmail IMAP 帐户的最佳方法是什么?除了配置一个 cron 作业,我什么都想不出来。我在 Linux (Ubuntu) 机器上运行 PHP + Nginx。

【问题讨论】:

    标签: php gmail imap


    【解决方案1】:

    我发现这正是蜂窝公司开发人员用来验证其客户 gmail 的方式。

    好吧,开始正常连接,然后:

    $t1=time();//mark time in
    $tt=$t1+(60*1);//total time = t1 + n seconds
    
    do{
        if(isset($t2)) unset($t2);//clean it at every loop cicle
        $t2=time();//mark time
        if(imap_num_msg($imap)!=0){//if there is any message (in the inbox)
    
            $mc=imap_check($imap);//messages check
            //var_dump($mc); die;//vardump it to see all the data it is possible to get with imap_check() and them customize it for yourself
    
    
        }else echo 'No new messagens';
    
        sleep(rand(7,13));//Give Google server a breack
        if(!@imap_ping($imap)){//if the connection is not up
            //start the imap connection the normal way like you did at first
        }
    
    }while($tt>$t2);//if the total time was not achivied yet, get back to the beginning of the loop
    

    就是这样。

    顺便说一下,这里有一些关于 IMAP 工作原理的有用信息。我的观点是:由于 IMAP 可以维持几乎一种“实时同步”连接,如果您不想配置 MTA 来接收电子邮件(如我),那么 IMAP 是获取“电子邮件推送”的真正选择"给你。

    • 除非您手动断开连接,否则每次连接到电子邮件时,连接都会保持 5 到 10 分钟的活跃状态
    • Gmail 确实将每个帐户限制为 10 个同时连接。
    • 但是,IMAP 帐户应检查邮箱,然后在超时前保持与 IMAP 服务器 (IMAP-IDLE) 的活动通道,以符合预设的行业标准 29 分钟。如果您将自动检索设置设置为 20 到 30 分钟之间,这应该会让您的手机连接到远程 IMAP 框。
    • 当 GMAIL 收到一封电子邮件时,它应该向 IMAP 空闲会话发送一个响应,并且 mobiPush 应该几乎立即接收它。
    • 所有计划每 10 分钟检索一次您的 Gmail 邮件,此选项将在收到的电子邮件到达 Gmail 服务器时立即同步。

    【讨论】:

      【解决方案2】:

      只有两种方法可以从电子邮件帐户获取信息,连接到它,定期阅读(例如,通过 cron-job)以获取新消息,或者将电子邮件转发到您自己的服务器,该服务器通过管道传输新电子邮件进入 PHP 脚本。

      Zend_Mail,Zend 框架的一部分有 Zend_Mail_Storage_Imap(可以在没有其他 MVC 结构的情况下使用),可以连接到 Gmail 以轮询帐户。

      【讨论】:

      • 谢谢你,阿利斯特。当我在我的域中使用 Google Apps 时,您会推荐什么解决方案?如果是cronjob,你有一个例子吗?
      猜你喜欢
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2012-06-02
      • 1970-01-01
      • 2012-01-02
      相关资源
      最近更新 更多