• No categories
May
3
2013
Uncategorized

Get emails from inbox of email server

Hello everybody, here i will give you a simple php class to view your inbox emails from your Gmail account-

GetEmails();
class ManageEmails
{
function ManageEmails($emailServer, $emailUsername, $emailPassword)
{
$this->Mailbox=@imap_open($emailServer, $emailUsername, $emailPassword);

if(!$this->Mailbox)
{
echo "Error: Connecting to mail server";
exit;
}
}

function GetTotalEmailCount()
{
if(!$this->Mailbox)
return false;

$headers=imap_headers($this->Mailbox);
return count($headers);
}

function GetEmails()
{
if(!$this->Mailbox)
return false;

/* retrieve emails */
$emails = imap_search($this->Mailbox, 'ALL'); //UNSEEN for unread emails in inbox
rsort($emails); // sort emails in reverse order - newest first
$num = 0;
$this->EmailsArr = array();
foreach($emails as $emailIndex)
{
/* get information specific to this email */
$overview = imap_fetch_overview($this->Mailbox,$emailIndex,0);
$message = imap_fetchbody($this->Mailbox,$emailIndex,2); // message body

$this->EmailsArr[$num]["ReadUnread"] = $overview[0]->seen ? 'read' : 'unread';
$this->EmailsArr[$num]["Subject"] = $overview[0]->subject;
$this->EmailsArr[$num]["From"] = $overview[0]->from;
$this->EmailsArr[$num]["SentDate"] = $overview[0]->date;
// $this->EmailsArr[$num]["Message"] = $message;
$this->EmailsArr[$num]["Attachments"] = $this->GetAttachments($emailIndex, "./afiles/");

$num++;
}
$this->printArray($this->EmailsArr); // you can use this array to display emails however you like
}

function GetAttachments($emailIndex,$path)
{
if(!$this->Mailbox)
return false;

$emailObj = imap_fetchstructure($this->Mailbox,$emailIndex);

$filesStr="";
if($emailObj->parts)
{
foreach($emailObj->parts as $key => $value)
{
$enc=$emailObj->parts[$key]->encoding;
if(isset($emailObj->parts[$key]->ifdparameters) && $emailObj->parts[$key]->ifdparameters == 1)
{
$name=$emailObj->parts[$key]->dparameters[0]->value;
$message = imap_fetchbody($this->Mailbox,$emailIndex,$key+1);
if ($enc == 0)
$message = imap_8bit($message);
if ($enc == 1)
$message = imap_8bit ($message);
if ($enc == 2)
$message = imap_binary ($message);
if ($enc == 3)
$message = imap_base64 ($message);
if ($enc == 4)
$message = quoted_printable_decode($message);
if ($enc == 5)
$message = $message;
$fp=fopen($path.$name,"w");
fwrite($fp,$message);
fclose($fp);
$filesStr=$filesStr.$name.",";
}

if(isset($emailObj->parts[$key]->parts))
{
foreach($emailObj->parts[$key]->parts as $keyb => $valueb)
{
$enc=$emailObj->parts[$key]->parts[$keyb]->encoding;
if($emailObj->parts[$key]->parts[$keyb]->ifdparameters)
{
$name=$emailObj->parts[$key]->parts[$keyb]->dparameters[0]->value;
$partnro = ($key+1).".".($keyb+1);
$message = imap_fetchbody($this->Mailbox,$emailIndex,$partnro);
if ($enc == 0)
$message = imap_8bit($message);
if ($enc == 1)
$message = imap_8bit ($message);
if ($enc == 2)
$message = imap_binary ($message);
if ($enc == 3)
$message = imap_base64 ($message);
if ($enc == 4)
$message = quoted_printable_decode($message);
if ($enc == 5)
$message = $message;
$fp=fopen($path.$name,"w");
fwrite($fp,$message);
fclose($fp);
$filesStr=$filesStr.$name.",";
}
}
}
}
}
$filesStr=substr($filesStr,0,(strlen($filesStr)-1)); //remove extra comma
return $filesStr;
}

function DeleteEmail($emailIndex)
{
if(!$this->Mailbox)
return false;

imap_delete($this->Mailbox,$emailIndex);
}

function CloseMailbox()
{
if(!$this->Mailbox)
return false;

imap_close($this->Mailbox,CL_EXPUNGE);
}

function printArray($arr)
{
echo "<pre>";
print_r($arr);
echo "</pre>";
}
}

About the Author:

Comments are closed.