Location: PHPKode > scripts > getMail > getmail/getMail.php
<?php
    /*  $Id: getMail.php,v 0.1 2009/04/09 15:52:27
     *  Copyright (C) 2009 Yury Z Bizzon <hide@address.com>
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 2 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     *  
     */
class getMail
{
	protected $server = '';
	protected $username = '';
	protected $password = '';
	protected $mbox = '';

	function getMail($username,$password,$mailserver = 'localhost',$servertype = 'pop',$port = '110') 
	{
		if($servertype == 'imap')
		{
			if($port == '') $port='143'; 
			$strConnect = '{'.$mailserver.':'.$port. '/novalidate-cert}INBOX'; 
		}
		else
			$strConnect = '{'.$mailserver.':'.$port. '/pop3/novalidate-cert}INBOX'; 

		$this->server = $strConnect;
		$this->username = $username;
		$this->password = $password;
	}
	
	public function connect() //Connect To the Mail Box
	{
		if ($this->mbox = @imap_open($this->server,$this->username,$this->password)) 
		{
			imap_errors();
			return true;
		}
		return false;
	}
	
	public function getHeaders($mid) // Get Header info
	{
		$mail_details=array('date' => '', 'from' => '',	'subject' => '');
		$mail_header = imap_header($this->mbox,$mid);
		if (isset($mail_header->from[0])) $sender = $mail_header->from[0];
		if($sender)
		{
			if (isset($mail_header->date)) $mail_details['date'] = $mail_header->date;
			if (isset($sender->mailbox) && isset($sender->host)) $mail_details['from'] = strtolower($sender->mailbox).'@'.$sender->host;
			if (isset($mail_header->subject)) $mail_details['subject'] = iconv_mime_decode($mail_header->subject,0,'UTF-8');
		}
		return $mail_details;
	}

	public function getTotalMails() //Get Total Number off Unread Email In Mailbox
	{
		$headers = imap_headers($this->mbox);
		return count($headers);
	}

	public function getBody($mid) //Get  body
	{
		$body['text'] = $body['charset'] = '';
	    $s = imap_fetchstructure($this->mbox,$mid);
	    if (!isset($s->parts) || !$s->parts)
	        $body['text'] .= $this->getPart($this->mbox,$mid,$s,0,$body);  // no part-number, so pass 0
	    else 
			foreach ($s->parts as $partno0=>$p0) 
			$body['text'] .= $this->getPart($this->mbox,$mid,$p0,$partno0+1,$body);
		return $body;
	}

	private function getPart($mbox,$mid,$p,$partno,&$body) //Get parts body
	{
		$data = ($partno)?
	        imap_fetchbody($mbox,$mid,$partno):  // multipart
	        imap_body($mbox,$mid);  // not multipart

		if ($p->encoding == ENCQUOTEDPRINTABLE)
			$data = quoted_printable_decode($data);
		elseif ($p->encoding == ENCBASE64)
			$data = base64_decode($data);

	    $params = array();
	    if ($p->ifparameters)
	        foreach ($p->parameters as $x)
	            $params[ strtolower( $x->attribute ) ] = $x->value;

	    if ($p->ifdparameters)
			foreach ($p->dparameters as $x)
	            $params[ strtolower( $x->attribute ) ] = $x->value;
		
	    if (($p->type == TYPETEXT or $p->type == TYPEMULTIPART)  && $data && $p->encoding != ENCBINARY && $p->encoding != ENCOTHER) 
		{
			if (isset($params['charset']) && $params['charset'] != '') $body['charset'] = $params['charset'];
			elseif (isset ($params['charset']) && $params['charset'] == '' && $body['charset'] == '') $body['charset'] = 'ISO-8859-1';
			if (isset($data)) $body['text'] .= trim($data)."\n";
		}

	    // SUBPART RECURSION
	    if (isset($p->parts)) 
	        foreach ($p->parts as $partno0=>$p2)
	            $this->getPart($mbox,$mid,$p2,$partno.'.'.($partno0+1),$body);  // 1.2, 1.2.1, etc.
	}
	
	public function deleteMails($mid) // Delete That Mail
	{
		imap_delete($this->mbox,$mid);
	}
	
	public function close_mailbox() //Close Mail Box
	{
		imap_close($this->mbox,CL_EXPUNGE);
	}
	
	public function getError() //Get  error
	{
		return imap_last_error();
	}
}
?>
Return current item: getMail