一个实用的可带附件的email函数
<?php
/*
$to 目标
$subject 主题
$message 正文
$from 发自
$content_type 类型
$attache 附件,文件名放在数组中
*/
function mail2( $to, $subject, $message, $from, $content_type, $attache="") {
  if(!empty($from)) $head = "From: $from\n";
  if(empty($content_type)) $content_type = "text/plain";  if(is_array($attache)) {
    $boundary = "===" . md5(uniqid("")) . "===";
    $head .= "Mime-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"";
    $head .= "$boundary\"\n\nThis is a multi-part message in MIME format.\n\n";
    $head .= "--$boundary\n";
    $head .= "Content-Type: $content_type\n";
    $head .= "\n$message\n\n";    while(list($key, $val) = each($attache)) {
      $fd = fopen("$val", "r") or die("unable to open file $val");
      $contents = chunk_split(base64_encode(fread($fd,filesize("$val"))));
      fclose($fd);
      $head .= "--$boundary\n";
      $head .= "Content-Type: application/octet-stream; name=\"".basename($val);
      $head .= "\"\nContent-Transfer-Encoding: BASE64\n";
      $head .= "Content-Disposition: attachment; filename=\"".basename($val);
      $head .= "\"\n\n" . $contents . "\n\n";
    }
    $head .= "--" . $boundary . "--\n\n";
  }else{
    if(!empty($content_type)) {
      $head .= "Content-Type: $content_type\n";
      $head .= "\n$message\n";
    } 
  }
  return mail($to,$subject,"",$head ); 
} // Calling sample 
mail2("[email protected]","Hello",$message,"[email protected]","",array("a.txt","count.txt"));
?>

解决方案 »

  1.   

    哦,谢谢,我是新手,我要做个能发html邮件和文本邮件的邮件系统。不知哪位有成功的例子我来学习。
      

  2.   

    还是到 www.phpe.net 去看看。 那里有这样的类
      

  3.   

    如果服务器本身支持mail()那就很简单,如果不支持要用到socket
    连接某一服务器的25端口(如www.sina.com)然后发送stmp的指令到该站点,就能实现。
    stmp指令可以查到,helo什么的我忘了。
      

  4.   

    请问xuzuning(唠叨) 你的程序是把本目录下的文件添加为附件,我改了一下,如果是用一个html页面的"<input name="add" type="file" id="add">" 来提交任意目录下的文件好象就有问题,实际收到的是"php4F.tmp",还是不太理解,有没有这样的实例?如解决另开贴给分,谢谢