PHP IMAP将电子邮件通知推送到iPhone [英] PHP IMAP push email notification to iPhone

查看:79
本文介绍了PHP IMAP将电子邮件通知推送到iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个PHP脚本,该脚本通过IMAP定期检查用户的收件箱中是否有新消息.该脚本会保持与IMAP服务器的开放连接,并每5秒获取一次最新消息的UID.如果UID大于最初记录的比较UID,则脚本会将推式通知发送到用户的iPhone,以通知他/她有新消息可用,将新UID记录为比较UID,并继续检查是否有新消息.以这种方式.这是脚本:

I'm working on a PHP script that periodically checks the user's inbox for new messages via IMAP. The script leaves an open connection to the IMAP server, and grabs the UID of the most recent message every 5 seconds. If the UID is greater than the initially recorded comparison UID, the script sends a push notification to the user's iPhone notifying him/her that there is a new message available, records the new UID as the comparison UID, and continues to check for new messages in this fashion. Here is the script:

<?php
$server = '{imap.gmail.com:993/ssl}';
$login = 'email_address@gmail.com';
$password = 'my_email_password';
$connection = imap_open($server, $login, $password) OR die ("can't connect: " . imap_last_error());

$imap_obj = imap_check($connection);
$number = $imap_obj->Nmsgs;
$uid = imap_uid($connection, $number);


//infinite loop, need to add some sort of escape condition...
for(;;){

$imap_obj = imap_check($connection);
$number = $imap_obj->Nmsgs;

    //if there is a new message send push notification
    if(imap_uid($connection, $number) > $uid){

    $uid = imap_uid($connection, $number);


$result = imap_fetch_overview($connection,$number,0);

$message = $result[0]->subject;


                $deviceToken = 'xxxxxxxxxxxxxxxxxx';
                $passphrase = 'my_secret_password';

                $ctx = stream_context_create();
                stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
                stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

                $fp = stream_socket_client(
                    'ssl://gateway.sandbox.push.apple.com:2195', $err,
                    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

                if (!$fp)
                    exit("Failed to connect: $err $errstr" . PHP_EOL);

                echo 'Connected to APNS' . PHP_EOL;

                // Create the payload body
                $body['aps'] = array(
                    'alert' => $message,
                    'sound' => 'default'
                    );

                // Encode the payload as JSON
                $payload = json_encode($body);

                // Build the binary notification
                $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

                // Send it to the server
                $result = fwrite($fp, $msg, strlen($msg));

                if (!$result)
                    //echo 'Message not delivered' . PHP_EOL;
                else
                    //echo 'Message successfully delivered' . PHP_EOL;

                // Close the connection to the server
                fclose($fp);

    }

sleep(5);
}

imap_close($connection);
?>

这有效.但这对我来说似乎效率很低.每个其他用户都与IMAP服务器保持不确定的连接,每隔几秒钟检查一次新消息,这似乎很愚蠢.

This works. But it seems terribly inefficient to me. Each additional user maintains an indefinite connection with the IMAP server, and checks for new messages every couple seconds, which seems silly.

是否有更好的方法可以做到这一点?用示例PHP代码/代码链接(这将是理想的,我会永远爱你),或者用抽象术语(这将产生相似但较少的无条件崇拜),有人可以解释此类任务的最佳做法吗?

Is there a better way to do this? Either with example PHP code/links to code (which would be ideal, and I would love you forever) or in abstract terms (which would yield similar yet less unconditional adoration) can someone explain best practices for this sort of task?

谢谢! 詹姆斯

推荐答案

您是否可以使用CRON之类的程序来安排脚本每30秒运行一次,该脚本将为每个用户保留最后一个UID的db记录,然后对每个用户进行搜索用户本地邮箱(通过本地外壳而不是imapd连接),如果特定用户有新消息,则推送通知并更新数据库中该用户的最新UID ...

Couldnt you use something like CRON to schedule a script to run every 30 seconds, which keeps a db record of the last UID for every user, and then searches each users mailbox locally (via local shell not imapd connection), if a particular user has a new message, push the notification and update the latest UID for that user in the DB...

这是假设PHP脚本位于邮件服务器上,并且您具有root用户访问权限.

This is assuming the PHP script is living on the mail server and you have root access.

这篇关于PHP IMAP将电子邮件通知推送到iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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