class sendmail{
function __construct(){
$this->set();
$this->auth = 1;
}
function set($server=yj_sysmail_smtp,$user="",$password=yj_sysmail_pass,$port=yj_sysmailport,$type=1,$mailusername=0){
$user=($user=="")?substr(yj_sysmail,0,stripos(yj_sysmail,"@")):$user;
$this->type = $type;
$this->server = $server;
$this->port = $port;
$this->user = $user;
$this->password = $password;
$this->mailusername = $mailusername;
}
function send($email_to, $email_subject, $email_message, $email_from = ''){
$email_subject = '=?utf-8?b?'.base64_encode(str_replace("r", '', $email_subject)).'?=';
$email_message = str_replace("rn.", " rn..", str_replace("n", "rn", str_replace("r", "n", str_replace("rn", "n", str_replace("nr", "r", $email_message)))));
$email_from = $email_from == '' ? '=?utf-8?b?'.base64_encode(yj_sysname)."?= <".yj_sysmail.">" : (preg_match('/^(.+?) <(.+?)>$/',$email_from, $from) ? '=?utf-8?b?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
$emails = explode(',', $email_to);
foreach($emails as $touser){
$tousers[] = preg_match('/^(.+?) <(.+?)>$/',$touser, $to) ? ($this->mailusername ? '=?utf-8?b?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
}
$email_to = implode(',', $tousers);
$headers = "mime-version: 1.0rnto: {$email_to}rnfrom: {$email_from}<{$email_from}>rnx-priority: 3rnx-mailer: eglive rndate: ".date("r")."rncontent-type: text/html; charset=utf-8rn";
if($this->type == 1){
return $this->smtp($email_to, $email_subject, $email_message, $email_from, $headers);
}elseif($this->type == 2){
return @mail($email_to, $email_subject, $email_message, $headers);
}else{
ini_set('smtp', $this->server);
ini_set('smtp_port', $this->port);
ini_set('sendmail_from', $email_from);
return @mail($email_to, $email_subject, $email_message, $headers);
}
}
function smtp($email_to, $email_subject, $email_message, $email_from = '', $headers = ''){
if(!$fp = @fsockopen($this->server, $this->port, $errno, $errstr, 10)){
$this->errorlog('smtp', "($this->server:$this->port) connect - unable to connect to the smtp server", 0);
return false;
}
stream_set_blocking($fp, true);
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != '220'){
$this->errorlog('smtp', "$this->server:$this->port connect - $lastmessage", 0);
return false;
}
fputs($fp, ($this->auth ? 'ehlo' : 'helo')." phpcmsrn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250){
$this->errorlog('smtp', "($this->server:$this->port) helo/ehlo - $lastmessage", 0);
return false;
}
while(1){
if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)){
break;
}
$lastmessage = fgets($fp, 512);
}
fputs($fp, "auth loginrn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334){
$this->errorlog('smtp', "($this->server:$this->port) auth login - $lastmessage", 0);
return false;
}
fputs($fp, base64_encode($this->user)."rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334){
$this->errorlog('smtp', "($this->server:$this->port) username - $lastmessage", 0);
return false;
}
fputs($fp, base64_encode($this->password)."rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 235){
$this->errorlog('smtp', "($this->server:$this->port) password - $lastmessage", 0);
return false;
}
fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "", $email_from).">rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250){
fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "", $email_from).">rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250){
$this->errorlog('smtp', "($this->server:$this->port) mail from - $lastmessage", 0);
return false;
}
}
$email_tos = array();
$emails = explode(',', $email_to);
foreach($emails as $touser){
$touser = trim($touser);
if($touser){
fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "", $touser).">rn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250){
fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "", $touser).">rn");
$lastmessage = fgets($fp, 512);
$this->errorlog('smtp', "($this->server:$this->port) rcpt to - $lastmessage", 0);
return false;
}
}
}
fputs($fp, "datarn");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 354){
$this->errorlog('smtp', "($this->server:$this->port) data - $lastmessage", 0);
}
$headers .= 'message-id: <'.gmdate('ymdhs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_server['http_host'].">rn";
fputs($fp, "date: ".gmdate('r')."rn");
fputs($fp, "to: ".$email_to."rn");
fputs($fp, "subject: ".$email_subject."rn");
fputs($fp, $headers."rn");
fputs($fp, "rnrn");
fputs($fp, "$email_messagern.rn");
$lastmessage = fgets($fp, 512);
fputs($fp, "quitrn");
return true;
}
function errorlog($type, $message, $is){
$this->error[] = array($type, $message, $is);
}
function mailmess($mess=""){
return "<style>*{font-family:verdana;font-size:13px;/*www.111cn.net提供*/}</style>
<table cellpadding='5' cellspacing='1' border='0' width='750' style='background-color:#fff;border:1px solid #3b5998;padding:15px;'>
<tr><td style='background:#3b5998;color:#fff;font-weight:bold;font-size:14px;'>".yj_syscnname." - ".yj_sysname."</td></tr>
<tr><td>{$mess}</td></tr>
</table>";
}
}
|