• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

发送MIME邮件~~(其实我对他的格式一窍不通,是看着OUTLOOK中的源码写的)

发布: 2007-7-01 15:06 | 作者: admin | 来源: | 查看: 19次 | 进入软件测试论坛讨论

领测软件测试网 <?
class mime_mail
{
var $boundary0="----=_NextPart_000_00EE_01C07425.958FDFE0";//三条分界,0是附件,2是HTML,1是要附加的图片
var $boundary1="----=_NextPart_001_00EF_01C07425.958FDFE0";
var $boundary2="----=_NextPart_002_00F0_01C07425.958FDFE0";
var $body;
function get_mail_header()//邮件头
{
return  $this->mail_header="MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary=\"$this->boundary0\"

This is a multi-part message in MIME format.

--$this->boundary0
Content-Type: multipart/related;
boundary=\"$this->boundary1\";
type=\"multipart/alternative\"


--$this->boundary1
Content-Type: multipart/alternative;
boundary=\"$this->boundary2\"
";
}

function str_row($str,$n)//把字符串分割成n行,OUTLOOK中好象必须把BASE64割成<77字符/行才行
{
    $i=0;
    if($n<1)return $str;
    while($i<strlen($str))
    {
      if(strlen($str)>$n)
      {
        $new_str.=substr($str,$i,$n).chr(13).chr(10);
        $i+=$n;
      }
      else
      {
        $new_str.=$str;
        $i=strlen($str);
      }
    }
  return $new_str;
}

function loadfromfile($filename)//读出文件并编码
{
   $fp=fopen($filename,"rb");
   $c=fread($fp,filesize($filename));
   fclose($fp);
   return $this->str_row(base64_encode($c),77);
}

function file_header($filename)//给出文件头
{
   switch(substr($filename,strlen($filename)-3,3))
   {
     case "gif":
     return "Content-Type: image/gif\n name=\"$filename\"";
     break;  
     case "jpg";
     return "Content-Type: image/jpeg\n name=\"$filename\"";
     break;
     case "txt";
     return "Content-Type: text/plain\n name=\"$filename\"";
     break;
     default:
     return "Content-Type: application/octetstream\n name=\"$filename\"";
   }
}

function mail_body($str)//HTML部分
{
  $this->body.="--".$this->boundary2."\nContent-Type: text/html;\n charset=\"gb2312\"\n\n".$str."\n\n--".$this->boundary2."--\n\n";
}

function mail_picture($filename)//附件的图片,HTML中用到的这里必须添加,否则不能在本地显示
{
  if($filename==null)
  {
   $this->body.="--".$this->boundary1."--\n\n";
  }
  for($i=0;$i<count($filename);$i++)
  {
    $cid=md5(uniqid(time()));
      $this->body=str_replace($filename[$i],"cid:$cid",$this->body);
  $this->body.="--".$this->boundary1."\n";
  $this->body.=$this->file_header($filename[$i])."\nContent-Transfer-Encoding: base64\n";
  $this->body.="Content-ID: <$cid>\n\n";
  $this->body.=$this->loadfromfile($filename[$i]);

  }
    $this->body.="\n\n--".$this->boundary1."--\n\n";
}
function mail_attachment($filename)//附件
{
  for($i=0;$i<count($filename);$i++)
  {
   $this->body.="--".$this->boundary0."\n";
   $this->body.=$this->file_header($filename[$i])."\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment;\n filename=\"$filename[$i]\"\n\n";
   $this->body.=$this->loadfromfile($filename[$i]);
  }
  
  
  $this->body.="--".$this->boundary0."--\n\n";
}

function send_mail($to,$subject,$from)//发送
{
  mail($to,$subject,$this->body,"From:".$from."\n".$this->get_mail_header());

}
};

/*$s=new mime_mail;
//这里是调用格式
$s->mail_body("<font color=red>heheehe</font><img src=\"test.gif\"><img src=\"Thumb1.jpg\">");
$s->mail_picture(array("test.gif","Thumb1.jpg"));//如果没有就写mail_picture(null);下面也是
$s->mail_attachment(array("MIME_MAIL.PHP","Thumb1.jpg","newProject.rar","session.zip"));
$s->send_mail("wan_dou@wenwenweb.com","测试","zhangxw@online.sh.cn");*/
?> 

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网