<?php /* smtp client class */
class c_smtp_client
{
var $connection;
var $server;
var $elog_fp;
var $log_file='./smtp_client.log';
var $do_log=true;
var $need_auth=true;
var $username;
var $password;
// 构造器
function c_smtp_client($server='')
{
if (!$server)
{
$this->server="localhost";
}
else
{
$this->server=$server;
}
$this->connection = fsockopen($this->server, 25);
if ($this->connection <= 0) return 0;
fputs($this->connection,"HELO xyz ");
}
function email($from_mail, $to_mail, $to_name, $header, $subject, $body)
{
if ($this->connection <= 0) return 0;
// 邮件用户认证
if ($this->need_auth)
{
$this->elog("AUTH LOGIN", 1);
fputs($this->connection,"AUTH LOGIN ");
$this->elog(fgets($this->connection, 1024));
$base64_username=base64_encode($this->username);
$this->elog("$base64_username", 1);
fputs($this->connection,"$base64_username ");
$this->elog(fgets($this->connection, 1024));
$base64_password=base64_encode($this->password);
$this->elog("$base64_password", 1);
fputs($this->connection,"$base64_password ");
$this->elog(fgets($this->connection, 1024));
}
$this->elog("MAIL FROM:$from_mail", 1);
fputs($this->connection,"MAIL FROM:$from_mail ");
$this->elog(fgets($this->connection, 1024));
$this->elog("RCPT TO:$to_mail", 1);
fputs($this->connection, "RCPT TO:$to_mail ");
$this->elog(fgets($this->connection, 1024));
$this->elog("DATA", 1);
fputs($this->connection, "DATA ");
$this->elog(fgets($this->connection, 1024));
$this->elog("Subject: $subject", 1);
$this->elog("To: $to_name", 1);
fputs($this->connection,"Subject: $subject ");
fputs($this->connection,"To: $to_name ");
if ($header)
{
$this->elog($header, 1);
fputs($this->connection, "$header ");
}
$this->elog("", 1);
$this->elog($body, 1);
$this->elog(".", 1);
fputs($this->connection,"");
fputs($this->connection,"$body");
fputs($this->connection,".");
$this->elog(fgets($this->connection, 1024));
return 1;
}
function send()
{
if ($this->connection)
{
fputs($this->connection, "QUIT ");
fclose($this->connection);
$this->connection=0;
}
}
function close()
{
$this->send();
}
function elog($text, $mode=0)
{
if (!$this->do_log) return;
// open file
if (!$this->elog_fp)
{
if (!($this->elog_fp=fopen($this->log_file, 'a'))) return;
fwrite($this->elog_fp, " ------------------------------------------- ");
fwrite($this->elog_fp, " Sent " . date("Y-m-d H:i:s") . " ");
fwrite($this->elog_fp, "------------------------------------------- ");
}
// write to log
if (!$mode)
{
fwrite($this->elog_fp, " $text ");
}
else
{
fwrite($this->elog_fp, "$text ");
}
}
}
?> |