my perl script won't send any mails

Need help with C, C++, perl, python, etc?

my perl script won't send any mails

Postby cc » 2006-11-10 16:04


I have this perl script, but it won't send any mails:
Code: Select all
#!/usr/bin/perl -w

use strict;
use warnings;

my $PingHost = "";
my $ExpectedRedirect = "";
my $mailto = "admin\";

my $ip = 'ip route flush cache';

open( INPING, "ping -c 4 $PingHost|" ) || die "ping open failed";
while( my $line = <INPING> ) {
  next unless( $line =~ /Redirect Host\(New nexthop: (.*)\)/ );
  next if( $1 eq $ExpectedRedirect );

   open( OUTMAIL, "| mailx -s 'CH Branch VPN Unexpected Redirect: $1' $mailto" ) || die "pipe to mail failed";
   print OUTMAIL scalar localtime();
   print OUTMAIL "\n\n";
   print OUTMAIL "Received an unexpected redirect \n";
   print OUTMAIL "\n";
   print OUTMAIL "have a nice day\n";   
close(INPING) || warn "bad pipe close";


knows someone what's wrong and howto solve this problem ?
Posts: 820
Joined: 2005-06-08 19:14

Postby kink » 2006-11-11 11:58

From a cursory look my best guess is that your regular expression does not match and therefore no mail is sent. You could try to deduce where it fails by adding some debug statements.
User avatar
Debian Developer, Site Admin
Debian Developer, Site Admin
Posts: 287
Joined: 2006-01-02 16:47
Location: Utrecht, The Netherlands

Postby drl » 2006-11-11 14:36


I checked that my mailx would send messages, then I created the stripped-down version of your script as noted below and it worked for me.

You could remove the test and see if the print will work. You could also make the print go to STDOUT if you suspect the mailx-pipe chain ... cheers, drl
Code: Select all

# @(#) p1       Demonstrate mail from perl script.

use strict;
use warnings;
use Carp;

my ($mail);
my ($program)   = "mailx";
my ($recipient) = "drl";
my ($domain)    = '@leap';

open $mail, "|-", "$program -s test $recipient$domain"
    || croak("Can't fork $program, error $!");

print $mail scalar localtime(), "\n";
print $mail "hello, world.\n";

close $mail || croak("Can't close $program: error $!");

["Sure, I can help you with that." -- USBank voice recognition system.
( Mn, 2.6.x, AMD-64 3000+, ASUS A8V Deluxe, 3 GB, SATA + IDE, NV34 )
Debian Wiki | Packages | Backports | Netinstall
User avatar
Posts: 434
Joined: 2006-09-20 02:27
Location: Saint Paul, Minnesota, USA

Postby arnestig » 2006-11-21 09:50

example using sendmail:
Code: Select all
open (OUT,"|/usr/sbin/sendmail -F 'input_the_sender_name' -f sender_adress\ -t");
print(OUT "To: send_to_adress\\n");
print(OUT "Subject: "Just an example"\n");
print(OUT "\n");
print(OUT "Test example"\n");
User avatar
Posts: 13
Joined: 2006-11-21 09:19
Location: Grästorp, Sweden

Return to Programming

Who is online

Users browsing this forum: No registered users and 7 guests