PHP实现.cn多域名释放监测并邮件提醒小功能
使用:宝塔或护卫神或虚拟主机环境(php5.5-7.3)使用
定期激发:宝塔护卫神定期访问网址任务,每天4.04-6.06,访问网址:本页网址?x=cha访问
场景:自己中意的.cn域名,一旦发现过期就邮件反馈(QQ邮箱+微信/QQ邮箱提醒功能)
index.php
<?php
//.cn域名定期监测是可否注册功能脚步
//使用:宝塔或护卫神或虚拟主机环境(php5.5-7.3)使用
//定期激发:宝塔护卫神定期访问网址任务,每天4.04-6.06,访问网址:本页网址?x=cha访问
//场景:自己中意的.cn域名,一旦发现过期就邮件反馈(QQ邮箱+微信/QQ邮箱提醒功能)
//作者:yujianyue 15058593138@.com 可接小功能定制
//修改以下自己关注的域名,仅支持.cn/com.cn结尾域名 一行一个
$tips = "
malide.cn 码(源码|二维码)立得,源码下载|二维码生成
echafen.cn E查分
11170.cn
aiyaha.com
mabida.com 七年老站
saolide.cn 扫(二维码/防伪码)立得
11184.cn
lidehui.cn
96448.cn 10年老站
soulide.cn 搜立得
63636.cn
71315.cn
pccx.cn
yidati.cn 易答题
";
//接受邮件
$mailx = "********@qq.com";
//配置发信邮件
$JmailUser="admin@*****.net"; //JmailUser
$JmailPass="**************"; //JmailPassword
$JmailAdds="admin@*****.net"; //JmailAddress
$Jmailsmtp="smtp.qq.com"; //Jmailsmtp
$Jmailport="465"; //mailport 推荐465
//没有把握:不建议修改以下代码
$geshu = "50"; //批量查询限制个数:建议10别超20
$biao = date("YmdHis");
$whose = array("cn","com.cn");
function QSe($domain) { //cn域名whois查询
$wsr = "whois.cnnic.net.cn";
$fp = @fsockopen($wsr, 43, $eno, $str, 1) or die("Socket Error".$eno."@".$str);
fputs($fp, $domain . "\r\n");
$out = "";
while(!feof($fp)){$out .= fgets($fp);}
if(Trim($out) == "") { return "No results!"; }
fclose($fp);
return strtolower($out);
}
if($_GET["x"] == "cha"){ //文件对应网址+?x=cha访问
$dmz=join("|",$whose);
preg_match_all("/\w+\.($dmz)/", $tips, $madm);
$doli = array_unique($madm[0]); //去重再查
if(count($doli)<1){ exit("<h2>没有识别到($dmz)域名</h2>"); }
if(count($doli)>$geshu){ exit("<h2>只提供($geshu)以内的批量查询</h2>"); }
$ii = 0; $it = "";
foreach($doli as $getal){
echo "<br>$getal:";
$dnsc = dns_get_record($getal, DNS_NS);
if(count($dnsc)>0){ //只查没有NDS记录的降低之查whois频率
echo "有解析已注册".count($dnsc);
}else{
$CACHE = QSe($getal);
if(stristr($CACHE, "No match")){
//未注册:发邮件
echo "Nomatch未注册"; $ii++; $it = "<br>$getal 未注册";
}else{
//忽略:有结果即已注册
echo "有Whois已注册";
}
sleep(1); //请务必确保查询间隔
}
}
if($ii>0){
//有未注册的 发送提醒
if($JmailUser == $mail || !file_exists("mail.php") ){
echo "<h1>未发送</h1>邮件支持文件不存在或收发邮件相同!";
}else{
$title = "提醒:{$ii}域名可注册!";
$descs = "<h3>发送给自己的即时提醒邮件:请勿拦截!</h3>$it !";
$descs .= "<p>查立得 - $biao </p>";
echo "<h1>$title</h1>".$descs;
include 'mail.php'; //邮件发送函数文件
$mail = new MySendMail();
$mail->setServer($Jmailsmtp, $JmailUser, $JmailPass, $Jmailport, true);
$mail->setFrom($JmailUser); //设置发件人
$mail->setReceiver($mailx); //设置收件人,多个收件人,调用多次
$mail->setMail($title, $descs); //设置邮件主题、内容
$mailoka = $mail->sendMail(); //发送
echo "<h1>发 $mailoka 封</h1>$mailx";
}
}
}
?>
mail.php
<?php
/**
* 邮件发送类
* 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的ssl连接
* 需要的php扩展:sockets、Fileinfo和openssl。
* 编码格式是UTF-8,传输编码格式是base64
* @example
* $mail = new MySendMail();
* $mail->setServer("smtp@126.com", "XXXXX@126.com", "XXXXX"); //设置smtp服务器,普通连接方式
* $mail->setServer("smtp.gmail.com", "XXXXX@gmail.com", "XXXXX", 465, true); //设置smtp服务器,到服务器的SSL连接
* $mail->setFrom("XXXXX"); //设置发件人
* $mail->setReceiver("XXXXX"); //设置收件人,多个收件人,调用多次
* $mail->setCc("XXXX"); //设置抄送,多个抄送,调用多次
* $mail->setBcc("XXXXX"); //设置秘密抄送,多个秘密抄送,调用多次
* $mail->addAttachment("XXXX"); //添加附件,多个附件,调用多次
* $mail->setMail("test", "<b>test</b>"); //设置邮件主题、内容
* $mail->sendMail(); //发送
*/
class MySendMail {
protected $_userName;
protected $_password;
protected $_sendServer;
protected $_port;
protected $_from;
protected $_to = array();
protected $_cc = array();
protected $_bcc = array();
protected $_subject;
protected $_body;
protected $_attachment = array();
protected $_socket;
protected $_isSecurity;
protected $_errorMessage;
public function setServer($server, $username="", $password="", $port=25, $isSecurity=false) {
$this->_sendServer = $server;
$this->_port = $port;
$this->_isSecurity = $isSecurity;
$this->_userName = empty($username) ? "" : base64_encode($username);
$this->_password = empty($password) ? "" : base64_encode($password);
return true;
}
public function setFrom($from) {
$this->_from = $from;
return true;
}
public function setReceiver($to) {
$this->_to[] = $to;
return true;
}
public function setCc($cc) {
$this->_cc[] = $cc;
return true;
}
public function setBcc($bcc) {
$this->_bcc[] = $bcc;
return true;
}
public function addAttachment($file) {
if(!file_exists($file)) {
$this->_errorMessage = "file " . $file . " does not exist.";
return false;
}
$this->_attachment[] = $file;
return true;
}
public function setMail($subject, $body) {
$this->_subject = base64_encode($subject);
$this->_body = base64_encode($body);
return true;
}
public function sendMail() {
$command = $this->getCommand();
$this->_isSecurity ? $this->socketSecurity() : $this->socket();
foreach ($command as $value) {
$result = $this->_isSecurity ? $this->sendCommandSecurity($value[0], $value[1]) : $this->sendCommand($value[0], $value[1]);
if($result) {
continue;
}
else{
return false;
}
}
$this->_isSecurity ? $this->closeSecutity() : $this->close();
return true;
}
public function error(){
if(!isset($this->_errorMessage)) {
$this->_errorMessage = "";
}
return $this->_errorMessage;
}
protected function getCommand() {
$separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符
$command = array(
array("HELO sendmail\r\n", 250)
);
if(!empty($this->_userName)){
$command[] = array("AUTH LOGIN\r\n", 334);
$command[] = array($this->_userName . "\r\n", 334);
$command[] = array($this->_password . "\r\n", 235);
}
//设置发件人
$command[] = array("MAIL FROM: <" . $this->_from . ">\r\n", 250);
$header = "FROM: <" . $this->_from . ">\r\n";
//设置收件人
if(!empty($this->_to)) {
$count = count($this->_to);
if($count == 1){
$command[] = array("RCPT TO: <" . $this->_to[0] . ">\r\n", 250);
$header .= "TO: <" . $this->_to[0] .">\r\n";
}
else{
for($i=0; $i<$count; $i++){
$command[] = array("RCPT TO: <" . $this->_to[$i] . ">\r\n", 250);
if($i == 0){
$header .= "TO: <" . $this->_to[$i] .">";
}
elseif($i + 1 == $count){
$header .= ",<" . $this->_to[$i] .">\r\n";
}
else{
$header .= ",<" . $this->_to[$i] .">";
}
}
}
}
//设置抄送
if(!empty($this->_cc)) {
$count = count($this->_cc);
if($count == 1){
$command[] = array("RCPT TO: <" . $this->_cc[0] . ">\r\n", 250);
$header .= "CC: <" . $this->_cc[0] .">\r\n";
}
else{
for($i=0; $i<$count; $i++){
$command[] = array("RCPT TO: <" . $this->_cc[$i] . ">\r\n", 250);
if($i == 0){
$header .= "CC: <" . $this->_cc[$i] .">";
}
elseif($i + 1 == $count){
$header .= ",<" . $this->_cc[$i] .">\r\n";
}
else{
$header .= ",<" . $this->_cc[$i] .">";
}
}
}
}
//设置秘密抄送
if(!empty($this->_bcc)) {
$count = count($this->_bcc);
if($count == 1) {
$command[] = array("RCPT TO: <" . $this->_bcc[0] . ">\r\n", 250);
$header .= "BCC: <" . $this->_bcc[0] .">\r\n";
}
else{
for($i=0; $i<$count; $i++){
$command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">\r\n", 250);
if($i == 0){
$header .= "BCC: <" . $this->_bcc[$i] .">";
}
elseif($i + 1 == $count){
$header .= ",<" . $this->_bcc[$i] .">\r\n";
}
else{
$header .= ",<" . $this->_bcc[$i] .">";
}
}
}
}
//主题
$header .= "Subject: =?UTF-8?B?" . $this->_subject ."?=\r\n";
if(isset($this->_attachment)) {
//含有附件的邮件头需要声明成这个
$header .= "Content-Type: multipart/mixed;\r\n";
}
elseif(false){
//邮件体含有图片资源的,且包含的图片在邮件内部时声明成这个,如果是引用的远程图片,就不需要了
$header .= "Content-Type: multipart/related;\r\n";
}
else{
//html或者纯文本的邮件声明成这个
$header .= "Content-Type: multipart/alternative;\r\n";
}
//邮件头分隔符
$header .= "\t" . 'boundary="' . $separator . '"';
$header .= "\r\nMIME-Version: 1.0\r\n";
//这里开始是邮件的body部分,body部分分成几段发送
$header .= "\r\n--" . $separator . "\r\n";
$header .= "Content-Type:text/html; charset=utf-8\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n\r\n";
$header .= $this->_body . "\r\n";
$header .= "--" . $separator . "\r\n";
//加入附件
if(!empty($this->_attachment)){
$count = count($this->_attachment);
for($i=0; $i<$count; $i++){
$header .= "\r\n--" . $separator . "\r\n";
$header .= "Content-Type: " . $this->getMIMEType($this->_attachment[$i]) . '; name="=?UTF-8?B?' . base64_encode( basename($this->_attachment[$i]) ) . '?="' . "\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= 'Content-Disposition: attachment; filename="=?UTF-8?B?' . base64_encode( basename($this->_attachment[$i]) ) . '?="' . "\r\n";
$header .= "\r\n";
$header .= $this->readFile($this->_attachment[$i]);
$header .= "\r\n--" . $separator . "\r\n";
}
}
//结束邮件数据发送
$header .= "\r\n.\r\n";
$command[] = array("DATA\r\n", 354);
$command[] = array($header, 250);
$command[] = array("QUIT\r\n", 221);
return $command;
}
protected function sendCommand($command, $code) {
//echo 'Send command:' . $command . ',expected code:' . $code . '<br />';
//发送命令给服务器
try{
if(socket_write($this->_socket, $command, strlen($command))){
if(empty($code)) {
return true;
}
$data = trim(socket_read($this->_socket, 1024));
//echo 'response:' . $data . '<br /><br />';
if($data) {
$pattern = "/^".$code."+?/";
if(preg_match($pattern, $data)) {
return true;
}
else{
$this->_errorMessage = "Error:" . $data . "|**| command:";
return false;
}
}
else{
$this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
return false;
}
}
else{
$this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
return false;
}
}catch(Exception $e) {
$this->_errorMessage = "Error:" . $e->getMessage();
}
}
protected function sendCommandSecurity($command, $code) {
//echo 'Send command:' . $command . ',expected code:' . $code . '<br />';
try {
if(fwrite($this->_socket, $command)){
//当邮件内容分多次发送时,没有$code,服务器没有返回
if(empty($code)) {
return true;
}
//读取服务器返回
$data = trim(fread($this->_socket, 1024));
//echo 'response:' . $data . '<br /><br />';
if($data) {
$pattern = "/^".$code."+?/";
if(preg_match($pattern, $data)) {
return true;
}
else{
$this->_errorMessage = "Error:" . $data . "|**| command:";
return false;
}
}
else{
return false;
}
}
else{
$this->_errorMessage = "Error: " . $command . " send failed";
return false;
}
}catch(Exception $e) {
$this->_errorMessage = "Error:" . $e->getMessage();
}
}
protected function readFile($file) {
if(file_exists($file)) {
$file_obj = file_get_contents($file);
return base64_encode($file_obj);
}
else {
$this->_errorMessage = "file " . $file . " dose not exist";
return false;
}
}
protected function getMIMEType($file) {
if(file_exists($file)) {
if(stristr($file."@", ".zip@")){
return "application/zip";
}
return mime_content_type($file);
}else {
return false; //false
}
}
protected function socket() {
//创建socket资源
$this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
if(!$this->_socket) {
$this->_errorMessage = socket_strerror(socket_last_error());
return false;
}
socket_set_block($this->_socket);//设置阻塞模式
//连接服务器
if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
$this->_errorMessage = socket_strerror(socket_last_error());
return false;
}
$str = socket_read($this->_socket, 1024);
if(!preg_match("/220+?/", $str)){
$this->_errorMessage = $str;
return false;
}
return true;
}
protected function socketSecurity() {
$remoteAddr = "tcp://" . $this->_sendServer . ":" . $this->_port;
$this->_socket = stream_socket_client($remoteAddr, $errno, $errstr, 30);
if(!$this->_socket){
$this->_errorMessage = $errstr;
return false;
}
stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
stream_set_blocking($this->_socket, 1); //设置阻塞模式
$str = fread($this->_socket, 1024);
if(!preg_match("/220+?/", $str)){
$this->_errorMessage = $str;
return false;
}
return true;
}
/**
* 关闭socket
* @access protected
* @return boolean
*/
protected function close() {
if(isset($this->_socket) && is_object($this->_socket)) {
$this->_socket->close();
return true;
}
$this->_errorMessage = "No resource can to be close";
return false;
}
protected function closeSecutity() {
if(isset($this->_socket) && is_object($this->_socket)) {
stream_socket_shutdown($this->_socket, STREAM_SHUT_WR);
return true;
}
$this->_errorMessage = "No resource can to be close";
return false;
}
}