0

0

邮件接收类

php中文网

php中文网

发布时间:2016-06-06 19:31:46

|

1762人浏览过

|

来源于php中文网

原创

根据网上找到的代码自己修改了一点。 添加了所有编码统一转换为utf-8进行保存。 支持嵌入式附件 无 源码与演示: 源码出处 ?php/** * NOTICE OF LICENSE * * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT. * * DO NOT USE THIS FILE IN OTHER PLACE

根据网上找到的代码自己修改了一点。
添加了所有编码统一转换为utf-8进行保存。
支持嵌入式附件

源码与演示:源码出处

lavender.ai
lavender.ai

销售类电子邮件写作教练

下载
<?php
/**
  * NOTICE OF LICENSE
  *
  * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
  *
  * DO NOT USE THIS FILE IN OTHER PLACE.
  *
  * @category    EveBit_Library
  * @package     Application
  * @author      Chen Qiao 
 * @version     $$Id: Email.PHP 175 2011-03-26 09:52:16Z chen.qiao $$
  * @copyright   Copyright (c) 2011 Evebit Inc. China (http://www.evebit.com)
  */
 
/**
  * Email class
  *
  * get mail total count,get mail list,get mail content,get mail attach
  *
  * For a example, if you want to get some specified email list.
  * 
 * $mail = new Evebit_Mail();
  * $mail->mailConnect($host,$port,$user,$pass,'INBOX',$ssl);
  * $mail->mail_list('5,9:20');
  * 
 *
  * show the five,and nine to twenty mail.
  * 
 * $mail->mail_list('5,9:20');
  * 
 *
  * @docinfo
  *
  * @package     Application
  * @author      Chen Qiao 
 * @version     $$Id: Email.PHP 175 2011-03-26 09:52:16Z chen.qiao $$
  */

 class receiveMail {
     
    /**
      * @var resource $_connect
      */
     private $_connect;
     /**
      * @var object $_mailInfo
      */
     private $_mailInfo;
     /**
      * @var int $_total_count
      */
     private $_total_count;
     /**
      * @var array $_total_count
      */
    /**
      * __construct of the class
      */
     public function __construct() {

     }
     
    /**
      * Open an IMAP stream to a mailbox
      *
      * @param string $host
      * @param string $port
      * @param string $user
      * @param string $pass
      * @param string $folder
      * @param string $ssl
      * @param string $pop
      * @return resource|bool
      */
     public function connect($host,$port,$user,$pass,$folder="INBOX",$ssl,$pop=false) {
         if($pop){
             $ssl = $pop.'/'.$ssl.'/novalidate-cert/notls';
         }
         $this->_connect = imap_open("{"."$host:$port/$ssl"."}$folder",$user,$pass);
         if(!$this->_connect) {
             //Evebit_Application::getSession()->addError('cannot connect: ' . imap_last_error());
             return false;   
        }
         return $this->_connect;
     }
     
    /**
      * Get information about the current mailbox
      *
      * @return object|bool
      */
     public function mailInfo(){
         $this->_mailInfo = imap_mailboxmsginfo($this->_connection);
         if(!$this->_mailInfo) {
             echo "get mailInfo failed: " . imap_last_error();
             return false;
         }
         return $this->_mailInfo;
     }

    /**
      * Read an overview of the information in the headers of the given message
      *
      * @param string $msg_range
      * @return array
      */
     public function mail_list($msg_range='')
     {
         if ($msg_range) {
             $range=$msg_range;
         } else {
             $this->mail_total_count();
             $range = "1:".$this->_total_count;
         }
         $overview  = imap_fetch_overview($this->_connect,$range);
         foreach ($overview  as $val) {
             $mail_list[$val->msgno]=$val->message_id;
         }
         return $mail_list;
     }
     
    /**
      * get the total count of the current mailbox
      *
      * @return int
      */
     public function mail_total_count(){
         $check = imap_check($this->_connect);
            $this->_total_count = $check->Nmsgs;
            return $this->_total_count;
     }
     
    /**
      * Read the header of the message
      *
      * @param string $msg_count
      * @return array
      */
     public function mail_header($msg_count) {
         $mail_header = array();
         $header=imap_header($this->_connect,$msg_count);
         if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster') {
			$mail_header['name']=$this->mail_decode($header -> subject);
			$mail_header['mid']=$header -> message_id;
			$mail_header['to']=$this->contact_conv($header -> to);
			$mail_header['from']=$this->contact_conv($header -> from);
			$mail_header['cc']=$this->contact_conv($header -> cc);
			$mail_header['reply_to']=$this->contact_conv($header -> reply_to);
			$create_time=explode(",",$header -> date);
			if (strlen($create_time[0])>6){
				$create_time=$create_time[0];	
			}else{
				$create_time=$create_time[1];	
			}
			$mail_header['create_time']=strtotime($create_time); 
			$subject = $header -> subject;
			$charset = substr($subject, stripos($subject, "=?") + 2, stripos($subject, "?", 3)-2); 
			$content=$this->get_body($msg_count);
			//$mail_header['content']=$this->auto_charset($content, $charset, 'utf-8'); 
			$mail_header['content']=$content;
         }
         return $mail_header;
     }
     
  /**
      * decode the subject of chinese
      *
      * @param string $subject
      * @return sting
      */
 function mail_decode($str) {
	if (stripos($str, 'GBK?B')) {
		$arr_temp = explode(" ", $str); 
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?GBK?B?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp2 = $tmp2 . auto_charset(base64_decode($tmp), 'gb2312', 'utf-8');
		} 
		return $tmp2 ;
	}
	if (stripos($str, 'GBK?Q')) {
		$arr_temp = explode(" ", $str); 
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?GBK?B?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp2 = $tmp2 . auto_charset(base64_decode($tmp), 'gb2312', 'utf-8');
		} 
		return $tmp2 ;
	}
	if (stripos($str, 'utf-8?B')) {
		$arr_temp = explode(" ", $str); 
		// dump($arr_temp[0]);
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?utf-8?B?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp2 = $tmp2 . base64_decode($tmp);
		} 
		return $tmp2 ;
	}
	if (stripos($str, 'utf-8?Q')) {
		$arr_temp = explode(" ", $str); 
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?utf-8?Q?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp = str_ireplace('?', '', $tmp);
			$tmp2 = $tmp2 . quoted_printable_decode($tmp);
		} 
		return $tmp2 ;
	} 
	if (stripos($str, 'gb2312?B')) {
		$arr_temp = explode(" ", $str); 
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?gb2312?B?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp2 = $tmp2 . auto_charset(base64_decode($tmp), 'gb2312', 'utf-8');
		}
		return $tmp2 ;
	}
	if (stripos($str, 'gb2312?Q')) {
		$arr_temp = explode(" ", $str); 
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?gb2312?Q?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp = str_ireplace('?', '', $tmp);
			$tmp2 = $tmp2 . auto_charset(quoted_printable_decode($tmp),'gb2312','utf-8');
		}
		return $tmp2 ;
	}
	if (stripos($str, 'gb18030?B')) {
		$arr_temp = explode(" ", $str); 
		// dump($arr_temp[0]);
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?gb18030?B?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp2 = $tmp2 . auto_charset(base64_decode($tmp), 'gb2312', 'utf-8');
		} 
		return $tmp2 ;
	}
	if (stripos($str, 'gb18030?Q')) {
		$arr_temp = explode(" ", $str); 
		for ($i = 0;$i <= count($arr_temp);$i++) {
			$tmp = str_ireplace('=?gb18030?Q?', '', $arr_temp[$i]);
			$tmp = str_ireplace('=?', '', $tmp);
			$tmp = str_ireplace('?', '', $tmp);
			$tmp2 = $tmp2 . auto_charset(quoted_printable_decode($tmp),'gb18030','utf-8');
		}
		return $tmp2 ;
	}
	return $str;
}

function auto_charset($fContents,$from,$to){
    $from   =  strtoupper($from)=='UTF8'? 'utf-8':$from;
    $to       =  strtoupper($to)=='UTF8'? 'utf-8':$to;
    if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
        //如果编码相同或者非字符串标量则不转换
        return $fContents;
    }
    if(is_string($fContents) ) {
        if(function_exists('mb_convert_encoding')){
            return mb_convert_encoding ($fContents, $to, $from);
        }elseif(function_exists('iconv')){
            return iconv($from,$to,$fContents);
        }else{
            return $fContents;
        }
    }
    elseif(is_array($fContents)){
        foreach ( $fContents as $key => $val ) {
            $_key =     auto_charset($key,$from,$to);
            $fContents[$_key] = auto_charset($val,$from,$to);
            if($key != $_key )
                unset($fContents[$key]);
        }
        return $fContents;
    }
    else{
        return $fContents;
    }
}
    /**
      * Mark a message for deletion from current mailbox
      *
      * @param string $msg_count
      */
     public function delete($msg_count) {
         imap_delete($this->_connect,$msg_count);
     }
     
    /**
      * get attach of the message
      *
      * @param string $msg_count
      * @param string $path
      * @return array
      */
     public function get_attach($msg_count,$path) {
		if(!$this->_connect) 
			return false;
		$struckture = imap_fetchstructure($this->_connect,$msg_count);
		$ar="";
		if($struckture->parts)
        {
			foreach($struckture->parts as $key => $value)
			{
				$enc=$struckture->parts[$key]->encoding;
				if($struckture->parts[$key]->ifdparameters)
				{
					$name=$this->mail_decode($struckture->parts[$key]->dparameters[0]->value);
					$cid=$struckture->parts[$key]->id;
					$cid=substr($cid,1,strlen($cid)-2);
					$disposition=$struckture->parts[$key]->disposition;
					
					$name=$cid."_".$disposition."_".$name;
					$message = imap_fetchbody($this->_connect,$msg_count,$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.urlencode($name),"w");
					fwrite($fp,$message);
					fclose($fp);
					$ar=$ar.$name.",";
				}
				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=$this->mail_decode($struckture->parts[$key]->parts[$keyb]->dparameters[0]->value);
							$id=$struckture->parts[$key]->parts[$keyb]->id;
							$disposition=$struckture->parts[$key]->parts[$keyb]->disposition;							
							
							$name=$id."_".$disposition."_".$name;
							$partnro = ($key+1).".".($keyb+1);
							$message = imap_fetchbody($this->_connect,$msg_count,$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.urlencode($name),"w");
							fwrite($fp,$message);
							fclose($fp);
							$ar=$ar.$name.",";
						}
					}
				}	
			}
		}
		$ar=substr($ar,0,(strlen($ar)-1));
		return $ar;
	}
     
    /**
      * download the attach of the mail to localhost
      *
      * @param string $file_path
      * @param string $message
      * @param string $name
      */
     public function down_attach($file_path,$name,$message) {
         if(is_dir($file_path)) {
             $file_open = fopen($file_path.$name,"w");
         } else {
             mkdir($file_path,"0777",true);
         }
         fwrite($file_open,$message);
         fclose($file_open);
     }
 
     
    /**
      * get the body of the message
      *
      * @param string $msg_count
      * @return string
      */
     public function get_body($msg_count) {
         $body = $this->get_part($msg_count, "TEXT/HTML");
         if ($body == '') {
             $body = $this->get_part($msg_count, "TEXT/PLAIN");
         }
         if ($body == ''){
             return '';
         }
         return $this->mail_decode($body);
     }
     
    /**
      * Read the structure of a particular message and fetch a particular
      * section of the body of the message
      *
      * @param string $msg_count
      * @param string $mime_type
      * @param object $structure
      * @param string $part_no
      * @return string|bool
      */
     private function get_part($msg_count, $mime_type, $structure = false, $part_no = false) {
         if(!$structure) {
             $structure = imap_fetchstructure($this->_connect, $msg_count);
         }
         if($structure) {
             if($mime_type == $this->get_mime_type($structure)) {
                 if(!$part_no) {
                     $part_no = "1";
                 }
                 $from_encoding = $structure->parameters[0]->value;
                 $text = imap_fetchbody($this->_connect, $msg_count, $part_no);
                 if($structure->encoding == 3) {
                     $text =  imap_base64($text);
                 } else if($structure->encoding == 4) {
                     $text =  imap_qprint($text);
                 }
                 $text = mb_convert_encoding($text,'utf-8',$from_encoding);
                 return $text;
             }
             if($structure->type == 1) {
                 while(list($index, $sub_structure) = each($structure->parts)) {
                     if($part_no) {
                         $prefix = $part_no . '.';
                     }
                     $data = $this->get_part($msg_count, $mime_type, $sub_structure, $prefix . ($index + 1));
                     if($data){
                         return $data;
                     }
                 }
             }
         }
         return false;
     }
     
    /**
      * get the subtype and type of the message structure
      *
      * @param object $structure
      */
     private function get_mime_type($structure) {
         $mime_type = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
         if($structure->subtype) {
             return $mime_type[(int) $structure->type] . '/' . $structure->subtype;
         }
         return "TEXT/PLAIN";
     }
     
    /**
      * put the message from unread to read
      *
      * @param string $msg_count
      * @return bool
      */
     public function mail_read($msg_count) {
         $status = imap_setflag_full($this->_connect, $msg_count , "//Seen");
         return $status;
     }
     
    /**
      * Close an IMAP stream
      */
     public    function close_mail() {
         imap_close($this->_connect,CL_EXPUNGE);
     }

	 function contact_conv($contact){
		foreach($contact as $vo) {
			if (isset($vo -> personal)) {
				$tmp = $tmp.$this->mail_decode($vo -> personal)."|".$vo -> mailbox . '@' . $vo -> host.';';
			} else {
				$tmp = $tmp.$this->mail_decode($vo -> mailbox)."|".$vo -> mailbox . '@' . $vo -> host.';';
			} 
			return $tmp;
		}
	}     
}
 
?>

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

705

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

233

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

117

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

22

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

61

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

30

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

15

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

669

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

58

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Git 教程
Git 教程

共21课时 | 3.7万人学习

Django 教程
Django 教程

共28课时 | 4.4万人学习

React 教程
React 教程

共58课时 | 5.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号