用PHP来发邮件,相信大家都不陌生,但读取收件箱的话,接触就少了,这次总结下自己的经验,希望可以帮助大家.
注意:1.PHP读取收件箱主要是利用imap扩展,所以在使用以下方法前,必须开启imap扩展模块的支持.
2.此方法支持中文,不会乱码,需要保持所有文件的编码的一致性
1.文件结构
2.邮件类 ./mailreceived/receiveMail.class.php
./mailreceived/receiveMail.class.php 文件内容如下:
<?php
// Main ReciveMail Class File - Version 1.0 (03-06-2015)
/*
* File: recivemail.class.php
* Description: Reciving mail With Attechment
* Version: 1.1
* Created: 03-06-2015
* Author: Sara Zhou
*/
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)
{
return false;
// echo "Error: Connecting to mail server";
// exit;
}
return true;
}
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')
{
$subject=$this->decode_mime($mail_header->subject);
$ccList=array();
foreach ($mail_header->cc as $k => $v)
{
$ccList[]=$v->mailbox.'@'.$v->host;
}
$toList=array();
foreach ($mail_header->to as $k => $v)
{
$toList[]=$v->mailbox.'@'.$v->host;
}
$ccList=implode(",", $ccList);
$toList=implode(",", $toList);
$mail_details=array(
'fromBy'=>strtolower($sender->mailbox).'@'.$sender->host,
'fromName'=>$this->decode_mime($sender->personal),
'ccList'=>$ccList,//strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
'toNameOth'