کلاس دریافت ایمیل با php

ahmad_m1234

Active Member
سلام
در این پست کلاس دریافت ایمیل گذاشتم که از imap و pop3 پشتیبانی می کند
طریقه کار کردن با آن خیلی ساده هست

کلاس :

PHP:
<?php
/*
*	Author : ahmad nemati
*	Email : [email protected]
*	Mob : 09171687267
*	blog : http://anemati.ir
*
*	email class
*	get more information : http://www.php.net/manual/en/book.imap.php
*	
*
*/
class email
{
	var $useremail;
	var $passemail;
	var $addressemail;
	var $portemail;
	var $ssl = false;
	private $mbox;
	
	function imapopen() 
	{
		$this->mbox = @imap_open("{".$this->addressemail.($this->ssl ? "/ssl" : "").":".$this->portemail."}INBOX",$this->useremail, $this->passemail);
		if(!$this->mbox)
		{
			echo "Can not Connect";
			exit;
		}
	}
	function imapclose()
	{
		if(!$this->mbox)
			return false;
			
		@imap_close($this->mbox);	
	}
	function imapcount()
	{
		if(!$this->mbox)
			return false;

		$headers=imap_headers($this->mbox);
		return count($headers);
	}
	function get_mime_type(&$structure) 
	{ 
		$primary_mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER"); 
		
		if($structure->subtype) { 
			return $primary_mime_type[(int) $structure->type] . '/' . $structure->subtype; 
		} 
		return "TEXT/PLAIN"; 
	} 
	function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) 
	{ 
		$prefix = '';
		if(!$structure) { 
			$structure = imap_fetchstructure($stream, $msg_number); 
		} 
		if($structure) { 
			if($mime_type == $this->get_mime_type($structure))
			{ 
				if(!$part_number) 
				{ 
					$part_number = "1"; 
				} 
				$text = imap_fetchbody($stream, $msg_number, $part_number); 				
				if($structure->encoding == 3) 
				{ 
					return imap_base64($text); 
				} 
				else if($structure->encoding == 4) 
				{ 
					return imap_qprint($text); 
				} 
				else
				{ 
					return $text; 
				} 
			} 
			if($structure->type == 1) 
			{ 
				while(list($index, $sub_structure) = each($structure->parts))
				{ 
					if($part_number)
					{ 
						$prefix = $part_number . '.'; 
					} 
					$data = $this->get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1)); 
					if($data)
					{ 
						return $data; 
					} 
				} 
			} 
		} 
		return false; 
	}
	function imapbody($nummsg)
	{
		if(!$this->mbox)
			return false;

		$body = $this->get_part($this->mbox, $nummsg, "TEXT/HTML");
		if ($body == "")
			$body = $this->get_part($this->mbox, $nummsg, "TEXT/PLAIN");
		if ($body == "") { 
			return "";
		}
		return $body;
	}
	function imapheader($nummsg)
	{
		if(!$this->mbox)
			return false;

		$mail_header=imap_headerinfo($this->mbox,$nummsg);
		$sender=$mail_header->from[0];
		$sender_replyto=$mail_header->reply_to[0];
		if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')
		{
			if(isset($sender_replyto->mailbox))
			{
				$mailbox = strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host;
			}else{
				$mailbox = '';			
			}
			if(isset($sender_replyto->personal))
			{
				$mailpersonal = iconv_mime_decode($sender_replyto->personal,0,"UTF-8");
			}else{
				$mailpersonal = '';			
			}
			if(isset($sender->personal))
			{
				$mailsenderpersonal = iconv_mime_decode($sender->personal,0,"UTF-8");
			}else{
				$mailsenderpersonal = '';			
			}
			if(isset($mail_header->subject))
			{
				$mailsubject = iconv_mime_decode($mail_header->subject,0,"UTF-8");
			}else{
				$mailsubject = '';			
			}			
			$mail_details=array(
					'from'=>strtolower($sender->mailbox).'@'.$sender->host,
					'fromName'=>$mailsenderpersonal,
					'reply_to'=>$mailbox,
					'reply_toname'=>$mailpersonal,
					'subject'=>$mailsubject,
					'to'=>strtolower($mail_header->toaddress)
				);
		}
		return $mail_details;
	} 
}

?>

کد امتحان :

PHP:
<?php
set_time_limit(0);
require_once('cls_email.php');
$imap = new email;
$imap->addressemail = "server";
$imap->portemail = "port";
$imap->ssl = true;
$imap->useremail = "username";
$imap->passemail = "password";
$imap->imapopen();

echo "<h1>Mailboxes</h1>\n";

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Imap Mail</title>
</head>
<body>
 
<?php 

$tot=$imap->imapcount();
for($i=$tot;$i>0;$i--)
{
	$head=$imap->imapheader($i);
	if($head['subject'] != ''){echo "Subjects :: ".$head['subject']."<br>";}else{echo "Subjects :: No Subject"."<br>";}
	echo "TO :: ".$head['to']."<br>";
	if($head['reply_to'] != ''){echo "Reply To :: ".$head['reply_to']."<br>";}
	if($head['reply_toname'] != ''){echo "Repty To Name :: ".$head['reply_toname']."<br>";}
	echo "From :: ".$head['from']."<br>";
	if($head['fromName'] != ''){echo "FromName :: ".$head['fromName']."<br>";}else{echo "FromName :: No From Name"."<br>";}
	echo "<br><br>";
	echo "<br>*******************************************************************************************<BR>";
	echo $imap->imapbody($i);
}
?>

</body>
<?php
$imap->imapclose(); 

?>
</html>

با سرویس جیمیل امتحان کردم کاملا جواب گرفتم
تنظیمات جیمیل برای imap :
آدرس : imap.gmail.com/imap
پورت : 993
ssl : true
توجه : نام کاربری شما باید به همراه نام هاست باشد مانند [email protected]
 
آخرین ویرایش:

iMacs

Member
عنوان تاپيك نوشته كلاس نه شرح كد دوست عزيز براي اينكه بتونيد اين كدها رو بفهميد بايد php حرفه اي باشيد و برنامه نويسي شما سطح بالاتري باشه وگرنه فقط گيج ميشيد و بس
 

جدیدترین ارسال ها

بالا