Email Notification When Perl CGI Script Is Used
I'll show you how to make your Perl CGI scripts send you an email every time they are used. These instructions are for Unix/Linux servers with sendmail on board.
(For information about modifying PHP scripts to send an email when they are used, see the Email Notification When PHP Script Is Used library article.)
Make a Backup Copy
Make a backup copy. If something should go wrong with your edits, you can restore your script with the backup.
Edit Your Script With a Plain Text Word Processor
Use a plain text word processor to make the edits to your scripts. NotePad and BBEdit are two plain text word processors. Others are also available.
WYSIWYG word processors, web page makers, and any program that generates rich or formatted text, can introduce script-breaking formatting codes into the file.
Where To Insert the Emailing Code
Insert the emailing code immediately below the first line of the script that does not start with a hash ("#") character.
Here is the emailing code:
open MYMAIL,"|/path/to/sendmail -t"; print MYMAIL <<EndOfMYMAIL; To: me\@domain.com Subject: A script was used Hi, This email comes from $0 Me EndOfMYMAIL close MYMAIL;
The first, second, and last lines of the above code may be indented. All other lines must begin at the left margin.
Customizing the Emailing Code
On the first line of the code, modify the directory path to sendmail (keep the -t) or to qmail for your server. Keep the vertical bar character as is.
(If you don't know the directory path to your sendmail, your hosting company's tech support should be able to tell you. Or, you can use Master Pre-Installation Tester to help locate sendmail.)
On the third line of the code, customize the To: email address. Note that the reverse slash character must precede the @ character.
The Subject: ... on line four may be customized.
The body content is at lines six through ten. It may be modified to suit your needs.
Special Identifying Variables
If you would like to insert some identifying information into the subject or body content, these are available:
-
Type $0 (that's a digit zero, not a letter O) where you want the name of the script to appear.
-
Type $ENV{HTTP_REFERER} where you want the URL of the referrer, which would probably be the URL of the form being used.
-
Type $ENV{REMOTE_ADDR} where you want the user's IP address.
Other Customization Considerations
@ symbols must be escaped with a backward-slash:
Unless they're used as the first character of a variable name (like the special identifying variables above), $ symbols must be escaped with a backward-slash.
Giving the Email Cc, and Bcc Destinations
Header lines Cc:, and/or Bcc: may be specified. Put them below the line beginning with "print" and above the first blank line. Between the To: and Subject: header lines would be good.
(A From: header line may also be specified in the same manner.)
Here is an example:
open MYMAIL,"|/path/to/sendmail -t"; print MYMAIL <<EndOfMYMAIL; To: me\@domain.com Cc: name2\@domain.com Bcc: name3\@domain.com From: fromname\@domain.com Subject: A script was used Hi, This email comes from $0 Me EndOfMYMAIL close MYMAIL;
Installing and Testing the Script
Upload the modified script as ASCII/plain text.
Use the script to test it.
If the Script Does Not Run
If the script now gives you "Internal Server Error" or "Premature End Of Script Header" errors, the Frequently Asked Questions page at the Master Series CGI support center might be able to help.
If the Script Will Not Send Email
If the script runs, but no mail arrives, see the "Reasons Why Scripts Won't Send Email" article for suggestions.
If Everything Works
Congratulations on a successful modification!
Will Bontrager