class email
{
function email()
{
$this->__construct();
}
function __construct()
{
@define('charset','gbk');
$this->set(mail_server, mail_port, mail_user, mail_pwd,mail_type);
$this->auth = 1;
}
function set($server, $port, $user, $password, $type = 1, $delimiter = 1, $mailusername = 0)
{
$this->type = $type;
$this->server = $server;
$this->port = $port;
$this->user = $user;
$this->password = $password;
$this->delimiter = $delimiter == 1 ? "rn" : ($delimiter == 2 ? "r" : "n");
$this->mailusername = $mailusername;
}
function send($email_to, $email_subject, $email_message, $email_from = '')
{
global $dircms;
$email_to=iconv("utf-8", "gbk", $email_to);
$email_subject=iconv("utf-8", "gbk", $email_subject);
$email_message=iconv("utf-8", "gbk", $email_message);
$email_subject = '=?'.charset.'?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)))));
$adminemail = $this->type == 1 ? $dircms['mail_user'] : $dircms['mail_user'];
$email_from = $email_from == '' ? '=?'.charset.'?b?'.base64_encode($dircms['site_name'])."?= <$adminemail>" : (preg_match('/^(.+?) <(.+?)>$/',$email_from, $from) ? '=?'.charset.'?b?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
$emails = explode(',', $email_to);
foreach($emails as $touser)
{
$tousers[] = preg_match('/^(.+?) <(.+?)>$/',$touser, $to) ? ($this->mailusername ? '=?'.charset.'?b?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
}
$email_to = implode(',', $tousers);
$headers = "from: $email_from{$this->delimiter}x-priority: 3{$this->delimiter}x-mailer: dircms {$this->delimiter}mime-version: 1.0{$this->delimiter}content-type: text/html; charset=".charset."{$this->delimiter}";
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 = '')
{
global $dircms;
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, "ehlo dircmsrn");
$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'].">{$this->delimiter}";
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");
fputs($fp, "quitrn");
return true;
}
function errorlog($type, $message, $is)
{
$this->error[] = array($type, $message, $is);
}
}
|