这就是我所拥有的:
# 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;
}
我已经仔细检查了所有变量,一切似乎都运行良好,但是,它不会发送电子邮件。我甚至没有收到任何错误消息。有什么想法吗?
编辑:我正在使用Mail :: Sendmail模块。我似乎正确地使用它,所以我不知道它为什么不起作用。
答案 0 :(得分:3)
LIMITATIONS文档的Mail::Sendmail部分提到它连接到localhost上的SMTP服务器,除非您修改Sendmail.pm或在脚本中传递不同的服务器名称(如{{3中所述) }})。您没有在脚本中指定服务器,也没有提及自定义Sendmail.pm,因此它可能使用localhost。
由于您没有收到错误,因此您可能确实在localhost上运行了SMTP服务器。但由于您没有收到电子邮件,因此SMTP服务器可能配置错误。它接受电子邮件,但无法提供。
您还应该在致电$Mail::Sendmail::log
后尝试打印sendmail
。这将告诉你更多关于发生了什么。