php - 通过 PHP 检查 Gmail IMAP 循环中的新邮件

标签 php gmail imap

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

最佳答案

我发现这正是移动公司开发人员用来验证其客户 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 分钟的事件 channel 。如果您将自动检索设置设为 20 到 30 分钟之间,您的手机应该会保持与远程 IMAP 盒的连接。
  • 当 GMAIL 收到电子邮件时,它应该向 IMAP 空闲 session 发送响应,mobiPush 应该几乎立即接收它。
  • 所有计划每 10 分钟检索一次 Gmail 邮件,此选项将在收到的电子邮件到达 Gmail 服务器时立即同步。

关于php - 通过 PHP 检查 Gmail IMAP 循环中的新邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4908209/

相关文章:

php - 具有由 2 个 mysql 查询填充的动态行的 HTML 表

php - 选择 php-session-id 作为 MySQL 数据库中的主键是个好主意吗?

email - Route 53 和 GMail - MX 重定向

ruby-on-rails-3 - 我的 Gmail 收件箱中的 noname 附件

c# - 在 MailSystem.Net 中使用字符串的参数太多

c# - 如何限制 Office 365 应用程序的邮箱访问

php - 基于文本字段和列表菜单的条件搜索

javascript - Node.js 中的 Hash_hmac 等效项

html - 如何处理 Gmail 中 HTML 电子邮件中的媒体查询?

ios - iOS 上的邮件应用程序如何工作?