duanqianruan8448 2012-08-01 19:28
浏览 52

PHP IMAP将电子邮件通知推送到iPhone

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

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.

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?

Thanks! James

  • 写回答

1条回答 默认 最新

  • douxian3828 2012-08-01 19:32
    关注

    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...

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

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上