title: Email-based PHP chat window
body: <p>I am building a chat/SMS based system I have it sending out the messages but what I need to do is live update the textarea with data from incoming emails without the user reloading (ajax?) I need to pass a number from the main page to fetch.php which gets the emails and creates an array out of emails which have not been read and come from the right sender what I need to do yet is send the number from the main page to the fetch page and return a array of new messages to the main textarea but all the tutorials I have found on ajax seem to require a database and I have no idea how to go about running and returning data on a delay help would be appreciated.</p>
  
  <p>Here is the content of fetch.php:</p>
  
  <pre><code>&lt;?php 
  
  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);
  ini_set('display_startup_errors', TRUE);
  
  /* connect to gmail */
  $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
  $username = 'user@gmail.com';
  $password = 'passwd';
  
  /* try to connect */
  $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
  
  /* grab emails */
  $emails = imap_search($inbox,'ALL');
  
  /* if emails are returned, cycle through each... */
  if($emails) {
  
    $messages[] = '';
  
    /* begin output var */
    $output = '';
  
    /* put the newest emails on top */
    rsort($emails);
  
    /* for every email... */
    foreach($emails as $email_number) {
  
      /* get information specific to this email */
      $overview = imap_fetch_overview($inbox,$email_number,0);
      $message = imap_fetchbody($inbox,$email_number,1);
  
      //print_r($overview);
  
      $Is_sms = strpos($overview[0]-&gt;from, "txt.voice.google.com");
      if($Is_sms === false) continue;
      if($overview[0]-&gt;seen != 0) continue;
  
      $pnl = strpos($overview[0]-&gt;from, ".");
      $pnumber = substr($overview[0]-&gt;from, $pnl +2, 10);
  
      if($pnumber != "3303331866") continue;
  
      $messages[] = $message;
  
          //$status = imap_setflag_full($mbox, $mail, "\\Seen \\Flagged", ST_UID);
      /* output the email header information */
      /*$output.= '&lt;div class="toggler '.($overview[0]-&gt;seen ? 'read' : 'unread').'"&gt;';
      $output.= '&lt;span class="subject"&gt;'.$overview[0]-&gt;subject.'&lt;/span&gt; ';
      $output.= '&lt;span class="from"&gt;'.$overview[0]-&gt;from.'&lt;/span&gt;';
      //$output.= '&lt;span class="date"&gt;on '.$overview[0]-&gt;date.'&lt;/span&gt;';
      $output.= '&lt;span class="pnumber"&gt;'.$pnumber.'&lt;/span&gt;';
      $output.= '&lt;/div&gt;';*/
  
      /* output the email body */
      //$output.= '&lt;div class="body"&gt;'.$message.'&lt;/div&gt;';
    }
  
    //echo $output;
    print_r($messages);
  } 
  
  /* close the connection */
  imap_close($inbox);
  </code></pre>
  
  <p>The main page is just a to number textbox, a content textarea, a message textbox, and a send button.</p>