PHP收邮件receiveMail

本文介绍如何使用PHP来读取收件箱,主要依赖imap扩展。确保开启imap扩展模块,并保持文件编码一致,以避免乱码。提供了一个包含邮件类和控制层的文件结构示例,通过访问特定URL即可查看效果。
摘要由CSDN通过智能技术生成

用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'
receivemail是一个专门用来接邮件PHP类,支持POP3和IMAP等邮件协议。 可以接邮件邮件附件。 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封邮件::"; 10 for($i = $tot; $i > 0; $i--) 11 { 12 $head = $obj->getHeaders($i); // 读取获取邮件头信息,返回数组 **数组键值为 (subject,to,toOth,toNameOth,from,fromName) 13 echo "主题 :: ".$head['subject'].""; 14 echo "件人 :: ".$head['to'].""; 15 echo "抄送 :: ".$head['toOth'].""; 16 echo "发件人 :: ".$head['from'].""; 17 echo "发件人名称 :: ".$head['fromName'].""; 18 echo ""; 19 echo "*******************************************************************************************"; 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 . ""; 25 echo "------------------------------------------------------------------------------------------"; 26 //$obj->deleteMails($i); // Delete Mail from Mail box 27 } 28 $obj->close_mailbox(); //Close Mail Box
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值