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

发布于 2024-10-16 01:12:48 字数 144 浏览 6 评论 0原文

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

I am studying an application to trigger a PHP script based on new IMAP emails arriving on Gmail. What's the best way to know a new email has arrived on a Gmail IMAP account? I can't think of anything but to configure a cron job. I am running PHP + Nginx on a Linux (Ubuntu) box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-10-23 01:12:48

我发现这正是蜂窝公司开发人员验证客户 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 服务器时立即同步。

I found out that that's just the way celular companies developers are doing to verify their clients gmail.

Well, start making the connection normaly, then:

$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

And that's it.

By the way, here's some good informations about how IMAP works. My point is that: as IMAP makes possible to mantain virtually a kind of "Live Sync" connection, if you don't want to configure a MTA to receive email (like me), so IMAP is a real option for get "email pusshed" to you.

  • The connection stays active from 5 to 10 minutes each time you connect to your email, unless you manually disconnect
  • Gmail does limit each account to 10 simultaneous connections.
  • An IMAP account, though, shoud check the mailbox and then keep an active channel to the IMAP server (IMAP-IDLE) for the preset industry standard of 29 minutes before it times out. If you set your Auto-Retrieve setting to between 20-30 minutes, that should keep your phone connected to the remote IMAP box.
  • When GMAIL gets an email it should send a response to the IMAP idle session and mobiPush should pick it up almost instantly.
  • All schedule to retrieve your Gmail mail every 10 minutes, this option will sync incoming E-mail immediately when it arrives at the Gmail servers.
绅士风度i 2024-10-23 01:12:48

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

Zend_Mail,Zend Framework 的一部分有 Zend_Mail_Storage_Imap (可以在没有MVC 结构的其余部分)可以连接到 Gmail 来轮询帐户。

There's only two ways to get info from an email account, connect to it, reading it regularly (for example, by a cron-job) for new messages, or have the email forwarded to your own server, which pipes new email arriving into a PHP script.

Zend_Mail, part of the Zend Framework has Zend_Mail_Storage_Imap (which can be used without the rest of the MVC structure) which can connect to Gmail to poll an account.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文