当前位置: 编程技术>php
本页文章导读:
▪获得Google PR值的PHP代码
今天更新blog程序后,突然发现google工具条上的PR条增加了一点,不过一直以来Google工具条上的PR因为缓冲问题,使得当前显示的PR值并不一定是目前页面的PR值,因此去查了下,发现blog站点(h.........
▪一键删除顽固的空文件夹 软件下载
今天下载了一款字体,下载后又不喜欢,也没加到系统里去就准备删掉。谁知麻烦来了,系统提示:无法删除文件:无法读取源文件或磁盘。 我晕了~怎么啦?当然,连点几次还.........
▪php中通过smtp发邮件的类,测试通过
smtp.php 代码如下:<?php class smtp { /* Public Variables */ var $smtp_port; var $time_out; var $host_name; var $log_file; var $relay_host; var $debug; var $auth; .........
[1]获得Google PR值的PHP代码
来源: 互联网 发布时间: 2013-11-30
今天更新blog程序后,突然发现google工具条上的PR条增加了一点,不过一直以来Google工具条上的PR因为缓冲问题,使得当前显示的PR值并不一定是目前页面的PR值,因此去查了下,发现blog站点(http://mlsx.xplore.cn/)的PR值上升到了3,但是首页 (http://mlsx.xplore.cn)还是2。霍霍,庆贺一下。
不过glemir今天上午告诉我他的blog早已经到了3,使我有点嫉妒,连带想起了Rachel的blog ,他们更新都没有我勤快,但是他们的PR基本上不必我差。由此我得出两个自我安慰的结论:
1)Rachel的PR和我的一样,是因为他的游记日志受欢迎,我没有他的手笔。
2)glemir的PR升的比我的快,那是因为他用了一个好blog程序--wordpress
哈哈,自嘲一下。
转入正题,贴上获得Google的PR值代码,声明不是我写的,只是修改,checksum的算法暂时还不知道。
演示代码点击这里
<?php
define('GOOGLE_MAGIC', 0xE6359A60);
function zeroFill($a, $b)
{
$z = hexdec(80000000);
if ($z & $a)
{
$a = ($a>>1);
$a &= (~$z);
$a |= 0x40000000;
$a = ($a>>($b-1));
}
else
{
$a = ($a>>$b);
}
return $a;
}
function mix($a,$b,$c)
{
$a -= $b; $a -= $c; $a ^= (zeroFill($c,13));
$b -= $c; $b -= $a; $b ^= ($a<<8);
$c -= $a; $c -= $b; $c ^= (zeroFill($b,13));
$a -= $b; $a -= $c; $a ^= (zeroFill($c,12));
$b -= $c; $b -= $a; $b ^= ($a<<16);
$c -= $a; $c -= $b; $c ^= (zeroFill($b,5));
$a -= $b; $a -= $c; $a ^= (zeroFill($c,3));
$b -= $c; $b -= $a; $b ^= ($a<<10);
$c -= $a; $c -= $b; $c ^= (zeroFill($b,15));
return array($a,$b,$c);
}
function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC)
{
if(is_null($length))
{
$length = sizeof($url);
}
$a = $b = 0x9E3779B9;
$c = $init;
$k = 0;
$len = $length;
while($len >= 12)
{
$a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
$b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
$c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
$mix = mix($a,$b,$c);
$a = $mix[0]; $b = $mix[1]; $c = $mix[2];
$k += 12;
$len -= 12;
}
$c += $length;
switch($len)
{
case 11: $c+=($url[$k+10]<<24);
case 10: $c+=($url[$k+9]<<16);
case 9 : $c+=($url[$k+8]<<8);
case 8 : $b+=($url[$k+7]<<24);
case 7 : $b+=($url[$k+6]<<16);
case 6 : $b+=($url[$k+5]<<8);
case 5 : $b+=($url[$k+4]);
case 4 : $a+=($url[$k+3]<<24);
case 3 : $a+=($url[$k+2]<<16);
case 2 : $a+=($url[$k+1]<<8);
case 1 : $a+=($url[$k+0]);
}
$mix = mix($a,$b,$c);
return $mix[2];
}
function strord($string) {
for($i=0;$i<strlen($string);$i++) {
$result[$i] = ord($string{$i});
}
return $result;
}
function ReadPR($link)
{
$fp = fsockopen ("www.google.com", 80, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)\n";
exit(1);
}
else
{
$out = "GET $link HTTP/1.0\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114.9-big; Linux 2.6)\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
do{
$line = fgets($fp, 128);
}while ($line !== "\r\n");
$data = fread($fp,8192);
fclose ($fp);
return $data;
}
}
function GetPR($url)
{
$url ='info:'.$url;
$ch = GoogleCH(strord($url));
$data = ReadPR("/search?client=navclient-auto&ch=6$ch&features=Rank&q=$url");
$rankarray = explode (':', $data);
return $rankarray[2];
}
if ( isset($_POST['url']) && $_POST['url'] !== '' )
{
echo 'PageRank: '.GetPR($_POST['url']);
}
?>
<html><title>Google PR值查询</title>
<body>Google PR值查询 <a href='/blog_article/pr.html' >获取源代码</a>
<form action='' method='post'><br />
URL:<input name='url' value='http://' type='text' size=40>
<input name='submit' type='submit' value='Get PR'>
</form></body></html>
不过glemir今天上午告诉我他的blog早已经到了3,使我有点嫉妒,连带想起了Rachel的blog ,他们更新都没有我勤快,但是他们的PR基本上不必我差。由此我得出两个自我安慰的结论:
1)Rachel的PR和我的一样,是因为他的游记日志受欢迎,我没有他的手笔。
2)glemir的PR升的比我的快,那是因为他用了一个好blog程序--wordpress
哈哈,自嘲一下。
转入正题,贴上获得Google的PR值代码,声明不是我写的,只是修改,checksum的算法暂时还不知道。
演示代码点击这里
代码如下:
<?php
define('GOOGLE_MAGIC', 0xE6359A60);
function zeroFill($a, $b)
{
$z = hexdec(80000000);
if ($z & $a)
{
$a = ($a>>1);
$a &= (~$z);
$a |= 0x40000000;
$a = ($a>>($b-1));
}
else
{
$a = ($a>>$b);
}
return $a;
}
function mix($a,$b,$c)
{
$a -= $b; $a -= $c; $a ^= (zeroFill($c,13));
$b -= $c; $b -= $a; $b ^= ($a<<8);
$c -= $a; $c -= $b; $c ^= (zeroFill($b,13));
$a -= $b; $a -= $c; $a ^= (zeroFill($c,12));
$b -= $c; $b -= $a; $b ^= ($a<<16);
$c -= $a; $c -= $b; $c ^= (zeroFill($b,5));
$a -= $b; $a -= $c; $a ^= (zeroFill($c,3));
$b -= $c; $b -= $a; $b ^= ($a<<10);
$c -= $a; $c -= $b; $c ^= (zeroFill($b,15));
return array($a,$b,$c);
}
function GoogleCH($url, $length=null, $init=GOOGLE_MAGIC)
{
if(is_null($length))
{
$length = sizeof($url);
}
$a = $b = 0x9E3779B9;
$c = $init;
$k = 0;
$len = $length;
while($len >= 12)
{
$a += ($url[$k+0] +($url[$k+1]<<8) +($url[$k+2]<<16) +($url[$k+3]<<24));
$b += ($url[$k+4] +($url[$k+5]<<8) +($url[$k+6]<<16) +($url[$k+7]<<24));
$c += ($url[$k+8] +($url[$k+9]<<8) +($url[$k+10]<<16)+($url[$k+11]<<24));
$mix = mix($a,$b,$c);
$a = $mix[0]; $b = $mix[1]; $c = $mix[2];
$k += 12;
$len -= 12;
}
$c += $length;
switch($len)
{
case 11: $c+=($url[$k+10]<<24);
case 10: $c+=($url[$k+9]<<16);
case 9 : $c+=($url[$k+8]<<8);
case 8 : $b+=($url[$k+7]<<24);
case 7 : $b+=($url[$k+6]<<16);
case 6 : $b+=($url[$k+5]<<8);
case 5 : $b+=($url[$k+4]);
case 4 : $a+=($url[$k+3]<<24);
case 3 : $a+=($url[$k+2]<<16);
case 2 : $a+=($url[$k+1]<<8);
case 1 : $a+=($url[$k+0]);
}
$mix = mix($a,$b,$c);
return $mix[2];
}
function strord($string) {
for($i=0;$i<strlen($string);$i++) {
$result[$i] = ord($string{$i});
}
return $result;
}
function ReadPR($link)
{
$fp = fsockopen ("www.google.com", 80, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)\n";
exit(1);
}
else
{
$out = "GET $link HTTP/1.0\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114.9-big; Linux 2.6)\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
do{
$line = fgets($fp, 128);
}while ($line !== "\r\n");
$data = fread($fp,8192);
fclose ($fp);
return $data;
}
}
function GetPR($url)
{
$url ='info:'.$url;
$ch = GoogleCH(strord($url));
$data = ReadPR("/search?client=navclient-auto&ch=6$ch&features=Rank&q=$url");
$rankarray = explode (':', $data);
return $rankarray[2];
}
if ( isset($_POST['url']) && $_POST['url'] !== '' )
{
echo 'PageRank: '.GetPR($_POST['url']);
}
?>
<html><title>Google PR值查询</title>
<body>Google PR值查询 <a href='/blog_article/pr.html' >获取源代码</a>
<form action='' method='post'><br />
URL:<input name='url' value='http://' type='text' size=40>
<input name='submit' type='submit' value='Get PR'>
</form></body></html>
[2]一键删除顽固的空文件夹 软件下载
来源: 互联网 发布时间: 2013-11-30
今天下载了一款字体,下载后又不喜欢,也没加到系统里去就准备删掉。谁知麻烦来了,系统提示:无法删除文件:无法读取源文件或磁盘。
我晕了~怎么啦?当然,连点几次还是这样告诉我!
根据以往的遭遇,我以为是某个文件删不了。又试着删除文件夹里的内容,出乎我的意料,竟然全部删掉了。这下我以为就剩一个空文件夹应该可以删掉了。不料想仍然告诉我:无法删除文件:无法读取源文件或磁盘。
虽然也不碍事,但一个无用的空文件夹在那里老是觉得不舒服。这下就用我所知道的开始收拾它。
先建新文件夹把它拖进去再删,不行,不准拖!
又重命名,不准!
朋友告诉我进安全模式删,也奈何不了它。注销、重启、粉碎、又在空文件夹里装入原文件依然无效……烦S人了!我下决心要把它消灭掉!
我又用最后一招,用命令提示符删除,唉!好像存心与我作对,它竟然连命令也不听,岿然不动。
无奈之下只得上网查找,找了几个软件都说可以解决这个问题,但其实都解决不了。有的方法又说得深奥、繁琐,看得头疼。罢罢罢,还是少动点脑筋。
最后找到一个口碑不错,大家都赞赏的软件unlocker 安装后按提示右键点击~哈哈,我不说大家也知道结果了。
为了让遇到这类问题的朋友少走弯路,少些烦恼,特在这里说几句,希望对大家有帮助。
我晕了~怎么啦?当然,连点几次还是这样告诉我!
根据以往的遭遇,我以为是某个文件删不了。又试着删除文件夹里的内容,出乎我的意料,竟然全部删掉了。这下我以为就剩一个空文件夹应该可以删掉了。不料想仍然告诉我:无法删除文件:无法读取源文件或磁盘。
虽然也不碍事,但一个无用的空文件夹在那里老是觉得不舒服。这下就用我所知道的开始收拾它。
先建新文件夹把它拖进去再删,不行,不准拖!
又重命名,不准!
朋友告诉我进安全模式删,也奈何不了它。注销、重启、粉碎、又在空文件夹里装入原文件依然无效……烦S人了!我下决心要把它消灭掉!
我又用最后一招,用命令提示符删除,唉!好像存心与我作对,它竟然连命令也不听,岿然不动。
无奈之下只得上网查找,找了几个软件都说可以解决这个问题,但其实都解决不了。有的方法又说得深奥、繁琐,看得头疼。罢罢罢,还是少动点脑筋。
最后找到一个口碑不错,大家都赞赏的软件unlocker 安装后按提示右键点击~哈哈,我不说大家也知道结果了。
为了让遇到这类问题的朋友少走弯路,少些烦恼,特在这里说几句,希望对大家有帮助。
软件简介:
Unlocker是一个免费的右键扩充工具,使用者在安装后,它便能整合于鼠标右键的操作当中(鼠标右键菜单里会多出一个Unlock的图标),当使用者发现有某个文件或目录无法删除时,只要按下鼠标右键中的“Unlocker”,那么程序马上就会显示出是哪一些程序占用了该目录或文件,接着只要按下弹出窗口中的“Unlock”就能够为你的文件解套。
Unlocker 不同于其它解锁软件的部分在于它并非强制关闭那些占用文件的程序,而是以解除文件与程序关连性的方式来解锁,因此不会像其它解锁程序一样因为强制关闭程序而造成使用者可能的数据遗失。
多语言版,免费使用。文件大小,191KB
请大家默认安装路径,否则不能正常运行。 醉意依然.2007.1.23
点击下载 unlocker1.8.5:http://ynysychh.googlepages.com/unlocker1.8.5.rar
[3]php中通过smtp发邮件的类,测试通过
来源: 互联网 发布时间: 2013-11-30
smtp.php
<?php
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML")
{
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "")
{
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to)
{
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to))
{
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body))
{
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
}
else
{
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo))
{
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth)
{
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user)))
{
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass)))
{
return $this->smtp_error("sending HELO command");
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">"))
{
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
{
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA"))
{
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body))
{
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom())
{
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT"))
{
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "")
{
return $this->smtp_sockopen_mx($address);
}
else
{
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok()))
{
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."\n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $MXHOSTS))
{
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
return FALSE;
}
foreach ($MXHOSTS as $host)
{
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok()))
{
$this->log_write("Warning: Cannot connect to mx host ".$host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
continue;
}
$this->log_write("Connected to mx host ".$host."\n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."\r\n".$body);
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response."\n");
if (!ereg("^[23]", $response))
{
fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned \"".$response."\"\n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "")
{
if($cmd=="")
{
$cmd = $arg;
}
else
{
$cmd = $cmd." ".$arg;
}
}
fputs($this->sock, $cmd."\r\n");
$this->smtp_debug("> ".$cmd."\n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".\n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "")
{
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
{
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "\([^()]*\)";
while (ereg($comment, $address))
{
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug)
{
echo $message;
}
}
}
?>
测试程序
<?php
/*
这是一个测试程序!!!
*/
require("smtp.php");
##########################################
$smtpserver = "smtp.163.com";//SMTP服务器
$smtpserverport = 25;//SMTP服务器端口
$smtpusermail = "reterry123@163.com";//SMTP服务器的用户邮箱
$smtpemailto = "dxytools@gmail.com";//发送给谁
$smtpuser = "reterry123";//SMTP服务器的用户帐号
$smtppass = "014040001";//SMTP服务器的用户密码
$mailsubject = "中文";//邮件主题
$mailbody = "<h1>中文</h1>测试下能淤泥新年感";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
##########################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = TRUE;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
?>
代码如下:
<?php
class smtp
{
/* Public Variables */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;
/* Private Variables */
var $sock;
/* Constractor */
function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
$this->debug = FALSE;
$this->smtp_port = $smtp_port;
$this->relay_host = $relay_host;
$this->time_out = 30; //is used in fsockopen()
$this->auth = $auth;//auth
$this->user = $user;
$this->pass = $pass;
$this->host_name = "localhost"; //is used in HELO command
$this->log_file = "";
$this->sock = FALSE;
}
/* Main Function */
function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
$mail_from = $this->get_address($this->strip_comment($from));
$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
$header .= "MIME-Version:1.0\r\n";
if($mailtype=="HTML")
{
$header .= "Content-Type:text/html\r\n";
}
$header .= "To: ".$to."\r\n";
if ($cc != "")
{
$header .= "Cc: ".$cc."\r\n";
}
$header .= "From: $from<".$from.">\r\n";
$header .= "Subject: ".$subject."\r\n";
$header .= $additional_headers;
$header .= "Date: ".date("r")."\r\n";
$header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";
list($msec, $sec) = explode(" ", microtime());
$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";
$TO = explode(",", $this->strip_comment($to));
if ($cc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
}
if ($bcc != "")
{
$TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
}
$sent = TRUE;
foreach ($TO as $rcpt_to)
{
$rcpt_to = $this->get_address($rcpt_to);
if (!$this->smtp_sockopen($rcpt_to))
{
$this->log_write("Error: Cannot send email to ".$rcpt_to."\n");
$sent = FALSE;
continue;
}
if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body))
{
$this->log_write("E-mail has been sent to <".$rcpt_to.">\n");
}
else
{
$this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");
$sent = FALSE;
}
fclose($this->sock);
$this->log_write("Disconnected from remote host\n");
}
return $sent;
}
/* Private Functions */
function smtp_send($helo, $from, $to, $header, $body = "")
{
if (!$this->smtp_putcmd("HELO", $helo))
{
return $this->smtp_error("sending HELO command");
}
#auth
if($this->auth)
{
if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user)))
{
return $this->smtp_error("sending HELO command");
}
if (!$this->smtp_putcmd("", base64_encode($this->pass)))
{
return $this->smtp_error("sending HELO command");
}
}
if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">"))
{
return $this->smtp_error("sending MAIL FROM command");
}
if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">"))
{
return $this->smtp_error("sending RCPT TO command");
}
if (!$this->smtp_putcmd("DATA"))
{
return $this->smtp_error("sending DATA command");
}
if (!$this->smtp_message($header, $body))
{
return $this->smtp_error("sending message");
}
if (!$this->smtp_eom())
{
return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
}
if (!$this->smtp_putcmd("QUIT"))
{
return $this->smtp_error("sending QUIT command");
}
return TRUE;
}
function smtp_sockopen($address)
{
if ($this->relay_host == "")
{
return $this->smtp_sockopen_mx($address);
}
else
{
return $this->smtp_sockopen_relay();
}
}
function smtp_sockopen_relay()
{
$this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok()))
{
$this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
return FALSE;
}
$this->log_write("Connected to relay host ".$this->relay_host."\n");
return TRUE;;
}
function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $MXHOSTS))
{
$this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");
return FALSE;
}
foreach ($MXHOSTS as $host)
{
$this->log_write("Trying to ".$host.":".$this->smtp_port."\n");
$this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
if (!($this->sock && $this->smtp_ok()))
{
$this->log_write("Warning: Cannot connect to mx host ".$host."\n");
$this->log_write("Error: ".$errstr." (".$errno.")\n");
continue;
}
$this->log_write("Connected to mx host ".$host."\n");
return TRUE;
}
$this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");
return FALSE;
}
function smtp_message($header, $body)
{
fputs($this->sock, $header."\r\n".$body);
$this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));
return TRUE;
}
function smtp_eom()
{
fputs($this->sock, "\r\n.\r\n");
$this->smtp_debug(". [EOM]\n");
return $this->smtp_ok();
}
function smtp_ok()
{
$response = str_replace("\r\n", "", fgets($this->sock, 512));
$this->smtp_debug($response."\n");
if (!ereg("^[23]", $response))
{
fputs($this->sock, "QUIT\r\n");
fgets($this->sock, 512);
$this->log_write("Error: Remote host returned \"".$response."\"\n");
return FALSE;
}
return TRUE;
}
function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "")
{
if($cmd=="")
{
$cmd = $arg;
}
else
{
$cmd = $cmd." ".$arg;
}
}
fputs($this->sock, $cmd."\r\n");
$this->smtp_debug("> ".$cmd."\n");
return $this->smtp_ok();
}
function smtp_error($string)
{
$this->log_write("Error: Error occurred while ".$string.".\n");
return FALSE;
}
function log_write($message)
{
$this->smtp_debug($message);
if ($this->log_file == "")
{
return TRUE;
}
$message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a")))
{
$this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");
return FALSE;;
}
flock($fp, LOCK_EX);
fputs($fp, $message);
fclose($fp);
return TRUE;
}
function strip_comment($address)
{
$comment = "\([^()]*\)";
while (ereg($comment, $address))
{
$address = ereg_replace($comment, "", $address);
}
return $address;
}
function get_address($address)
{
$address = ereg_replace("([ \t\r\n])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
return $address;
}
function smtp_debug($message)
{
if ($this->debug)
{
echo $message;
}
}
}
?>
测试程序
代码如下:
<?php
/*
这是一个测试程序!!!
*/
require("smtp.php");
##########################################
$smtpserver = "smtp.163.com";//SMTP服务器
$smtpserverport = 25;//SMTP服务器端口
$smtpusermail = "reterry123@163.com";//SMTP服务器的用户邮箱
$smtpemailto = "dxytools@gmail.com";//发送给谁
$smtpuser = "reterry123";//SMTP服务器的用户帐号
$smtppass = "014040001";//SMTP服务器的用户密码
$mailsubject = "中文";//邮件主题
$mailbody = "<h1>中文</h1>测试下能淤泥新年感";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
##########################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = TRUE;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
?>
最新技术文章: