#!/usr/bin/perl use warnings; use LWP::Simple; use Email::Send; use Email::Send::Gmail; use Email::MIME::Creator; use Net::Ping::External qw(ping); use File::Fetch; use Tie::File; my $testfolder = "/Users/Appy/Desktop/"; my $to = "test2\@gmail.com"; my $from = "test1\@gmail.com"; my $date = localtime(); my $count = 0; my $logfilelocation = "/Users/Appy/Desktop/"; tie @file, 'Tie::File', $testfolder . "testfile.txt" or die; foreach $URL (@file) { my $string1 = $URL; if ( $string1 =~ m/ # Match ping\s # 'ping ' /ix # i = Ignore case # x = allow the regexp to go over multiple lines ) { my $URI = substr $URL, 8, 28; print "$URI\n"; &myping($URI,$URL); sub myping { my $newURI = $_[0]; my $newURL = $_[1]; my $alive = ping(host => "$newURI"); if ($alive) { print "$newURI is active.\n"; $count = 0; } else { print "$newURI is inactive\n"; my $c = substr $newURL, 5, 2; if ($count<$c) { $count = $count + 1; print "$count\n"; &myping; } elsif ($count==$c) { my $logfile = $logfilelocation . "log.txt"; my $logmsg = "$date cannot ping $newURI"; open LOGFILE, ">>$logfile" or die "cannot open $logfile for append: $!"; print LOGFILE $logmsg, "\n"; close LOGFILE; my $emailadd = substr $newURL, 22; @personal = split(/,/, $emailadd); foreach $eadd (@personal) { my $email = Email::MIME->create( header => [ From => $from, To => $eadd, Subject => $logmsg, ], attributes => { filename =>"log.txt", content_type =>"application/text", disposition =>"attachment", Name =>"/Desktop/log.txt", }, body => "$newURI is not working" ); my $sender = Email::Send->new( { mailer => 'Gmail', mailer_args => [ username => 'appy.test1@gmail.com', password => '1234qwerty', ] } ); eval { $sender->send($email) }; die "Error sending email: $@" if $@ } } } } } }