下面为smtp_mail.php,为类文件<?php
// 需要 php_sockets.dll 扩展模块
set_time_limit(120); class smtp_mail
{
var $host; //主机
var $port; //端口 一般为25
var $user; //SMTP认证的帐号
var $pass; //认证密码
var $debug = false; //是否显示和服务器会话信息
var $conn;
var $result_str; //结果
var $in; //客户机发送的命令
var $from; //源信箱
var $to; //目标信箱
var $subject; //主题
var $body; //内容
var $auth=false;
var $content_type = "text/plain"; // 调试函数
function debug_show($strMessage)
{
if($this->debug)
{
echo $strMessage."<p>\r\n";
}
} // 功能:实现SMTP命令的发送
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("客户机命令:".$this->in);
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
} // 功能:初始化SMTP邮件发送
function smtp_mail($host,$port,$user,$pass,$auth=false,$debug=false)
{
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->pass = base64_encode($pass);
$this->debug = $debug;
$this->auth = $auth; // 初始化SOCKET
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if($this->socket)
{
$this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("创建SOCKET失败,请检查您的网络连接和参数");
} // 建立到SMTP服务器的TCP连接
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn)
{
$this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("创建SOCKET连接失败,请检查您的网络连接和参数");
}
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
} // 功能:实现真正的邮件传送
function send($from,$to,$subject,$body,$attache="")
{
if($from == "" || $to == "")
{
exit("请输入信箱地址");
}
if($subject == "")
{
$sebject = "无标题";
}
if($body == "")
{
$body = "无内容";
} $this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body; $All = "From:".$this->from."\r\n";
$All.= "To:".$this->to."\r\n";
$All.= "Subject:".$this->subject."\r\n";
$All.= "Mime-Version: 1.0\r\n"; // 判断是否有附件
if(is_array($attache))
{
// 发送含有附件的内容
$boundary = "===" . md5(uniqid("")) . "==="; $All.= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"\r\n\r\n";
$All.= "This is a multi-part message in MIME format.\r\n\r\n";
$All.= "--" . $boundary . "\r\n";
$All.= "Content-Type: ". $this->content_type ."\r\n";
$All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$All.= $this->body . "\r\n\r\n"; while(list($key, $val) = each($attache))
{
$fd = fopen($val, "r") or die("不能打开文件".$val."!\r\n");
$contents = chunk_split(base64_encode(fread($fd,filesize($val))));
fclose($fd);
$All.= "--" . $boundary . "\r\n";
$All.= "Content-Type: application/octet-stream; name=\"".basename($val)."\"\r\n";
$All.= "Content-Transfer-Encoding: BASE64\r\n";
$All.= "Content-Disposition: attachment; filename=\"".basename($val)."\"\r\n\r\n";
$All.= $contents . "\r\n";
}
$All.= "--" . $boundary . "--\r\n";
}
else
{
// 发送无附件的内容
$All.= "Content-Type: ".$this->content_type."\r\n";
$All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$All.= $this->body;
} //以下是和服务器会话
$this->in = "EHLO HELO\r\n";
$this->docommand(); // 是否需要验证
if($this->auth)
{
$this->in = "AUTH LOGIN\r\n";
$this->docommand(); $this->in = $this->user."\r\n";
$this->docommand(); $this->in = $this->pass."\r\n";
$this->docommand();
} $this->in = "MAIL FROM:".$this->from."\r\n";
$this->docommand(); $this->in = "RCPT TO:".$this->to."\r\n";
$this->docommand(); $this->in = "DATA\r\n";
$this->docommand(); $this->in = $All."\r\n.\r\n";
$this->docommand(); //结束,关闭连接
$this->in = "QUIT\r\n";
$this->docommand();
}
}
?>
// 需要 php_sockets.dll 扩展模块
set_time_limit(120); class smtp_mail
{
var $host; //主机
var $port; //端口 一般为25
var $user; //SMTP认证的帐号
var $pass; //认证密码
var $debug = false; //是否显示和服务器会话信息
var $conn;
var $result_str; //结果
var $in; //客户机发送的命令
var $from; //源信箱
var $to; //目标信箱
var $subject; //主题
var $body; //内容
var $auth=false;
var $content_type = "text/plain"; // 调试函数
function debug_show($strMessage)
{
if($this->debug)
{
echo $strMessage."<p>\r\n";
}
} // 功能:实现SMTP命令的发送
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("客户机命令:".$this->in);
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
} // 功能:初始化SMTP邮件发送
function smtp_mail($host,$port,$user,$pass,$auth=false,$debug=false)
{
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->pass = base64_encode($pass);
$this->debug = $debug;
$this->auth = $auth; // 初始化SOCKET
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if($this->socket)
{
$this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("创建SOCKET失败,请检查您的网络连接和参数");
} // 建立到SMTP服务器的TCP连接
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn)
{
$this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("创建SOCKET连接失败,请检查您的网络连接和参数");
}
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
} // 功能:实现真正的邮件传送
function send($from,$to,$subject,$body,$attache="")
{
if($from == "" || $to == "")
{
exit("请输入信箱地址");
}
if($subject == "")
{
$sebject = "无标题";
}
if($body == "")
{
$body = "无内容";
} $this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body; $All = "From:".$this->from."\r\n";
$All.= "To:".$this->to."\r\n";
$All.= "Subject:".$this->subject."\r\n";
$All.= "Mime-Version: 1.0\r\n"; // 判断是否有附件
if(is_array($attache))
{
// 发送含有附件的内容
$boundary = "===" . md5(uniqid("")) . "==="; $All.= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"\r\n\r\n";
$All.= "This is a multi-part message in MIME format.\r\n\r\n";
$All.= "--" . $boundary . "\r\n";
$All.= "Content-Type: ". $this->content_type ."\r\n";
$All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$All.= $this->body . "\r\n\r\n"; while(list($key, $val) = each($attache))
{
$fd = fopen($val, "r") or die("不能打开文件".$val."!\r\n");
$contents = chunk_split(base64_encode(fread($fd,filesize($val))));
fclose($fd);
$All.= "--" . $boundary . "\r\n";
$All.= "Content-Type: application/octet-stream; name=\"".basename($val)."\"\r\n";
$All.= "Content-Transfer-Encoding: BASE64\r\n";
$All.= "Content-Disposition: attachment; filename=\"".basename($val)."\"\r\n\r\n";
$All.= $contents . "\r\n";
}
$All.= "--" . $boundary . "--\r\n";
}
else
{
// 发送无附件的内容
$All.= "Content-Type: ".$this->content_type."\r\n";
$All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$All.= $this->body;
} //以下是和服务器会话
$this->in = "EHLO HELO\r\n";
$this->docommand(); // 是否需要验证
if($this->auth)
{
$this->in = "AUTH LOGIN\r\n";
$this->docommand(); $this->in = $this->user."\r\n";
$this->docommand(); $this->in = $this->pass."\r\n";
$this->docommand();
} $this->in = "MAIL FROM:".$this->from."\r\n";
$this->docommand(); $this->in = "RCPT TO:".$this->to."\r\n";
$this->docommand(); $this->in = "DATA\r\n";
$this->docommand(); $this->in = $All."\r\n.\r\n";
$this->docommand(); //结束,关闭连接
$this->in = "QUIT\r\n";
$this->docommand();
}
}
?>
require("smtp_mail.php");
$smtp = new smtp_mail("smtphost","25","username","password",true,false);
$smtp->send("username@smtphost","[email protected]","主题:HELLO","内容:THIS IS A TEST MAIL",array("a.txt","a.txt"));
?>
http://www.phpe.net/?n=Classes&t=12
/*
$to 目标
$subject 主题
$message 正文
$from 发自
$content_type 类型
$attache 附件,文件名放在数组中
*/
$email = $_POST['email'];
$email = trim("$email");$message = "hello";function mail2( $to, $subject, $message, $from, $content_type, $attache="") {
if(!empty($from)) $head = "From: $from\n";
if(empty($content_type)) $content_type = "text/plain"; if(is_array($attache)) {
$boundary = "===" . md5(uniqid("")) . "===";
$head .= "Mime-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"";
$head .= "$boundary\"\n\nThis is a multi-part message in MIME format.\n\n";
$head .= "--$boundary\n";
$head .= "Content-Type: $content_type\n";
$head .= "\n$message\n\n"; while(list($key, $val) = each($attache)) {
$fd = fopen("$val", "r") or die("unable to open file $val");
$contents = chunk_split(base64_encode(fread($fd,filesize("$val"))));
fclose($fd);
$head .= "--$boundary\n";
$head .= "Content-Type: application/octet-stream; name=\"".basename($val);
$head .= "\"\nContent-Transfer-Encoding: BASE64\n";
$head .= "Content-Disposition: attachment; filename=\"".basename($val);
$head .= "\"\n\n" . $contents . "\n\n";
}
$head .= "--" . $boundary . "--\n\n";
}else{
if(!empty($content_type)) {
$head .= "Content-Type: $content_type\n";
$head .= "\n$message\n";
}
}
return mail($to,$subject,"",$head );
} mail2("$email","",$message,"","",array("a.doc"));?>
Mail_Mime http://pear.php.net/package/Mail_Mime