Guest User

Untitled

a guest
May 19th, 2018
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. <?php
  2.  
  3. /* connect to gmail */
  4. $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
  5. $username = '****@gmail.com';
  6. $password = 'PASSword';
  7. echo "<script>console.log( 'Debug Objects: " . $username . "' ); </script>";
  8. /* try to connect */
  9.  
  10. $items = array(
  11. 'msg' => array(),
  12. 'unseen_items' => array()
  13. );
  14.  
  15.  
  16. $mailbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
  17. if ($mailbox === false) {
  18. echo 'Error : ' . imap_last_error();
  19. } else {
  20. // Gets status information about the given mailbox
  21. $status = imap_status($mailbox, "{" . $host . "}INBOX", SA_ALL);
  22. if ($status === false) {
  23. echo 'Error : ' . imap_last_error();
  24. } else {
  25. $items['msg']['all'] = $status->messages;
  26. $items['msg']['unseen'] = $status->unseen;
  27. // No unseen messages
  28. if ($status->unseen == 0) {
  29. echo 'No unseen messages';
  30. } else {
  31. // Gets UIDs for unseen messages
  32. $messages = imap_search($mailbox, 'UNSEEN');
  33. if ($messages === false) {
  34. echo 'Error : ' . imap_last_error();
  35. } else {
  36. // For each all unseen messages
  37. foreach($messages as $uid ) {
  38. // Get header of the message
  39. $message = imap_headerinfo($mailbox, $uid);
  40. if ($message) {
  41. // From message
  42. $from = $message->from[0];
  43. $from_addres = $from->mailbox . "@" . $from->host;
  44. // Subject
  45. $subject = isset($message->subject)
  46. ? imap_utf8($message->subject) : 'No subject';
  47. // Re-format date
  48. $date = date("Y-m-d H:i", strtotime($message->date));
  49. // Add to data array
  50. $items['msg']['unseen_items'][] = array(
  51. $uid, $from_addres, $date, $subject
  52. );
  53. }
  54. }
  55. }
  56. }
  57. }
  58. // Close an IMAP stream
  59. imap_close($mailbox);
  60. }
  61. print_r(imap_errors());
  62. // Show messages from data array
  63. echo '<pre>' . print_r($items, true) . '</pre>';
  64. ?>
Add Comment
Please, Sign In to add comment