Skip to content

Instantly share code, notes, and snippets.

@ivankristianto
Last active October 11, 2015 14:52
Show Gist options
  • Select an option

  • Save ivankristianto/86c2ce220362f23a36b1 to your computer and use it in GitHub Desktop.

Select an option

Save ivankristianto/86c2ce220362f23a36b1 to your computer and use it in GitHub Desktop.
PHP IMAP Helper function
<?php
function fetch_imap( $host, $username, $password, $port ){
$hostname = '{'.$host.':'.$port.'/imap/ssl/novalidate-cert}INBOX';
/* try to connect */
$inbox = imap_open( $hostname, $username, $password )
or die("Can't connect to '$hostname': " . var_dump(imap_errors()) );
// or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search( $inbox, 'UNSEEN' );
$inboxes = array();
if( $emails ){
/* 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, 2 );
$inboxes[] = array( 'overview' => $overview, 'message' => $message );
}
}
/* close the connection */
imap_close( $inbox );
return $inboxes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment