#!/usr/bin/perl
############################################################

## Initialisation.
# First we say what other routines we will be using.
use strict 'vars';
my $mailer = '/usr/lib/sendmail -t';
#my $mailer = '/var/qmail/bin/sendmail -t';
my $date = &getRFC822date(time());

$| = 1; # just in case -- actually makes no difference, it seems one
        # of Apache/Boa/CGIWrap is buffering the output anyway.

print "Content-Type: text/plain\n\nFirst some random junk to show that we are actually working...\n";
print ((('#'x80)."\n")x128);
print "\nOk, About to send mail...\n";

open (MAIL, "| $mailer");
print MAIL 
"From: py8ieh\@bath.ac.uk
To: py8ieh\@bath.ac.uk
Subject: testing sendmail
Date: $date

Well, the e-mail got there, but did the script finish?\n\n";
foreach my $key (sort(keys(%ENV))) { print MAIL "$key = $ENV{$key} \n"; }
close (MAIL);

print "Oh. It worked.\n";

exit;



sub getRFC822date {
    my($now) = @_;

    # My version:
    # my($tsec,$tmin,$thour,$tmday,$tmon,$tyear,$twday,$tyday,$tisdst) = gmtime($now);
    # $tyear += 1900; # as mentioned below, this is not RFC822 compliant, but is Y2K-safe.
    # $tsec = "0$tsec" if $tsec < 10;
    # $tmin = "0$tmin" if $tmin < 10;
    # $thour = "0$thour" if $thour < 10;
    # $tmon = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")[$tmon];
    # $twday = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")[$twday];
    # return = "$twday, $tmday $tmon $tyear $thour:$tmin:$tsec GMT";

    # "Better" version:
    # Returns today's date as an RFC822 compliant string with the
    # exception that the year is returned as four digits.  In my
    # extremely valuable opinion RFC822 was wrong to specify the year
    # as two digits.  Many email systems generate four-digit years.
    #  DHD  January 2000
    use POSIX;
    my ($oldlocale, $date);
    $oldlocale = POSIX::setlocale (LC_TIME); # save the old locale
    POSIX::setlocale (LC_TIME, "en"); # set the locale to RFC822's
    $date = POSIX::strftime ("%a, %e %b %Y %T %Z", localtime($now)); # generate the local time string
    POSIX::setlocale (LC_TIME, $oldlocale); # revert the locale (not needed?)
    return $date;
}                              
