2014-10-02 32 views
1

我有一個Perl腳本,它會做一些東西。腳本完成任務後,如何獲得電子郵件以及日誌(包括腳本執行的所有操作)?Perl重定向電子郵件腳本的STDOUT

我打算從bash腳本調用perl腳本,然後讓代碼通過電子郵件發送日誌以及bash腳本。

但我想知道是否還有其他更好的方法,我只能用單個腳本(perl)來實現此目的,而不是使用2個腳本,1個(perl腳本)執行任務和其他(bash腳本)以用於發送電子郵件日誌。

+0

Perl可以如果回答您的問題發送郵件不同的模塊。 – 2014-10-02 14:20:48

+0

如何發送電子郵件日誌? – Jill448 2014-10-02 14:38:12

+0

通過使用一些模塊,http://stackoverflow.com/q/2530154/223226 – 2014-10-02 14:39:36

回答

0

我後來寫了這個函數,它工作得很好。

它需要MIME ::精簡版模塊被安裝在系統上 - 不知道這是否會成爲絆腳石(它肯定是在我家)

道歉,如果代碼不遵循最新標準,它大約有3年的歷史,並且運行在我認爲的Perl 5.6.1上。

sub emailer($$$$$$;$); 

use MIME::Lite; 


sub emailer($$$$$$;$) 
{ 

    #-------------------------------------------------------------------------# 
    # Get incoming parameters             # 
    #-------------------------------------------------------------------------# 

    my ($exchange_svr, $to, $cc, $from, $subject, $message, $attachment) = @_; 

    #-------------------------------------------------------------------------# 
    # create a new message to be sent in HTML format       # 
    #-------------------------------------------------------------------------# 

    my $msg = MIME::Lite->new(
        From => $from, 
        To  => $to, 
        Cc  => $cc, 
        Subject => $subject, 
        Type => 'text/html', 
        Data => $message 
      ); 


    #-------------------------------------------------------------------------# 
    # Check if there is an attachment and that the file actually does exist # 
    # Only plain text documents are supported in this functioN.    # 
    #-------------------------------------------------------------------------# 

    if ($attachment) 
    { 

     #---------------------------------------------------------------------# 
     # if the attachment does not exist then show a warning    # 
     # The email will arrive with no attachment       # 
     #---------------------------------------------------------------------# 

     if (! -f $attachment) 
     { 
      print "WARNING - Unable to locate $attachment"; 
     } 
     else 
     { 
      #-----------------------------------------------------------------# 
      # add the attachment            # 
      #-----------------------------------------------------------------# 

      print "ATTACH", "$attachment"; 

      $msg->attach(
        Type => "text/plain", 
        Path => $attachment, 
        Disposition => "attachment" 
      ); 
     } 
    } 

    #-------------------------------------------------------------------------# 
    # send the email               # 
    #-------------------------------------------------------------------------# 

    MIME::Lite->send('smtp', $exch_svr, Timeout => 20); 

    $msg->send() or die "SENDMAIL ERROR - Error sending email"; 

} 

它看起來像這樣當它被用來

emailer($exchange_server, 
     "[email protected]", 
     "[email protected]", 
     "[email protected]", 
     "Subject in here", 
     "The Message in here", 
     "/full/path/to/attachment"); 

您還可以選擇添加第七屆參數是附件(您需要提供完整路徑)和附件必須是一個文本文件(我將送一個CSV文件)

編輯

我只是重新讀您的文章,看到你想發送附件,所以我說這部分的例子

1

首先你說你想讓你的STDOUT重定向到一個日誌文件。查看在這之前的職位的詳細信息:

How can I redirect standard output to a file in Perl?

# redirect STDOUT to file 
my $log_file = "log.txt"; 
open STDOUT, '>', $log_file; 

如果你使用Linux,你應該能夠發出一個sendmail命令來獲取與日誌信息的電子郵件:

# define your to, from and subject. 
my $to = <who you are sending email to>; 
my $from = <who is it from>; 
my $subject = "This is a subject"; 

# push the contents of your log file into the email body 
open (LOG, '<', $log_file) or die "Failed to open $log_file: $!"; 
my @log_contents = <LOG>; 
close LOG; 

push @body, @log_contents; 

# open and write to the mail file 
open MAIL, '|/usr/sbin/sendmail -t' or die "Failed to send mail: $!"; 

# email header 
print MAIL "To: ${to}\n"; 
print MAIL "From: ${from}\n"; 
print MAIL "Subject: ${subject}\n\n"; 

# email body 
print MAIL @body; 

# send the email 
close MAIL; 
print "Email sent successfully.\n"; 

這是一種快速發送消息的非常簡單的方法。

如果你在Windows我會去了解一下可用於在Perl中發送電子郵件,如MIME ::精簡版

+0

sendmail命令的使用相對簡單。當然,如果你想把日誌文件作爲一個單獨的附件而不是電子郵件正文,我會更加註意thonnor的反應並使用一個模塊。 – tjwrona1992 2014-10-02 14:55:16

+0

你也可以參考這個網站獲取使用sendmail的一些幫助:http://www.tutorialspoint.com/perl/perl_sending_email.htm – tjwrona1992 2014-10-02 15:08:18