0

This is what I've got:

# Examples:
#   logman-parse --pipe=[/path/to/pipe] --mail=[mail address]
#   logman-parse --pipe=/etc/pipes/critical --mail=root@domain.net
#   logman-parse --pipe=/home/user/pipe --mail=john.doe@gmail.com

use warnings;
use strict;
use Getopt::Long;
use Mail::Sendmail;

my $pipe;
my $mailto;

GetOptions( "pipe=s"   => \$pipe,
            "mailto=s" => \$mailto
          ) or die "Could not parse command line arguments";

# Check to see if there are no arguments left over, the email address 
# supplied is valid and the file given is in fact a pipe.
my $email_regex = qr/\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+/;

@ARGV == 0                or die "Too many arguments supplied";
$mailto =~ $email_regex   or die "Invalid email supplied";
-p $pipe                  or die "Pipe supplied does not exist";

# Load current contents of pipe into array @pipe_lines
open( my $pipe_handle, "<", $pipe) or die "Cannot open $pipe";
my @pipe_lines = <$pipe_handle>;

# Remove duplicate array entries by first adding them all to a hash,
# then extracting the keys into another array named @uniques
my @uniques = ();
my %seen    = ();
foreach my $line (@pipe_lines) {
    if ( $seen{$line} ) {
        $seen{$line}++;
        next;
    }
    push(@uniques, $line);
}

# Formatting each value to $date, $hostname, $facility and $message. 
# Then send the formatted text.
for my $i (0 .. $#uniques) {

    # Grab each component of the log entry
    (my $timestamp, my $rest) = unpack('A16 A*', $uniques[$i]);
    my @else = split(/ /, $rest, 3);
    my $formatted_message  = "Time: " . $timestamp . "\n";
    $formatted_message    .= "Hostname: " . $else[0] . "\n";
    $formatted_message    .= "Subject: " . $else[1] . "\n";
    $formatted_message    .= "Message: " . $else[2] . "\n";

    print $formatted_message."\n";

    # Send the message
    my %mail = ( To      => $mailto,
                 From    => 'logman@localhost.localdomain',
                 Subject => 'LOGMAN: '.$else[0],
                 Message => $formatted_message
               );

    sendmail(%mail) or die $Mail::Sendmail::error;
}

I've double checked all the variables and everything seems to be working fine, however, it doesn't send the email. I don't even get any error messages. Any ideas?

EDIT: I'm using the Mail::Sendmail module. I seem to be using it correctly, so I don't know why it isn't working.

  • did you ask it nicely? Seriously, do you have an SMTP server installed? Have you configured `Sendmail.pm` to use it? – Pedro Silva Aug 09 '11 at 19:32
  • This is where I look like an idiot. I'm supposed to configure Sendmail? –  Aug 09 '11 at 20:22

1 Answers1

3

The LIMITATIONS section of the Mail::Sendmail docs mentions that it connects to a SMTP server on localhost unless you modify Sendmail.pm or pass a different server name in your script (as mentioned under CONFIGURATION). You didn't specify a server in your script, and you don't mention customizing Sendmail.pm, so it's probably using localhost.

Since you're not getting an error, it's likely that you do have a SMTP server running on localhost. But since you're not getting the email, it's likely that SMTP server is misconfigured. It accepts email, but can't deliver it.

You should also try printing $Mail::Sendmail::log after calling sendmail. That will tell you more about what happened.

cjm
  • 61,471
  • 9
  • 126
  • 175
  • 1
    Yeah, this bit is a bit misleading: `Simple platform independent e-mail from your perl script. Only requires Perl 5 and a network connection.` – Pedro Silva Aug 09 '11 at 21:44
  • Thanks I'll try printing the log tomorrow at work. However, this is on a fairly fresh install of Red Hat and I never touched `sendmail`. –  Aug 09 '11 at 22:42