Search code examples
phpemailgmail-imap

How to receive gmail using imap in server?


I want to receive the mails from gmail using imap. I have used the code $connection_result = $imap->connect('{imap.gmail.com:993/imap/ssl}INBOX', '[email protected]', 'xxxxxx'); It works in the local xampp but if host in my server, its giving error as

Cannot connect to Mail: Can't connect to gmail-imap.l.google.com,993: Network is unreachable

How to use in server. Anyone can help?


Solution

  • $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = '[email protected]';
    $password = 'example';
    $inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
    $emails = imap_search($inbox,'ALL');
    if($emails) {
         $output = '';
         rsort($emails);
         foreach($emails as $email_number) {
          $overview = imap_fetch_overview($inbox,$email_number,0);
           $output.= 'Name:  '.$overview[0]->from.'</br>';
           $output.= 'Message ID:  '.$overview[0]->message_id.'</br>';
           $output.= 'Subject:  '.$overview[0]->subject.'</br>';
           $output.= 'UID:  '.$overview[0]->uid.'</br>';
           $output.= '<hr/>';
         }
          echo $output;
      }  
      imap_close($inbox);