Tuesday, October 23, 2012

Perl: send email script

#/usr/bin/perl

use Net::SMTP;

my $SUBJECT = "[warn] Punch INET";
my $FROM = "inet-checker\@dena.jp";
my $TO = "punchvn\@gmail.com";
my $SMTP_SERVER = "SVNserver.localdomain";

my $speed=`php speedtest.php`;
$speed =~ s/\n//g;
print "$speed\n";
if ($speed < 1) {
        &sendmail("$speed Mbps");
}

sub sendmail {
        my ($sp) = @_;
        my $smtp = Net::SMTP->new($SMTP_SERVER, Hello => $SMTP_SERVER, Timeout => 300);
        $smtp->mail($ENV{USER});
        $smtp->to($TO);

        my $data = "To:$TO\n";
        $data .= "From:$FROM\n";
        $data .= "Subject:$SUBJECT\n\n";
        $data .= "INET speed is $sp";

        $smtp->data($data);
        $smtp->quit;
        print "\nSent mail to $TO\n";
}

No comments: