Email Notification When PHP Script Is Used
Your PHP scripts can send you an email every time they are used.
Link redirectors, form loaders, page load counters, image delivery scripts, pretty much any PHP script can be modified to send you an email when it is used.
(For information about modifying Perl CGI scripts to send an email when they are used, see the Email Notification When Perl CGI Script Is Used library article.)
The modification consists of inserting 1 line into the PHP script.
I'll first describe the line to insert; then talk about where in the PHP script to insert it.
The Line To Insert
To have a PHP script send you an email, insert 1 line into the script. One line will do it. Here is a prototype:
mail("Destination-address","Email-subject","Email-body");
The line is a call to the mail() function. The 3 parameters mail() must have are the destination, the subject, and the email content.
Later, I'll describe how to put the line into PHP script. But first, here is an example of the PHP line to insert:
mail("will@example.com","Script Used","Image swap script was used.");
Some servers are configured to insert the email from address as "nobody" or "anonymous" unless a from address is specified. Specify a from address as a fourth parameter to the mail() function. The format is "From: name@example.com".
Example:
mail("will@example.com","Script Used","Image swap script was used.","From: will@example.com");
The location of the script can be in the email subject line and/or in the email content. The $_SERVER["PHP_SELF"] variable contains the script's location.
These two examples have the script location as the email subject and the script location as part of the email content, respectively.
mail("will@example.com",$_SERVER["PHP_SELF"],"Image swap script was used.","From: will@example.com");
mail("will@example.com","Script Used","Script " . $_SERVER["PHP_SELF"] . " was used.","From: will@example.com");
Where To Insert the Line into the PHP Script
Before making any changes to a script, make a backup copy. If something should go wrong with your edits, restore your script with the backup.
These are generic instructions. It may be impractical or impossible to insert the line in every PHP script according to these instructions. Each PHP script is different and I don't know what your script contains.
Be that as it may, the instructions should work just fine for most PHP scripts.
Here are the instructions:
Insert the line to send you an email immediately following the PHP code block starting characters. The characters are either <?php or <?
Example:
<?php mail("will@example.com","Script Used",$_SERVER["PHP_SELF"],"From: will@example.com"); /* original PHP code */ ?>
Upload the modified PHP script to the server and test it. It should send you an email.
If you don't received the email, verify the email address at the first mail() parameter is correct. If yes, one of these situations may apply:
-
Email filters put the email into the junk or trash area.
-
Your ISP filtered the email and never sent it to you. (Some actually do this.)
-
The PHP script broke when the line was added.
-
The hosting company has mail() function restrictions in place. (Some do, even though mail() is a standard built-in PHP function.)
If you don't receive an email, make a copy of the edited PHP script and restore the original with the backup. Upload the restored copy to the server so it will work as before.
To try to determine the reason for non-delivery, create a short PHP script, like this:
<?php mail("will@example.com","Script Used",$_SERVER["PHP_SELF"],"From: will@example.com"); ?> -done-
Replace the mail(...) line with the line you previously inserted into your PHP script.
Upload the PHP script to your server and type its URL into your browser. When the word "-done-" appears in your browser, the email should have been sent.
If you receive the email now, but didn't previously when the mail(...) line was inserted into the other script, it may be because inserting the line broke the script.
But if you still don't receive the email, it is likely a filtering issue or the hosting company has imposed emailing restrictions. If your hosting company's tech support says there are no restrictions imposed on emailing from your server, then it's likely a filtering issue.
Will Bontrager