<?php 
if($_POST[submit]!=""){
$subject=$_POST[subject];
$mailbody=$_POST[mailbody];
$mailbody=str_replace('\\','',$mailbody);
$envelope["from"]=$_POST[formuser];
$part1["type"] = TYPEMULTIPART;
$part1["subtype"] = "mixed";

$part2["type"] = TYPETEXT;
$part2["subtype"] = "plain";
$part2["encoding"] = ENCBINARY;
$part2["contents.data"] = "$mailbody\n\n\n\t";

$filename = $_FILES['upfile']['name'];
if($filename!=""){
$file=$_FILES['upfile']['tmp_name'];
$fp = @fopen($file, "r");
$contents = @fread($fp, @filesize($file));
@fclose($fp);

 if($_FILES['upfile']['type']){
    $mimeType = $_FILES['upfile']['type']; 
  }else{
    $mimeType ="application/unknown"; 
  }
  $part3["type"] = TYPEAPPLICATION;
  $part3["encoding"] = ENCBINARY;
  $part3["subtype"] = $mimeType;
  $part3["description"] = $filename;
  $part3["contents.data"] = $contents;
  }
 $body[1] = $part1;
 $body[2] = $part2;
 if($filename!=""){
   $body[3] = $part3;
 }
$message=imap_mail_compose($envelope, $body);
list($msgheader,$msgbody)=split("\r\n\r\n",$message,2);
  $data=trim($_POST[touser]);
  $datas=explode("*",$data);
$mail_date=date("Y-m-d H:i:s");            //获取时间
$ip=getenv('REMOTE_ADDR');            //获取IP地址
include_once("conn/conn.php"); 
  while(list($name,$value)=each($datas)){
    $sendes=imap_mail($value,$subject,$msgbody,$msgheader);
		$sqlstr = 'select * from tb_mail_log where tb_mail_id = 0';		//生成sql语句
		$rst = $conn -> execute($sqlstr) or die('Error: '.$conn -> errorMsg()) ;
			$fields = array();										//创建一个数组
			$fields['tb_mail_ip'] = $ip;						//数组中添加数据
			$fields['tb_mail_title'] = $subject;						//数组中添加数据
			$fields['tb_mail_formuser'] = $_POST['formuser'];						//数组中添加数据
			$fields['tb_mail_touser'] = $value;						//数组中添加数据
			$fields['tb_mail_date'] = $mail_date;						//数组中添加数据
			/*添加新数据*/
			$insert = $conn -> getInsertSQL($rst,$fields) or die('update error：'.$conn -> errorMsg());
			$conn -> execute($insert);								//执行添加


$sql1="select * from tb_colleague where tb_colleague_mail='".$value."'";
$ret=$conn->execute($sql1);

if(count($ret->GetRows())>0){
 echo "<script>alert('邮件发\\r送成功!');history.back();</script>";
}else{
 echo "<meta http-equiv=\"Refresh\" content=\"3;url=insert_colleagues.php?mail_address=$value\">邮件已经发送成功！这是一个新客户，您可以将其添加到您的联系人</p>";
}


 } 

}else{
  echo "<script>alert('邮件发送失败!');history.back();</script>";
}

?> 



