目录
邮件头的内容:
邮件正文 :
首页 后端开发 php教程 用PHP实现POP3邮件的解码(三)

用PHP实现POP3邮件的解码(三)

May 19, 2016 pm 02:05 PM

实现 MIME 解码的类

该类实现解码的方法是 decode($head=null,$body=null,$content_num=-1),为了处理上的方便,要求输入的是两个字符数组,在我们的上篇中,所用到的POP类所收取得到的就是两个这样的数组,一个是邮件头内容,一个是邮件的正文内容。限于篇幅,不对其做详细的说明,其实现思想跟本文上篇中所介绍的POP类类似。请参考其中的注释。

该类中用到了大量的正则表达式的操作,对此不熟悉的读者,请参考正则表达式的有关资料。

class decode_mail
  {
  var $from_name;var $to_name;var $mail_time;var $from_mail;var $to_mail;
  var $reply_to;var $cc_to;var $subject;

// 解码后的邮件头部分的信息:
  var $body;

// 解码后得到的正文数据,为一个数组。
  var $body_type; // 正文类型
  var $tem_num=0;
  var $get_content_num=0;
  var $body_temp=array();
  var $body_code_type;
  var $boundary;

// 以上是一些方法中用到的一些全局性的临时变量,由于 php不能做到良好的封装,所以只能放在这里定义
  var $err_str; // 错误信息
  var $debug=0; // 调试标记
  var $month_num=array("Jan"=>1,"Feb"=>2,"Mar"=>3,"APR"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,
  "Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12); // 把英文月份转换成数字表示的月份

function decode($head=null,$body=null,$content_num=-1) // 调用的主方法,$head 与 $body 是两个数组,$content_num 表示的是当正文有多个部分的时候,只取出指定部分的内容以提高效率,默认为 -1 ,表示解码全部内容,如果解码成功,该 方法返回 true
  {
   if (!$head and !$body)
   {
   $this->err_str="没有指定邮件的头与内容!!";
   return false;
   }
  if (gettype($head)=="array")
   {
   $have_decode=true;
   $this->decode_head($head);
   }
  if (gettype($body)=="array")
   {
   $this->get_content_num=$content_num;
   $this->body_temp=$body;
   $have_decode=true;
   $this->decode_body();
   unset($this->body_temp);
   }
  if (!$have_decode)
   {
   $this->err_str="传递的参数不对,用法:new decode_mail(head,body) 两个参数都是数组";
   return false;
   }
  }
  function decode_head($head) // 邮件头内容 的解码,取出邮件头中有意义的内容
  {
   $i=0;
   $this->from_name=$this->to_name=$this->mail_time=$this->from_mail=$this->
   to_mail=$this->reply_to=$this->cc_to=$this->subject="";
   $this->body_type=$Sthis->boundary=$this->body_code_type="";
   while ($head[$i])
   {
   if (strpos($head[$i],"=?"))
   $head[$i]=$this->decode_mime($head[$i]);  //如果有编码的内容,则进行解码,解码函数是上文所介绍的 decode_mime()
   $pos=strpos($head[$i],":");
   $summ=substr($head[$i],0,$pos);
   $content=substr($head[$i],$pos+1);  //将邮件头信息的标识与内容分开
   if ($this->debug) echo $summ.":----:".$content."
";
   switch (strtoupper($summ))
   {
   case "FROM": // 发件人地址及姓名(可能没有姓名,只有地址信息)
   if ($left_tag_pos=strpos($content,"   {
   $mail_lenth=strrpos($content,">")-$left_tag_pos-1;
   $this->from_name=substr($content,0,$left_tag_pos);
   $this->from_mail=substr($content,$left_tag_pos+1,$mail_lenth);
   if (trim($this->from_name)=="") $this->from_name=$this->from_mail;
   else
   if (ereg("[\"|\']([^\'\"]+)[\'|\"]",$this->from_name,$reg))
   $this->from_name=$reg[1];
   }
   else
   {
   $this->from_name=$content;
   $this->from_mail=$content;
   //没有发件人的邮件地址
   }
   break;
   case "TO": //收件人地址及姓名(可能 没有姓名)
   if ($left_tag_pos=strpos($content,"   {
   $mail_lenth=strrpos($content,">")-$left_tag_pos-1;
   $this->to_name=substr($content,0,$left_tag_pos);
   $this->to_mail=substr($content,$left_tag_pos+1,$mail_lenth);
   if (trim($this->to_name)=="") $this->to_name=$this->to_mail;
   else
   if (ereg("[\"|\']([^\'\"]+)[\'|\"]",$this->to_name,$reg))
   $this->to_name=$reg[1];
   }
   else
   {
   $this->to_name=$content;
   $this->to_mail=$content;
   //没有分开收件人的邮件地址
   }
   break;

case "DATE" : //发送日期,为了处理方便,这里返回的是一个 Unix 时间戳,可以用 date("Y-m-d",$this->mail_time) 来得到一般格式的日期

$content=trim($content);
   $day=strtok($content," ");
   $day=substr($day,0,strlen($day)-1);
   $date=strtok(" ");
   $month=$this->month_num[strtok(" ")];
   $year=strtok(" ");
   $time=strtok(" ");
   $time=split(":",$time);
   $this->mail_time=mktime($time[0],$time[1],$time[2],$month,$date,$year);
   break;
   case "SUBJECT":  //邮件主题
   $this->subject=$content;
   break;
   case "REPLY_TO": // 回复地址(可能没有)
   if (ereg("]+)>",$content,$reg))
   $this->reply_to=$reg[1];
   else $this->reply_to=$content;
   break;
   case "CONTENT-TYPE": // 整个邮件的 Content类型, eregi("([^;]*);",$content,$reg);
   $this->body_type=trim($reg[1]);
   if (eregi("multipart",$content)) // 如果是 multipart 类型,取得 分隔符
   {
   while (!eregi('boundary=\"(.*)\"',$head[$i],$reg) and $head[$i])
   $i++;
   $this->boundary=$reg[1];
   }
   else //对于一般的正文类型,直接取得其编码方法
   {
   while (!eregi("charset=[\"|\'](.*)[\'|\"]",$head[$i],$reg))
   $i++;
   $this->body_char_set=$reg[1];
   while (!eregi("Content-Transfer-Encoding:(.*)",$head[$i],$reg))
   $i++;
   $this->body_code_type=trim($reg[1]);
   }
   break;
   case "CC": //抄送到。。
   if (ereg("]+)>",$content,$reg))
   $this->cc_to=$reg[1];
   else
   $this->cc_to=$content;
   default:
   break;
   } // end switch
  
   $i++;
   } // end while
  
   if (trim($this->reply_to)=="")  //如果没有指定回复地址,则回复地址为发送人地址
   $this->reply_to=$this->from_mail;
  }// end function define

function decode_body() //正文的解码,其中用到了不少邮件头解码所得来的信息
  {
  $i=0;
  if (!eregi("multipart",$this->body_type)) // 如果不是复合类型,可以直接解码
   {
   $tem_body=implode($this->body_temp,"\r\n");
   switch (strtolower($this->body_code_type)) // body_code_type ,正文的编码方式,由邮件头信息中取得
   {case "base64":
   $tem_body=base64_decode($tem_body);
   break;

case "quoted-printable":
   $tem_body=quoted_printable_decode($tem_body);
   break;
   }

$this->tem_num=0;
   $this->body=array();
   $this->body[$this->tem_num][content_id]="";
   $this->body[$this->tem_num][type]=$this->body_type;
   switch (strtolower($this->body_type))

{
   case "text/html":
   $this->body[$this->tem_num][name]="超文本正文";
   break;
   case "text/plain":
   $this->body[$this->tem_num][name]="文本正文";
   break;
   default:
   $this->body[$this->tem_num][name]="未知正文";
   }
  
   $this->body[$this->tem_num][size]=strlen($tem_body);
   $this->body[$this->tem_num][content]=$tem_body;
   unset($tem_body);
   }
   else // 如果是复合类型的
   {
   $this->body=array();
   $this->tem_num=0;
   $this->decode_mult($this->body_type,$this->boundary,0);  //调用复合类型的解码方法
   }
  }

function decode_mult($type,$boundary,$begin_row) // 该方法用递归的方法实现 复合类型邮件正文的解码,邮件源文件取自于 body_temp 数组,调用时给出该复合类型的类型、分隔符及 在 body_temp 数组中的开始指针

{
  $i=$begin_row;
  $lines=count($this->body_temp);
  while ($i   {
   while (!eregi($boundary,$this->body_temp[$i]))//找到一个开始标识
   $i++;
   if (eregi($boundary."--",$this->body_temp[$i]))
   {
   return $i;
   }

while (!eregi("Content-Type:([^;]*);",$this->body_temp[$i],$reg ) and $this->body_temp[$i])
   $i++;
   $sub_type=trim($reg[1]); // 取得这一个部分的 类型是milt or text ....
   if (eregi("multipart",$sub_type))// 该子部分又是有多个部分的;
   {
   while (!eregi('boundary=\"([^\"]*)\"',$this->body_temp[$i],$reg) and $this->body_temp[$i])
   $i++;
   $sub_boundary=$reg[1];// 子部分的分隔符;
   $i++;
   $last_row=$this->decode_mult($sub_type,$sub_boundary,$i);
   $i=$last_row;
   }
   else
   {
   $comm="";
   while (trim($this->body_temp[$i])!="")
   {
   if (strpos($this->body_temp[$i],"=?"))
   $this->body_temp[$i]=$this->decode_mime($this->body_temp[$i]);
   if (eregi("Content-Transfer-Encoding:(.*)",$this->body_temp[$i],$reg))
   $code_type=strtolower(trim($reg[1])); // 编码方式
   $comm.=$this->body_temp[$i]."\r\n";
   $i++;
   } // comm 是编码的说明部分

if (eregi('name=[\"]([^\"]*)[\"]',$comm,$reg))
   $name=$reg[1];

if (eregi("Content-Disposition:(.*);",$comm,$reg))
   $disp=$reg[1];

if (eregi("charset=[\"|\'](.*)[\'|\"]",$comm,$reg))
   $char_set=$reg[1];

if (eregi("Content-ID:[ ]*\",$comm,$reg)) // 图片的标识符。
   $content_id=$reg[1];

$this->body[$this->tem_num][type]=$sub_type;
   $this->body[$this->tem_num][content_id]=$content_id;
   $this->body[$this->tem_num][char_set]=$char_set;
   if ($name)
   $this->body[$this->tem_num][name]=$name;
   else
   switch (strtolower($sub_type))
   {
   case "text/html":
   $this->body[$this->tem_num][name]="超文本正文";
   break;

case "text/plain":
   $this->body[$this->tem_num][name]="文本正文";
   break;

default:
   $this->body[$this->tem_num][name]="未知正文";
   }  
  
   // 下一行开始取回正文
   if ($this->get_content_num==-1 or $this->get_content_num==$this->tem_num) // 判断这个部分是否是需要的。-1 表示全部
   {
   $content="";
   while (!ereg($boundary,$this->body_temp[$i]))
   {
   //$content[]=$this->body_temp[$i];
   $content.=$this->body_temp[$i]."\r\n";
   $i++;
   }

//$content=implode("\r\n",$content);
   switch ($code_type)
   {
   case "base64":
   $content=base64_decode($content);
   break;

case "quoted-printable":
   $content=str_replace("\n","\r\n",quoted_printable_decode($content));
   break;
   }

$this->body[$this->tem_num][size]=strlen($content);
   $this->body[$this->tem_num][content]=$content;
   }
   else
   {
   while (!ereg($boundary,$this->body_temp[$i]))
   $i++;
   }
   $this->tem_num++;
   }
   // end else
  } // end while;
  } // end function define   

function decode_mime($string) {
  //decode_mime 已在上文中给出,这里略过。
  }
  } // end class define

在这里要特别说明一点的是html正文里所用图片的解码。发送html格式的正文时,都会碰到图片如何传送的问题。图片在 html 文档里是一个用PHP实现POP3邮件的解码(三)的标签,关键是这个源文件从何来的。很多邮件的处理方法是用一个绝对的 url 标识,就是在邮件的html正文里用用PHP实现POP3邮件的解码(三)之类的标签,这样,在阅读邮件时,邮件阅读器(通常是用内嵌的浏览器)会自动从网上下载图片,但是如果邮件收下来之后,与 Internet 的连接断了,图片也就不能正常显示。

所以更好的方法是把图片放在邮件中一起发送出去。在 MIME 编码里,描述图片与正文的关系,除了上面所提到的multipart/related MIME头信息之外,还用到了一个 Content-ID: 的属性来使图片与 html 正文之间建立关系。html 文档中的图片在编码时,其MIME头中加入一个 Content-ID:122223443556dsdf@ntsever 之类的属性,122223443556dsdf@ntsever是一个唯一的标识,在 html 文档里,用PHP实现POP3邮件的解码(三)标签被修改成用PHP实现POP3邮件的解码(三),在解码的时候,实际上,还需要把 html 正文中的这些用PHP实现POP3邮件的解码(三)标签进行修改,使之指向解码后的图片的具体路径。但是考虑到具体的解码程序中对图片会有不同的处理,所以在这个解码的类中,没有对 hmtl 正文中的用PHP实现POP3邮件的解码(三)标签进行修改。所以在实际使用这个类时,对于有图片的 html 正文,还需要一定的处理。正文中的图片,可以用临时文件来保存,也可以用数据库来保存。

现在我们已经介绍了POP3 收取邮件并进行 MIME 解码的原理。下面给出一个使用这两个类的一段小程序:


  include("pop3.inc.php");
  include("mime.inc.php");
  $host="pop.china.com";
  $user="boss_ch";
  $pass="mypassWord";
  $rec=new pop3($host,110,2);
  $decoder=new decode_mail();

if (!$rec->open()) die($rec->err_str);

if (!$rec->login($user,$pass)) die($rec->err_str);

if (!$rec->stat()) die($rec->err_str);
  echo "共有".$rec->messages."封信件,共".$rec->size."字节大小
";

if ($rec->messages>0)
   {
   if (!$rec->listmail()) die($rec->err_str);
   echo "以下是信件内容:
";
   for ($i=1;$imail_list);$i++)
   {
   echo "信件".$rec->mail_list[$i][num].",大小:".$rec->mail_list[$i][size]."
";
   $rec->getmail($rec->mail_list[$i][num]);
   $decoder->decode($rec->head,$rec->body);
   echo "

邮件头的内容:


";
   echo $decoder->from_name."(".$decoder->from_mail.") 于".date("Y-m-d H:i:s",$decoder->mail_time)." 发给".$decoder->to_name."(".$decoder->to_mail.")";
   echo "\n
抄送:";

if ($decoder->cc_to) echo $decoder->cc_to;else echo "无";
   echo "\n
主题:".$decoder->subject;

echo "\n
回复到:".$decoder->reply_to;  
   echo "

邮件正文 :


";
   echo "正文类型:".$decoder->body_type;
   echo "
正文各内容:";
   for ($j=0;$jbody);$j++)
   {
   echo "\n
类型:".$decoder->body[$j][type];
   echo "\n
名称:".$decoder->body[$j][name];
   echo "\n
大小:".$decoder->body[$j][size];
   echo "\n
content_id:".$decoder->body[$j][content_id];
   echo "\n
正文字符集".$decoder->body[$j][char_set];
   echo "
";<br>   echo "正文内容:".$decoder->body[$j][content];<br>   echo "
登录后复制
";
   }
  $rec->dele($i);
   }
  }

$rec->close();
  ?>

如有想要取得完整源代码的朋友,请与本人联系: boss_ch@netease.com

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1654
14
CakePHP 教程
1413
52
Laravel 教程
1306
25
PHP教程
1252
29
C# 教程
1225
24
在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

会话如何劫持工作,如何在PHP中减轻它? 会话如何劫持工作,如何在PHP中减轻它? Apr 06, 2025 am 12:02 AM

会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

什么是REST API设计原理? 什么是REST API设计原理? Apr 04, 2025 am 12:01 AM

RESTAPI设计原则包括资源定义、URI设计、HTTP方法使用、状态码使用、版本控制和HATEOAS。1.资源应使用名词表示并保持层次结构。2.HTTP方法应符合其语义,如GET用于获取资源。3.状态码应正确使用,如404表示资源不存在。4.版本控制可通过URI或头部实现。5.HATEOAS通过响应中的链接引导客户端操作。

您如何在PHP中有效处理异常(尝试,捕捉,最后,投掷)? 您如何在PHP中有效处理异常(尝试,捕捉,最后,投掷)? Apr 05, 2025 am 12:03 AM

在PHP中,异常处理通过try,catch,finally,和throw关键字实现。1)try块包围可能抛出异常的代码;2)catch块处理异常;3)finally块确保代码始终执行;4)throw用于手动抛出异常。这些机制帮助提升代码的健壮性和可维护性。

PHP中的匿名类是什么?您何时可以使用它们? PHP中的匿名类是什么?您何时可以使用它们? Apr 04, 2025 am 12:02 AM

匿名类在PHP中的主要作用是创建一次性使用的对象。1.匿名类允许在代码中直接定义没有名字的类,适用于临时需求。2.它们可以继承类或实现接口,增加灵活性。3.使用时需注意性能和代码可读性,避免重复定义相同的匿名类。

包括,require,incement_once,require_once之间有什么区别? 包括,require,incement_once,require_once之间有什么区别? Apr 05, 2025 am 12:07 AM

在PHP中,include,require,include_once,require_once的区别在于:1)include产生警告并继续执行,2)require产生致命错误并停止执行,3)include_once和require_once防止重复包含。这些函数的选择取决于文件的重要性和是否需要防止重复包含,合理使用可以提高代码的可读性和可维护性。

说明PHP中的不同错误类型(注意,警告,致命错误,解析错误)。 说明PHP中的不同错误类型(注意,警告,致命错误,解析错误)。 Apr 08, 2025 am 12:03 AM

PHP中有四种主要错误类型:1.Notice:最轻微,不会中断程序,如访问未定义变量;2.Warning:比Notice严重,不会终止程序,如包含不存在文件;3.FatalError:最严重,会终止程序,如调用不存在函数;4.ParseError:语法错误,会阻止程序执行,如忘记添加结束标签。

PHP和Python:比较两种流行的编程语言 PHP和Python:比较两种流行的编程语言 Apr 14, 2025 am 12:13 AM

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

See all articles