<?php
// 全部mail函数15-8-11

session_start();

if(strtolower($_POST['code'])!=strtolower($_SESSION['code'])){
	echo "<script>
	alert('验证码错误".$code."');
	history.go(-1);
	</script>";
}else{
	$ls_msg=sendmail($_POST);
	unset($_SESSION['feedback']);
}

/*------------------------------------------------------
//  发送邮件的函数，适用于webmotor,从数据库获得反馈邮箱
//　在一般php文件中，用于php文件头的下面，适用于一个form反馈
//　使用方法：sendmail($_POST)  返回相关信息
------------------------------------------------------*/
function sendmail($postvars,$lang="en"){

require_once('conn.php');
$opendb=new mydb();
require_once('char_set.php');
global $sendmailOKMsg,$sendmailFailMsg,$sendmailFailMsgcn,$sendmailOKMsgcn;

$Back=$HTTP_REFERER;      /* 返回路径 */
$Subject="Information from ".$_SERVER['HTTP_HOST'];  /* 标题*/

$config_sql="select * from ls_config ";
$config_query=$opendb->query($config_sql);
$config_rows=mysql_fetch_assoc($config_query);
$emailAddress=$config_rows['feedbackEmail'];

	if(count($postvars)!=0)  //pass vars fron URL
	{
		while(list($akey,$avalue)=each($postvars))
		{
			if(strpos($akey,"mail")!==false)  //获得客户邮件地址
			{$email_from=trim($avalue);}
    		switch ($akey){
    		case "Mr":
       		$Mr=$avalue;
       		break;
    		case "name":
        	$name=$avalue;
       		break;
    		case "Submit":
        	break;
    		case "submit":
        	break;
    		case "ec_cid":
        	break;
    		case "guid":
        	break;
    		case "ls2":
        	break;
			case "code":
        	break;
			
   			default:
			if(strpos($akey,"select")===0||strpos($akey,"button")===0){break;}
	    	$Content.=urldecode($akey).":  ";
	    	$Content.=urldecode($avalue)."<br/>\r\n";
        	break;
    		}
		}
	}
	$headers = "MIME-Version:1.0\r\n"; 
	$headers .= "Content-type:text/html; charset=utf-8" . "\r\n"; 
	$headers .= "From:$email_from"; 
	$Content .="<br/>\r\n".date('Y-m-d');
	
	if(mail($emailAddress,$Subject,$Content, $headers))
	{
		$openck=new cktb();
		if($openck->checktable("ls_pro_feedback","CREATE TABLE IF NOT EXISTS `ls_pro_feedback` (
  `ID` int(4) NOT NULL AUTO_INCREMENT,
  `requirement` text,
  `name` varchar(100) DEFAULT NULL,
  `company` varchar(200) DEFAULT NULL,
  `tel` varchar(100) DEFAULT NULL,
  `email` varchar(150) DEFAULT NULL,
  `date` varchar(200) DEFAULT NULL,
  `path` varchar(1000) DEFAULT NULL,
  `address` varchar(500) DEFAULT NULL,
  `fax` varchar(100) DEFAULT NULL,
  `http` varchar(100) DEFAULT NULL,
  `proID` int(4) DEFAULT NULL,
  `ip` varchar(50) DEFAULT NULL,
  `explorer` varchar(100) DEFAULT NULL,
  `home` varchar(300) DEFAULT NULL,
  `system` varchar(50) DEFAULT NULL,
  `enable` int(1) DEFAULT '1',
  `cateNameID` int(4) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;"))	$ls_msg = $ls_msg .$openck->ls_msg;

		foreach($postvars  as $akey => $avalue){
			switch (urldecode($akey)){
			case "Subject":
       		$subject=$avalue;
       		break;
    		case "Comments":
       		$comments=$avalue;
       		break;
    		case "Name":
        	$username=$avalue;
       		break;
    		case "Companyname":
			$companyname=$avalue;
        	break;
			case "Address":
			$address=$avalue;
        	break;
    		case "Tel":
			$tel=$avalue;
        	break;
			case "Fax":
			$fax=$avalue;
        	break;
    		case "Mail":
			$mail=$avalue;
        	break;
			case "Web":
			$http=$avalue;
        	break;
    		case "path":
			$path=$avalue;
        	break;
    		case "pro_id":
			$pro_id=$avalue;
        	break;
			case "guid":
			$guid=$avalue;
        	break;
			case "business":
			$business=$avalue;
        	break;
    		}
		}
		//反馈写入数据库
		require_once('ls_admin/inc/conn.php');
		$opendb=new mydb();
		require_once('ls_admin/inc/char_set.php');
		require_once('ls_admin/inc/ls_syscode.php');
		
		date_default_timezone_set('Asia/Shanghai');//设置时区为中国时间
		
		if($path == NULL)
			$path = $subject;
		
		$gifo = new get_gust_info();
		$ip_home = '';
		$ipadds = $gifo->Getaddress();
		foreach($ipadds[0] as $value){
			 $ip_home .= " ".iconv("utf-8","utf-8",$value);    
		}
		if($ip_home == NULL)
			 $ip_home = "本地";
		
		if($pro_id != null){
			$insert_pro_feedback_sql=sprintf("insert into ls_pro_feedback(requirement,name,company,tel,email,path,date,ip,explorer,home,system,proID,cateNameID) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
						GetSQLValueString($comments,"text"),
						GetSQLValueString($username,"text"),
						GetSQLValueString($companyname,"text"),
						GetSQLValueString($tel,"text"),
						GetSQLValueString($mail,"text"),
						GetSQLValueString($path,"text"),
						GetSQLValueString(date("Y-m-d H:i:s"),"text"),
						GetSQLValueString($gifo->Getip(),"text"),
						GetSQLValueString($gifo->GetBrowser()." - ".$gifo->GetLang(),"text"),
						GetSQLValueString($ip_home,"text"),
						GetSQLValueString($gifo->GetOs(),"text"),
						GetSQLValueString($pro_id,"int"),
						GetSQLValueString($business,"int")
						);
			$insert_query=$opendb->query($insert_pro_feedback_sql);

		}else{
			$insert_pro_feedback_sql=sprintf("insert into ls_pro_feedback(requirement,name,company,tel,http,fax,email,path,address,date,ip,explorer,home,system,proID,cateNameID) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
						GetSQLValueString($comments,"text"),
						GetSQLValueString($username,"text"),
						GetSQLValueString($companyname,"text"),
						GetSQLValueString($tel,"text"),
						GetSQLValueString($http,"text"),
						GetSQLValueString($fax,"text"),
						GetSQLValueString($mail,"text"),
						GetSQLValueString($path,"text"),
						GetSQLValueString($address,"text"),
						GetSQLValueString(date("Y-m-d H:i:s"),"text"),
						GetSQLValueString($gifo->Getip(),"text"),
						GetSQLValueString($gifo->GetBrowser()." - ".$gifo->GetLang(),"text"),
						GetSQLValueString($ip_home,"text"),
						GetSQLValueString($gifo->GetOs(),"text"),
						GetSQLValueString($pro_id,"int"),
						GetSQLValueString($business,"int")
						);
			$insert_query=$opendb->query($insert_pro_feedback_sql);
		}
		
		if($lang=="cn")
		{
			return($sendmailOKMsgcn==""?"谢谢，给我们的反馈信息已经发送，我们尽快和您取得联系":$sendmailOKMsgcn);//成功 
		}
		else
		{
			return($sendmailOKMsg==""?"THANK YOU FOR CONTACTING US, OUR CUSTOMER SERVICE REPRESENTATIVE WILL CONTACT YOU WITHIN 24 HOURS. THANK YOU AGAIN":$sendmailOKMsg);//成功 
			
		}
		
	}
	else
	{
		if($lang=="cn")
		{
			return($sendmailFailMsgcn==""?"抱歉，您的反馈信息未能发出":$sendmailFailMsgcn);// 失败给提示
		}
		else
		{
			return($sendmailFailMsg==""?"SORRY, YOUR MESSAGE SENDING FAILURE":$sendmailFailMsg);
		}
	}
}

/*------------------------------------------------------
//  jmailer是jmail类的发送邮件的函数
//  入口参数与相同
//  $header=="lanscend" 使用lanscend官方邮箱发送
//　 14-3-5 赵
------------------------------------------------------*/
function jmailer($emailAddress,$Subject,$Content, $headers="", $addressee="", $attachedFile=array())
{
 date_default_timezone_set("Asia/Shanghai");//设定时区东八区
$jmail = new COM('JMail.Message')  or die('无法调用Jmail组件');
$jmail->silent = true; //屏蔽例外错误
$jmail->charset = 'gb2312'; //否则中文会乱码
$jmail->From = 'lanscendmail@163.com'; //这里理论上是可以不与发件人地址相同的，但用163的试了几次都不行，改成一样的就可以了
$jmail->FromName = 'rickbellco.com';
$jmail->AddRecipient($emailAddress); //可添加多个邮件接受者
$jmail->Subject =  $Subject;
$jmail->Body = $Content; 
$jmail->MailServerUserName = 'lanscendmail@163.com'; //发信邮件账号
$jmail->MailServerPassword = 'ls87085738'; //账户的密码

try{
     $email = $jmail->Send('smtp.163.com');
     if($email) 
	 	return true;
     else  
		 echo '邮件发送失败！错误原因:' ;
	} catch (Exception $e){
     		echo $e->getMessage();
		}


}

/*------------------------------------------------------
//  pmailer 模拟mail的发送邮件的函数
//　使用phpmailer类，入口参数与相同
//  $header=="lanscend" 使用lanscend官方邮箱发送
//　  14-3-5 赵
------------------------------------------------------*/
function pmailer($emailAddress,$Subject,$Content, $headers="", $addressee="", $attachedFile=array())
{
 date_default_timezone_set("Asia/Shanghai");//设定时区东八区

// Include PHP Mailer Class
require_once("mail/class.phpmailer.php");
//include("mail/class.smtp.php"); 
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "base64";     
$mail->IsSMTP();
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;
$mail->Port = 25;
if($headers=="lanscend")
{
	$mail->Host = "smtp.ym.163.com";
	$mail->Username   = "support@lanscend.com";
	$mail->Password = "ls2120";
}
else
{
	$mail->Host = "smtp.qq.com";
	$mail->Username   = "lssendmail@qq.com";
	$mail->Password = "ls2120";
}
if($addressee=="")
{$addressee=substr($emailAddress,0,strpos($emailAddress,"@"));}
$mail->SetFrom($mail->Username, "lanscend mailer");

if($attachedFile[0] !='')
{
	$mail->addAttachment("$attachedFile[0]");  // 增加附件
}
  
$mail->Subject= $Subject;
$mail->MsgHTML($Content);
$mail->AddAddress($emailAddress, $addressee);
if($mail->Send())
{return true;}
else
{echo '邮件发送失败！错误原因:'.$mail->ErrorInfo;}
}

/*------------------------------------------------------
//  邮件附件处理的函数
//　由使用pmailer
//  $name 附件名称，默认是文件名， doc类型
//　author: 
------------------------------------------------------*/
function addAttachment($path, $name = "", $encoding = "base64", $type = "doc/docx") {
        if(!@is_file($path))
        {
            $this->error_handler(sprintf("Could not access [%s] file", $path));
            return false;
        }

        $filename = basename($path);
        if($name == "")
            $name = $filename;

        // Append to $attachment array
        $cur = count($this->attachment);
        $this->attachment[$cur][0] = $path;
        $this->attachment[$cur][1] = $filename;
        $this->attachment[$cur][2] = $name;
        $this->attachment[$cur][3] = $encoding;
        $this->attachment[$cur][4] = $type;
        $this->attachment[$cur][5] = false; // isStringAttachment
        $this->attachment[$cur][6] = "attachment";
        $this->attachment[$cur][7] = 0;

        return true;
    }
	

?>