关 键 词:
格式 邮件 认证 支持 this-connection this-elog fputs
<?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 xyzrn");
}
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 LOGINrn");
$this->elog(fgets($this->connection, 1024));
$base64_username=base64_encode($this->username);
$this->elog("$base64_username", 1);
fputs($this->connection,"$base64_usernamern");
$this->elog(fgets($this->connection, 1024));
$base64_password=base64_encode($this->password);
$this->elog("$base64_password", 1);
fputs($this->connection,"$base64_passwordrn");
$this->elog(fgets($this->connection, 1024));
}
$this->elog("MAIL FROM:$from_mail", 1);
fputs($this->connection,"MAIL FROM:$from_mailrn");
$this->elog(fgets($this->connection, 1024));
$this->elog("RCPT TO:$to_mail", 1);
fputs($this->connection, "RCPT TO:$to_mailrn");
$this->elog(fgets($this->connection, 1024));
$this->elog("DATA", 1);
fputs($this->connection, "DATArn");
$this->elog(fgets($this->connection, 1024));
$this->elog("Subject: $subject", 1);
$this->elog("To: $to_name", 1);
fputs($this->connection,"Subject: $subjectrn");
fputs($this->connection,"To: $to_namern");
if ($header)
{
$this->elog($header, 1);
fputs($this->connection, "$headerrn");
}
$this->elog("", 1);
$this->elog($body, 1);
$this->elog(".", 1);
fputs($this->connection,"rn");
fputs($this->connection,"$body rn");
fputs($this->connection,".rn");
$this->elog(fgets($this->connection, 1024));
return 1;
}
function send()
{
if ($this->connection)
{
fputs($this->connection, "QUITrn");
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, "n-------------------------------------------n");
fwrite($this->elog_fp, " Sent " . date("Y-m-d H:i:s") . "n");
fwrite($this->elog_fp, "-------------------------------------------n");
}
// write to log
if (!$mode)
{
fwrite($this->elog_fp, " $textn");
}
else
{
fwrite($this->elog_fp, "$textn");
}
}
}
?>
c_mail.php
<?php
/* 邮件发送类 */
require_once dirname(__FILE__)."/c_smtp_client.php";
static $c_smtp_client;
if(!isset($c_smtp_client))
{
$c_smtp_client = & new c_smtp_client($smtp_server['name']);
$c_smtp_client->do_log = false;
$c_smtp_client->need_auth = $smtp_server['need_auth'];
$c_smtp_client->username = $smtp_server['username'];
$c_smtp_client->password = $smtp_server['password'];
}
class c_mail
{
// html格式的Mail信笺
function html_mailer($from_name,$from_mail,$to_name,$to_mail,$subject,$message,$reply_mail)
{
$headers = "";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=gb2312rn";
$headers .= "From: ".$from_name."<".$from_mail.">rn";
$headers .= "To: ".$to_name."<".$to_mail.">rn";
$headers .= "Reply-To: ".$from_name."<".$reply_mail.">rn";
$headers .= "X-Priority: 1rn";
$headers .= "X-MSMail-Priority: Highrn";
$headers .= "X-Mailer: WebCMS MAIL SERV";
// 开始发送邮件
if($smtp_server['name']=='')
{
@mail($to_mail, $subject, $message, $headers);
}
else
{
$c_smtp_client->email($from_mail,$to_mail,$to_name,$headers,$subject,$message);
$c_smtp_client->send();
}
}
}
?>
config.inc.php
<?php
//smtp_server['name']=='';则表示直接使用mail();
//smtp_server['name']=='localhost';表示程序所在主机的smtp服务器
$smtp_server['name'] = "";
$smtp_server['need_auth'] = true;
$smtp_server['username'] = '';
$smtp_server['password'] = '';
?>
sendmail.php
<?php
//调用方法
require_once dirname(__FILE__)."/config.inc.php";
require_once dirname(__FILE__)."/c_mail.php";
$c_mail = new c_mail();
$c_mail->html_mailer($from_name,$from_mail,$to_name,$to_mail,$subject,$message,$reply_mail);
?>欢迎进入PHP开发资源论坛讨论。
·Zope的优点及和Apache+PHP+MySQL的比较
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·黑防黑:黑客口述—关于Php后门的隐藏技巧
·PRADO框架TDataGrid使用教程(1)
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·黑防黑:黑客口述—关于Php后门的隐藏技巧
·PRADO框架TDataGrid使用教程(1)
热门技术文档
·Zope的优点及和Apache+PHP+MySQL的比较
·Windows 下的 PHP 扩展编程
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·php时间求法(二)
·Windows 下的 PHP 扩展编程
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·php时间求法(二)
最新图文档
本站编辑推荐:(本站开通Delphi4PHP专区,欢迎进入论坛交流!)
- · 3分钟快速了解 Delphi for PHP 特色 (中文), PDF档
- · 购买Delphi for PHP的五大理由, PDF档
- · Delphi for PHP 使用规格介绍, PDF档
- · Delphi for PHP 問答集 (From CodeGear)
- · Delphi for PHP 产品价格表
编缉最近更新文章
网站赞助商
搜索您感兴趣的内容




