动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 支持stmp认证、HTML格式邮件的类

作者来源: 
阅读 1295 人次 , 2006-3-29 4:08:00 


c_smtp_client.php
<?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\r\n");
}

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\r\n");
$this->elog(fgets($this->connection, 1024));
 
$base64_username=base64_encode($this->username);
$this->elog("$base64_username", 1);
fputs($this->connection,"$base64_username\r\n");
$this->elog(fgets($this->connection, 1024));

$base64_password=base64_encode($this->password);
$this->elog("$base64_password", 1);
fputs($this->connection,"$base64_password\r\n");
$this->elog(fgets($this->connection, 1024));
}

$this->elog("mail from:$from_mail", 1);
fputs($this->connection,"mail from:$from_mail\r\n");
$this->elog(fgets($this->connection, 1024));

$this->elog("rcpt to:$to_mail", 1);
fputs($this->connection, "rcpt to:$to_mail\r\n");
$this->elog(fgets($this->connection, 1024));

$this->elog("data", 1);
fputs($this->connection, "data\r\n");
$this->elog(fgets($this->connection, 1024));

$this->elog("subject: $subject", 1);
$this->elog("to: $to_name", 1);
fputs($this->connection,"subject: $subject\r\n");
fputs($this->connection,"to: $to_name\r\n");

if ($header)
{
$this->elog($header, 1);
fputs($this->connection, "$header\r\n");
}

$this->elog("", 1);
$this->elog($body, 1);
$this->elog(".", 1);
fputs($this->connection,"\r\n");
fputs($this->connection,"$body \r\n");
fputs($this->connection,".\r\n");
$this->elog(fgets($this->connection, 1024));

return 1;
}


function send()
{
if ($this->connection)
{
fputs($this->connection, "quit\r\n");
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, " $text\n");
}
else
{
fwrite($this->elog_fp, "$text\n");
}
}
}
?>



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.0\r\n";
$headers .= "content-type: text/html; charset=gb2312\r\n";
$headers .= "from: ".$from_name."<".$from_mail.">\r\n";
$headers .= "to: ".$to_name."<".$to_mail.">\r\n";
$headers .= "reply-to: ".$from_name."<".$reply_mail.">\r\n";
$headers .= "x-priority: 1\r\n";
$headers .= "x-msmail-priority: high\r\n";
$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);

?>

 本文Tags邮件  HTML/CSS  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:PHP画图的程序
· 下一篇:无限级别菜单的实现(其实还是有限级别的^0^)
· PHP教程.经验技巧(下)
· PHP新手上路(一)
· 用session代替apache服务器验证
· PHP4.04在英文win2000下的安装
· 面向对象编程 -- Classes and Objects in PHP5


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077