Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
// read_gmail.php
set_time_limit(4000);
// Connect to gmail
$url = '{imap.gmail.com:993/imap/ssl}INBOX';
$user = 'username_gmail_anda@gmail.com';
$pass = 'isi_dengan_password_anda';
// try to connect
$inbox = imap_open($url, $user, $pass) or die('Error When Connect: ' . imap_last_error());
// get unseen (unread) emails
$mails = imap_search($inbox,'UNSEEN');
$i = 1;
foreach($mails as $mail) {
// email header
$header = imap_headerinfo($inbox, $mail);
// email body
$body = imap_body($inbox, $mail);
echo $i . '.&nbsp;';
echo $header->fromaddress . '&nbsp;';
echo $header->subject . '&nbsp;';
echo $body;
echo '<br/>';
$i++;
// mark as seen
imap_setflag_full($inbox, $mail, '\\Seen \\Flagged');
}
// Disconnect from gmail
imap_expunge($inbox);
imap_close($inbox);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment