Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- # @todo
- # recursive line checking based on lenght
- # check for response RFC
- # fread 4096 except mails
- $login = "first.last@gmail.com";
- $password = "p4$$W0rD";
- if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15)))
- {
- die("Could not connect to host");
- }
- // server greeting
- get_line($fp, "/^\* OK/");
- // unique ID
- define('UID', uniqid());
- put_line($fp, UID." LOGIN $login $password\r\n");
- get_line($fp, "/^".UID." OK/");
- $msgbox = "INBOX";
- put_line($fp, UID." SELECT $msgbox\r\n");
- get_line($fp, "/^".UID." OK/");
- put_line($fp, UID." IDLE\r\n");
- get_line($fp, "/^./");
- while(TRUE)
- {
- $response = get_line($fp, "/^./");
- if(preg_match('/([\d]+) EXISTS/', $response, $matches))
- {
- // DONE: OK/NO/BAD
- put_line($fp, "DONE\r\n");
- get_line($fp, "/^./");
- // FETCH: OK/NO/BAD
- put_line($fp, UID." FETCH ".$matches[1]." BODY[TEXT]\r\n");
- get_line($fp, "/^./");
- echo time();
- put_line($fp, UID." IDLE"."\r\n");
- }
- }
- fclose($fp);
- function put_line($fp, $line)
- {
- echo "sending $line";
- fwrite($fp, $line);
- }
- function get_line($fp, $expect)
- {
- echo ">get_line $expect\r\n";
- $r = "";
- echo ".";
- $timeout = 10 * 1000000; // 10 s
- $timeout = 10 * 10;
- echo stream_set_blocking($fp, 1);
- echo stream_set_timeout($fp, 0, $timeout);
- $info = stream_get_meta_data($fp);
- while ((!feof($fp)) && (!$info['timed_out'])) {
- echo ".";
- $line = fgets($fp, 4096);
- $info = stream_get_meta_data($fp);
- echo "received " . $line;
- $r .= $line;
- if (preg_match($expect, $line)) {
- break;
- }
- }
- return $r;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement