SMTP 로 메 일 보 내기

7292 단어 socketFP
$params = array (
	'SmtpServer' => 'mail.XXX.com',
	'SmtpAuth' => '1',
	'SmtpUser' => '[email protected]',
	'SmtpPassword' => 'XXX',
	'SystemMailer' => 'Cor Inc.',
	'SystemMailAddress' => '[email protected]',
);
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"=>"[email protected]")   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
"; } if ( substr($lastmessage,0,3) != 220 ) return -1; $yourname = "YOURNAME"; if($check == "1") $lastact="HELO ".$yourname."\r
"; else $lastact="HELO ".$yourname."\r
"; 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
"; 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
"; 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
"; 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
"; 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
"; 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
"; 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
"; $head .= "Content-type: text/html; charset=$charset\r
"; $head .= "Subject: $subject\r
"; $message = $head."\r
".$message; $head="From: \"".$from_name."\" <$from>\r
"; $message = $head.$message; //$head="To: \"".$to_name."\" <$to>\r
"; $head="To: ".implode(",",$tomails)."\r
"; $message = $head.$message; $message .= "\r
.\r
"; fputs($fp, $message); // if($mail_debug){ $lastmessage = fgets ($fp,512); echo $head; echo $lastmessage; } $lastact="QUIT\r
"; fputs($fp,$lastace); fclose($fp); return 0; }

좋은 웹페이지 즐겨찾기