$params = array (
'SmtpServer' => 'mail.XXX.com',
'SmtpAuth' => '1',
'SmtpUser' => 'system@XXX.com',
'SmtpPassword' => 'XXX',
'SystemMailer' => 'Cor Inc.',
'SystemMailAddress' => 'system@XXX.com',
);
define("MAIL_SMTP_SERVER", $params['SmtpServer']);
define("MAIL_AUTH", $params['SmtpAuth']);
define("MAIL_USERNAME", $params['SmtpUser']);
define("MAIL_PASSWORD", $params['SmtpPassword']);
define('SYSTEM_MAILER', $params['SystemMailer']);
define("SUPPORT_EMAIL",$params['SystemMailAddress']);
/**
* 通过SMTP发送邮件
*
* @param array $to
* 例 array("will"=>"will@126.com") 数组key值为收件人姓名,数组value值为收件人邮件地址, 可同时多个收件人;
* @param string $from_name 发件人姓名
* @param string $from 发件人邮件地址
* @param string $subject 邮件主题
* @param string $message 邮件内容 注:支持HTML
* @param string $charset 邮件编码 默认指定为gbk
* @return int
* 返回值 0 为发送成功, 非0时为发送失败.
*/
function SendMail($to, $from_name,$from, $subject, $message , $charset="gbk")
{
//发送邮件
if(!$from_name) $from_name = SYSTEM_MAILER;
if(!$from) $from = SUPPORT_EMAIL;
if( SYSTEM_MAILER && function_exists('mail'))//使用sendmail组件发送邮件
{
$message = str_replace("\r", '', $message);
if(!is_array($to)) return -7;
foreach($to as $key=>$email)
{
if($email)
{
@mail($email, $subject, $message, "From: $from");
}
}
}
else //使用SMTP发送邮件
{
//return SmtpSendMail($tmp, $from_name,$from, $subject, $message, $charset);
//大量邮件群发
if(is_array($to)){
foreach($to as $key=>$value){
$tmp[$key]=$value;
if(count($tmp)==10){
$result[]=SmtpSendMail($tmp, $from_name,$from, $subject, $message, $charset);
$tmp="";
}
}
if($tmp){
return SmtpSendMail($tmp, $from_name,$from, $subject, $message, $charset);
}else{
return array_pop($result);
}
}else{
return -7;
}
}
return 0;
}
function SmtpSendMail($to, $from_name,$from, $subject, $message, $charset="gbk")
{
$smtp = explode(":",MAIL_SMTP_SERVER);
$check = MAIL_AUTH;
$smtp_host = $smtp[0];
$smtp_port = $smtp[1] ? $smtp[1] : 25;
$mail_debug = $GLOBALS['queue_debug'];
if ($check) {
$username = MAIL_USERNAME;
$password = MAIL_PASSWORD;
}
$s_from = SUPPORT_EMAIL;
$fp = @fsockopen ($smtp_host, $smtp_port, $errno, $errstr, 10);
if (!$fp ) return -1;
if(function_exists("stream_set_blocking")){
stream_set_blocking($fp, true);
} else {
set_socket_blocking($fp, true );
}
stream_set_timeout($fp,10);
$lastmessage=fgets($fp,512);
//调式信息
if($mail_debug){
echo $lastmessage."\r\n";
}
if ( substr($lastmessage,0,3) != 220 ) return -1;
$yourname = "YOURNAME";
if($check == "1") $lastact="HELO ".$yourname."\r\n";
else $lastact="HELO ".$yourname."\r\n";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
//调式信息
if($mail_debug){
echo $lastact;
echo $lastmessage;
}
if (substr($lastmessage,0,3) != 220 ) return -2;
if ($check=="1") {
$lastact="AUTH LOGIN"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
//调式信息
if($mail_debug){
echo $lastact;
echo $lastmessage;
}
if (substr($lastmessage,0,3) != 334) return -3;
$lastact=base64_encode($username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
//调式信息
if($mail_debug){
echo $lastact;
echo $lastmessage;
}
if (substr($lastmessage,0,3) != 334) return -4;
$lastact=base64_encode($password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
//调式信息
if($mail_debug){
echo $lastact;
echo $lastmessage;
}
if (substr($lastmessage,0,3) != "235") return -5;
}
$lastact="MAIL FROM: <$s_from>\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
//调式信息
if($mail_debug){
echo $lastact;
echo $lastmessage;
}
if (substr($lastmessage,0,3) != 250) return -6;
$i=0;
if(is_array($to)){
foreach($to as $key=>$email){
if($email){
$lastact = "RCPT TO: <$email>\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
//调式信息
if($mail_debug){
echo $lastact;
echo $lastmessage;
}
if (substr($lastmessage,0,3) == 250){
//return -7;
$i++;
}
$tomails[]="$key<$email>";
}
}
if($i<1) return -7;
}
else{
return -7;
}
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
//调式信息
if($mail_debug){
echo $lastact;
echo $lastmessage;
}
if (substr($lastmessage,0,3) != 354) return -8;
$head .= "MIME-Version: 1.0\r\n";
$head .= "Content-type: text/html; charset=$charset\r\n";
$head .= "Subject: $subject\r\n";
$message = $head."\r\n".$message;
$head="From: \"".$from_name."\" <$from>\r\n";
$message = $head.$message;
//$head="To: \"".$to_name."\" <$to>\r\n";
$head="To: ".implode(",",$tomails)."\r\n";
$message = $head.$message;
$message .= "\r\n.\r\n";
fputs($fp, $message);
//调式信息
if($mail_debug){
$lastmessage = fgets ($fp,512);
echo $head;
echo $lastmessage;
}
$lastact="QUIT\r\n";
fputs($fp,$lastace);
fclose($fp);
return 0;
}