(我就是用的这个,里面的example.php很不错的示例文件。要用到的php_imap.dll这个插件,我的这个帐号的资源里面有。将这个资源下载后放在c:/windows/system32的下面,并且还要找到c:/windows里面的php.ini这个文件,打开后找到:extension=php_imap.dll,将它前面的“;”去掉(意思就是说打开这个扩展。)。重启下apache。)
近日在网上看很多人问到PHP接收邮件的类,因为手上正好有这个类,所以放上去供大家下载。
receivemail是一个专门用来接收邮件的PHP类,支持POP3和IMAP等邮件协议。
可以接收邮件及邮件附件。
使用方法如下:
01.
include
(
"receivemail.class.php"
);
02.
// 创建一个 reciveMail 对象
03.
$obj
=
new
receiveMail(
'abc@abc.com'
,
'xxxxxx'
,
'abc@abc.com'
,
'pop.abc.com'
,
'pop3'
,
'110'
,false);
04.
// 连接到邮件服务器
05.
$obj
->connect();
//If connection fails give error message and exit
06.
// 读取未读邮件数
07.
$tot
=
$obj
->getTotalMails();
//Total Mails in Inbox Return integer value
08.
09.
echo
"收到$tot封邮件::<br>"
;
10.
for
(
$i
=
$tot
;
$i
> 0;
$i
--)
11.
{
12.
$head
=
$obj
->getHeaders(
$i
);
// 读取获取邮件头信息,返回数组 **数组键值为 (subject,to,toOth,toNameOth,from,fromName)
13.
echo
"主题 :: "
.
$head
[
'subject'
].
"<br>"
;
14.
echo
"收件人 :: "
.
$head
[
'to'
].
"<br>"
;
15.
echo
"抄送 :: "
.
$head
[
'toOth'
].
"<br>"
;
16.
echo
"发件人 :: "
.
$head
[
'from'
].
"<br>"
;
17.
echo
"发件人名称 :: "
.
$head
[
'fromName'
].
"<br>"
;
18.
echo
"<br><br>"
;
19.
echo
"<br>*******************************************************************************************<br>"
;
20.
echo
$obj
->getBody(
$i
);
// 邮件正文
21.
$str
=
$obj
->GetAttach(
$i
,
"./"
);
// 获取邮件附件,返回的文件名以逗号隔开。 例如. (mailid, Path to store file)
22.
$ar
=
explode
(
","
,
$str
);
23.
foreach
(
$ar
as
$key
=>
$value
)
24.
echo
(
$value
==
""
) ?
""
:
"Atteched File :: "
.
$value
.
"<br>"
;
25.
echo
"<br>------------------------------------------------------------------------------------------<br>"
;
26.
//$obj->deleteMails($i); // Delete Mail from Mail box
27.
}
28.
$obj
->close_mailbox();
//Close Mail Box
reciveMail类的构造函数如下:
function reciveMail($username,$password,$EmailAddress,$mailserver='localhost',$servertype='pop',$port='110')
参数说明:
$username : User name off the mail box
$password : Password of mailbox
$emailAddress :Email address of that mailbox some time the uname and email address are identical
$mailserver :Ip or name of the POP or IMAP mail server
$servertype :if this server is imap or pop default is pop
$port :Server port for pop or imap Default is 110 for pop and 143 for imap
以上就是有关PHP接收邮件类的介绍,receivemail.class.php这个类可以下载本文附件。
附件包含以下文件:
/example.php
/ReadMe.txt
/receivemail.class.php
同时欢迎大家留言。
receivemail.class.php需要imap模块支持。php_imap.dll可以通过附件php_imap.dll.rar下载。
附件下载:
mailreceived.zip 4.27KB
php_imap.dll.rar 265.46KB
另外给mailreceived.zip 4.27KB里面的两个文件(附加给那些下载不了的同志):
receivemail.class.php:
<?php
// Main ReciveMail Class File - Version 1.1 (02-06-2009)
/*
* File: recivemail.class.php
* Description: Reciving mail With Attechment
* Version: 1.1
* Created: 01-03-2006
* Modified: 02-06-2009
* Author: Mitul Koradia
* Email: mitulkoradia@gmail.com
* Cell : +91 9825273322
*/
/***************** Changes *********************
*
* 1) Added feature to retrive embedded attachment - Changes provided by. Antti <anttiantti83@gmail.com>
* 2) Added SSL Supported mailbox.
*
**************************************************/
class receiveMail
{
var $server='';
var $username='';
var $password='';
var $marubox='';
var $email='';
function receiveMail($username,$password,$EmailAddress,$mailserver='localhost',$servertype='pop',$port='110',$ssl = false) //Constructure
{
if($servertype=='imap')
{
if($port=='') $port='143';
$strConnect='{'.$mailserver.':'.$port. '}INBOX';
}
else
{
$strConnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}INBOX';
}
$this->server = $strConnect;
$this->username = $username;
$this->password = $password;
$this->email = $EmailAddress;
}
function connect() //Connect To the Mail Box
{
$this->marubox=@imap_open($this->server,$this->username,$this->password);
if(!$this->marubox)
{
echo "Error: Connecting to mail server";
exit;
}
}
function getHeaders($mid) // Get Header info
{
if(!$this->marubox)
return false;
$mail_header=imap_header($this->marubox,$mid);
$sender=$mail_header->from[0];
$sender_replyto=$mail_header->reply_to[0];
if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')
{
$mail_details=array(
'from'=>strtolower($sender->mailbox).'@'.$sender->host,
'fromName'=>$sender->personal,
'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
'toNameOth'=>$sender_replyto->personal,
'subject'=>$mail_header->subject,
'to'=>strtolower($mail_header->toaddress)
);
}
return $mail_details;
}
function get_mime_type(&$structure) //Get Mime type Internal Private Use
{
$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) //Get Part Of Message Internal Private Use
{
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) /* multipart */
{
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 getTotalMails() //Get Total Number off Unread Email In Mailbox
{
if(!$this->marubox)
return false;
$headers=imap_headers($this->marubox);
return count($headers);
}
function GetAttach($mid,$path) // Get Atteced File from Mail
{
if(!$this->marubox)
return false;
$struckture = imap_fetchstructure($this->marubox,$mid);
$ar="";
if($struckture->parts)
{
foreach($struckture->parts as $key => $value)
{
$enc=$struckture->parts[$key]->encoding;
if($struckture->parts[$key]->ifdparameters)
{
$name=$struckture->parts[$key]->dparameters[0]->value;
$message = imap_fetchbody($this->marubox,$mid,$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);
$ar=$ar.$name.",";
}
// Support for embedded attachments starts here
if($struckture->parts[$key]->parts)
{
foreach($struckture->parts[$key]->parts as $keyb => $valueb)
{
$enc=$struckture->parts[$key]->parts[$keyb]->encoding;
if($struckture->parts[$key]->parts[$keyb]->ifdparameters)
{
$name=$struckture->parts[$key]->parts[$keyb]->dparameters[0]->value;
$partnro = ($key+1).".".($keyb+1);
$message = imap_fetchbody($this->marubox,$mid,$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);
$ar=$ar.$name.",";
}
}
}
}
}
$ar=substr($ar,0,(strlen($ar)-1));
return $ar;
}
function getBody($mid) // Get Message Body
{
if(!$this->marubox)
return false;
$body = $this->get_part($this->marubox, $mid, "TEXT/HTML");
if ($body == "")
$body = $this->get_part($this->marubox, $mid, "TEXT/PLAIN");
if ($body == "") {
return "";
}
return $body;
}
function deleteMails($mid) // Delete That Mail
{
if(!$this->marubox)
return false;
imap_delete($this->marubox,$mid);
}
function close_mailbox() //Close Mail Box
{
if(!$this->marubox)
return false;
imap_close($this->marubox,CL_EXPUNGE);
}
}
?>
example.php:
<?
/*
* File: example.php
* Description: Received Mail Example
* Created: 01-03-2006
* Author: Mitul Koradia
* Email: mitulkoradia@gmail.com
* Cell : +91 9825273322
*/
include("receivemail.class.php");
// Creating a object of reciveMail Class
$obj= new receiveMail('abc@abc.com','xxxxxx','abc@abc.com','pop.abc.com','pop3','110',false);
//Connect to the Mail Box
$obj->connect(); //If connection fails give error message and exit
// Get Total Number of Unread Email in mail box
$tot=$obj->getTotalMails(); //Total Mails in Inbox Return integer value
echo "Total Mails:: $tot<br>";
for($i=$tot;$i>0;$i--)
{
$head=$obj->getHeaders($i); // Get Header Info Return Array Of Headers **Array Keys are (subject,to,toOth,toNameOth,from,fromName)
echo "Subjects :: ".$head['subject']."<br>";
echo "TO :: ".$head['to']."<br>";
echo "To Other :: ".$head['toOth']."<br>";
echo "ToName Other :: ".$head['toNameOth']."<br>";
echo "From :: ".$head['from']."<br>";
echo "FromName :: ".$head['fromName']."<br>";
echo "<br><br>";
echo "<br>*******************************************************************************************<BR>";
echo $obj->getBody($i); // Get Body Of Mail number Return String Get Mail id in interger
$str=$obj->GetAttach($i,"./"); // Get attached File from Mail Return name of file in comma separated string args. (mailid, Path to store file)
$ar=explode(",",$str);
foreach($ar as $key=>$value)
echo ($value=="")?"":"Atteched File :: ".$value."<br>";
echo "<br>------------------------------------------------------------------------------------------<BR>";
//$obj->deleteMails($i); // Delete Mail from Mail box
}
$obj->close_mailbox(); //Close Mail Box
?>