开发者

My perl script wont send an email

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜