Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
// read_gmail2.php
require_once 'vendor/autoload.php';
use FastSimpleHTMLDom\Document;
set_time_limit(4000);
// Connect to gmail
$url = '{imap.gmail.com:993/imap/ssl}INBOX';
$user = 'your_email_id@gmail.com';
$pass = 'your_gmail_password';
// try to connect
$inbox = imap_open($url, $user, $pass) or die('Error When Connect: ' . imap_last_error());
// get seen (unread) emails
$mails = imap_search($inbox,'SEEN');
$i = 1;
foreach($mails as $mail) {
$fetchstructure = imap_fetchstructure($inbox, $mail);
if ($fetchstructure->type == 1) {//if email contail html
$body = imap_fetchbody($inbox, $mail, 2);
if ($i == 7) {//my last inbox
echo $body;
}
} elseif($fetchstructure->type == 0) {//if email contain only text
$body = imap_fetchbody($inbox, $mail, 1);
}
$i++;
}
// 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