Email Notification from any Script
If you want to be notified every time a certain script on
your site is used, here is how to do it. (Not applicable
to NT servers, only UNIX/Linux. Few NT's can use the
sendmail mailing code presented here.)
- Download your working script as ASCII/plain text.
- Make a backup copy. If something should go wrong
with your edits, you can restore your script with
the backup.
- Load the script into an ASCII/plain word processor
like NotePad or BBEdit.
- The first line of Perl scripts begin with a sharp
symbol (also called "pound" or "hash"): #
Sometimes several subsequent lines also begin with
the sharp symbol.
Find the first line in the script that does not
begin with a sharp symbol. At that point, insert
the following 16 lines:
open MYaltMAIL,"|/path/to/sendmail -t";
print MYaltMAIL <<EndOfMyAltMAIL;
To: me\@domain.com
Subject: Customize subject line
Hi,
I come from $0
This is worth lots of \$'s!
Sincerely,
Me
EndOfMyAltMAIL
close MYaltMAIL;
Lines three through fifteen of the code may have no
left margin. For the first, second, and last lines,
a left margin is optional.
- 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: |
(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 find it.)
- On the third line of the code, customize the
To: email addresses.
- On the fourth line of the code, customize the
Subject: text.
- The first blank line must follow the Subject: line
(that first blank line separates the email header
from the body content).
- On lines six through twelve, customize the body
content to suit. (Leave the line containing only
"EndOfMyAltMAIL" as it is, on a line by itself,
no left margin, and no punctuation.)
If you would like to insert some identifying
information, you can use the indicated variables:
- 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.
Any of the above identifying variables can also be
used in the Subject: line, if you wish.
- "@" symbols must be escaped with a backward-slash: \
- Unless they're used as the first character of a
variable name (like the identifying variables in
step 9), "$" symbols must be escaped with a
backward-slash: \
- Header lines From:, 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. Example:
To: me\@domain.com
From: fromname\@domain.com
Cc: name2\@domain.com
Bcc: name3\@domain.com
Subject: Customize subject line
- Upload the modified script as ASCII/plain text.
- Test it.
- If the script works but you get no mail, either
- the directory path to sendmail is incorrect,
- the To: address is wrong, or
- (if the To: address is to the same domain where
sendmail is installed) your sendmail might be
refusing to send mail to its own domain.
If you suspect the latter (only a few sendmail
installations are configured this way), get an
email address outside your domain, such as from
http://usa.net or http://hotmail.com, and try again.
If it still doesn't send mail, check the other two
points. The reason is almost certain to be one of
the three points mentioned.
- If the script now gives you "Internal Server Error"
or "Premature End Of Script Header" errors, use the
points listed at the CGI FAQ to debug.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2001 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.