Domain Expiration Notify Script
Script #2 - Notification
As I stated previously, the domain notification script is actually composed of two seperate files. Whereas the first script is to be run in CRON on an interval, the second script only runs once a day in CRON. The sole purpose of this second script is to run through all the data collected in the database created by the first script, and check for any domains that are to expire soon. It then notifies intended recipients. I chose to have notifications sent at 60 days before expiration, 30 days before expiration, 14 days before expiration, and every day from 7 days before expiration onward. This keeps the notifications from becoming too annoying, but really makes sure you don't miss a beat when it counts.
<?
/*************************************************************
* Domain Renewal Notifier
* File Name: domainnotify.php
*------------------------------------------------------------
* Author: Wayne Eggert
* Email: wayne@techdose.com
*
* DESCRIPTION: Notifies a user via email when domains are
* coming up for renewal.
* Emails @ 60-days, 30-days, 14-days, and <7days
*************************************************************/
include("database.php"); // replace with your database connection include or a mysql_connect, etc of your own
// grab all the domains so we can check their expirations and status
// to determine if an email has to be sent.
$sql = "SELECT domainName,registrar,expiration,status from domains WHERE DATE_ADD(expiration,INTERVAL -60 DAY) <= CURRENT_DATE()";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$expTimestamp = strtotime($row[expiration]);
$curTimestamp = strtotime(date("Y-m-d",time()));
$daysLeft = (($expTimestamp-$curTimestamp)/24/60/60);
// email when 60 days are left
if($daysLeft == 60){
$sixty_days .= $row[domainName]."\n";
$sixty_days .= "Registrar: ".$row[registrar]."\n";
$sixty_days .= "Expires: ".$row[expiration]."\n\n";
}
// email when only 30 days are left
if($daysLeft == 30){
$thirty_days .= $row[domainName]."\n";
$thirty_days .= "Registrar: ".$row[registrar]."\n";
$thirty_days .= "Expires: ".$row[expiration]."\n\n";
}
// email when only 14 days are left
if($daysLeft == 14){
$fourteen_days .= $row[domainName]."\n";
$fourteen_days .= "Registrar: ".$row[registrar]."\n";
$fourteen_days .= "Expires: ".$row[expiration]."\n\n";
}
// email every day from 7 days before expiration
if($daysLeft <= 7){
$seven_or_less .= $row[domainName]."\n";
$seven_or_less .= "Registrar: ".$row[registrar]."\n";
$seven_or_less .= "Expires: ".$row[expiration]."\n\n";
}
if($row[status]!='ACTIVE' && $row[status]!='REGISTRAR-LOCK' && $row[status]!='CLIENT UPDATE PROHIBITED'){
$status_change .= $row[domainName]."\n";
$status_change .= "Status: ".$row[status]."\n\n";
}
}
// compose and send email if needed
if($sixty_days != '' || $thirty_days != '' || $fourteen_days !='' || $seven_or_less != '' || $status_change != ''){
if($seven_or_less != ''){
$msg.="**WARNING**\nTHE FOLLOWING DOMAINS EXPIRE IN 7 DAYS OR LESS:\n";
$msg.=$seven_or_less;
}
if($fourteen_days != ''){
$msg.="THE FOLLOWING DOMAINS EXPIRE IN 14 DAYS:\n";
$msg.=$fourteen_days;
}
if($thirty_days != ''){
$msg.="THE FOLLOWING DOMAINS EXPIRE IN 30 DAYS:\n";
$msg.=$thirty_days;
}
if($sixty_days != ''){
$msg.="THE FOLLOWING DOMAINS EXPIRE IN 60 DAYS:\n";
$msg.=$sixty_days;
}
if($status_change != ''){
$msg.="THERE MAY BE A STATUS PROBLEM ON THE FOLLOWING DOMAINS:\n";
$msg.=$status_change;
}
$subject='Domains Are Expiring Soon! '.date("m-d-Y",time());
$headers = "From: System Admin <domainexpirations@yourdomain.com>\r\n";
$headers .= "X-Sender: <domainexpirations@yourdomain.com>\r\n";
$headers .= "Content-Type: text; charset=iso-8859-1\r\n";
mail("youremail@yourdomain.com", $subject, $msg, $headers);
}
?>
Explanation:
- The first line is just a database include I created. Replace this with your include file or MySQL connection code.
- The WHILE loop goes though the domains, one at a time.
- Upon finding an issue with any of the ports being checked, the $problem variable will be set to true.
- Finally, the script emails a notification to the user if any of the ports could not be reached.
Adding to CRON
These scripts are intended to be run periodically on a server. The first script should be run every 15-30 minutes, as to not flood WHOIS servers or cause problems with retrieving WHOIS information. The second script should be run once a day, to send out notifications of domain expirations.
To add to CRON add the following lines to /etc/crontab or root's crontab:
*/30 * * * * root lynx -dump http://www.yourdomain.com/whois.php 1> /dev/null 2> /dev/null
00 1 * * * root lynx -dump http://www.yourdomain.com/domainnotify.php 1> /dev/null 2> /dev/null
*Be sure to replace www.yourdomain.com with the appropriate http location of your script and the script names with whatever names you saved the script code to.
You could also run these scripts using php's cgi executable, if you have it installed on your system (likely in /usr/bin/php).
Overview
These scripts are meant to give you some ideas on how to solve the problems associated with domain renewals. There are some third-party sites available that will gladly take your money to notify you of domain status changes or domain expirations, but if you're tight on cash or don't have a large number of domains to monitor, these scripts should do just the trick.
Comments:
| How to set cron tab |
| Posted 10/16/06 12:01AM by creativetest |
| How to set cron tab |
| how to get the registrar |
| Posted 06/03/07 3:40PM by rocko |
|
Hi, I see your script. It is really good and help me a lot. But Im trying to use a registrar field. I use different string functions but I couldn't. There are any way to use the registrar field? Thanks in advance. |

