Thread: Perl + Sendmail

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    63
    Rep Power
    14
    This is kind of complicated and I hope I'm explaining it properly.

    What I'm trying to do is create a set of scripts attached to an alias in sendmail so that I can get the local weather on my 2-way pager.

    So if I send an email to say getweather@foo.com, sendmail will route the message to a perl script that gets the current weather information from the National Weather Service METAR data, formats it into an e-mail and sends it back to my pager.

    Does anyone know of a script that is currently available or have any suggestions? I already know how to get sendmail to direct messages to a PERL script and I can get that script to e-mail a message back to me. The problem I'm having is getting the data from METAR.

    Any help will be greatly appreciated.

    Thanks,

    Chris.

    [This message has been edited by chris22 (edited November 18, 2000).]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    71
    Rep Power
    14
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by chris22:
    ... sendmail will route the message to a perl script that gets the current weather information...[/quote]

    Could you please tell me how do you do that. I'm currently using script for reading email POP3 account which is run by cron command. I wonder if there is better way?

    I have some scripts that extract information from other pages, but what is METAR ?

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    63
    Rep Power
    14
    The script I'm using is a modified version of mailback ( http://www.cynicism.com/~derek/mailback/ ).

    What you want to do is set up an aliases in sendmail that redirects incoming messages to this perl script.

    e.g.

    admin: "|/mail/mailback.pl"

    Of course, make sure you run newaliases after making this change. Also, make sure your mailback.pl script is executable.

    Also, create a file in the same dir as you put the script called message. What ever you put in that file will be the content of the returned message.

    METAR is basically an hourly weather report that is posted to the NOAA website in plain text format (see http://weather.noaa.gov/pub/data/obs...etar/stations/ ). Information about decoding them is here.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    #!/usr/bin/perl
    $path = "/mail";
    $logging = "yes";
    $sendmail = "/usr/sbin/sendmail";
    $includeOriginal = "yes";
    undef $/;
    $incoming = <>;
    ($inHeader, $inMessage) = split(/nn/,$incoming,2);

    $time = localtime(time);

    @headers = split(/n/,$inHeader);
    foreach $header (@headers) {
    ($junk, $mail{'Subject'}) = split(/: /,$header) if $header =~ m/^Subject:/;
    ($junk, $mail{'From'}) = split(/: /,$header) if $header =~ m/^To:/;
    ($junk, $mail{'To'}) = split(/: /,$header) if $header =~ m/^From:/;
    ($junk, $mail{'To'}) = split(/: /,$header) if $header =~ m/^Reply-To:/;
    }
    if ($mail{'From'} =~ m/@/) {
    ($id,$junk) = split(/@/,$mail{'From'});
    }
    else {
    $id = $mail{'From'};
    }

    &checkExceptions;
    &log("autoresponse") if $logging;

    # open MESSAGE, "$path/message-$id";
    open MESSAGE, "$path/message";
    $mail{'Message'} = <MESSAGE>;
    close MESSAGE;

    &sendMessage;

    sub sendMessage {
    $sendmailMessage = "From: $mail{'From'}nDate: $timenTo: $mail{'To'}nSubject: Re: $mail{'Subject'}nn$mail{'Message'}";
    if ($includeOriginal) {
    $inMessage =~ s/^/>/g;
    $inMessage =~ s/n/n>/g;
    $inMessage = "nYou wrote:nn" . $inMessage;
    $sendmailMessage = "From: $mail{'From'}nDate: $timenTo: $mail{'To'}nSubject: Re: $mail{'Subject'}nn$mail{'Message'}$inMessage";
    } else {
    $sendmailMessage = "From: $mail{'From'}nDate: $timenTo: $mail{'To'}nSubject: Re: $mail{'Subject'}nn$mail{'Message'}";
    }
    open (MAIL , "|$sendmail -t");
    print MAIL $sendmailMessage;
    print $sendmailMessage;
    close (MAIL);
    }

    sub checkExceptions {
    local $realSender = $mail{'To'};
    $realSender =~ s/.*<(.*)>/$1/;
    open (EXCEPTIONS, "$path/exceptions-$id");
    foreach $person (<EXCEPTIONS> ) {
    chop($person);
    if ($realSender eq $person) {
    &log("excepted");
    exit;
    }
    }
    }

    sub log {
    $action = shift @_;
    open (LOG, ">>$path/log");
    print LOG "$time : $action to $mail{'To'} for $mail{'From'} regarding $mail{'Subject'}n";
    }
    [/code]

    [This message has been edited by chris22 (edited November 19, 2000).]

IMN logo majestic logo threadwatch logo seochat tools logo