2011-08-09 73 views
0

這是我有:我的Perl腳本不會發送電子郵件

# Examples: 
# logman-parse --pipe=[/path/to/pipe] --mail=[mail address] 
# logman-parse --pipe=/etc/pipes/critical [email protected] 
# logman-parse --pipe=/home/user/pipe [email protected] 

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._%-][email protected][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 => '[email protected]localdomain', 
       Subject => 'LOGMAN: '.$else[0], 
       Message => $formatted_message 
       ); 

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

我仔細檢查過所有的變量,一切似乎是工作的罰款,但是,它不發送電子郵件。我甚至沒有收到任何錯誤消息。有任何想法嗎?

編輯:我正在使用Mail :: Sendmail模塊。我似乎正確使用它,所以我不知道它爲什麼不起作用。

+0

你問得好嗎?真的,你有沒有安裝SMTP服務器?你配置了'Sendmail.pm'來使用它嗎? –

+0

這是我看起來像個白癡的地方。我應該配置Sendmail? – n0pe

回答

3

Mail::Sendmail文檔的LIMITATIONS部分提到它,除非你修改Sendmail.pm或在腳本中通過不同的服務器名稱(CONFIGURATION下提)連接到本地主機上的SMTP服務器。您沒有在您的腳本中指定服務器,並且您沒有提及自定義Sendmail.pm,因此它可能使用localhost。

由於您沒有收到錯誤,所以很可能您的本地主機上運行了SMTP服務器。但是,由於您沒有收到電子郵件,可能是SMTP服務器配置錯誤。它接受電子郵件,但無法發送。

在致電sendmail後,您還應該嘗試打印$Mail::Sendmail::log。這會告訴你更多關於發生的事情。

+1

是的,這一點有點讓人誤解:'從你的perl腳本簡單的平臺獨立的電子郵件。只需要Perl 5和一個網絡連接。' –

+0

謝謝我會在明天工作時嘗試打印日誌。然而,這是一個相當新鮮的紅帽安裝,我從來沒有碰過'sendmail'。 – n0pe