[英]Perl redirect STDOUT of a script in email
我有一個perl腳本,可以完成一些工作。 一旦腳本完成其任務以及日志(包括腳本執行的所有操作),如何獲得電子郵件?
我打算從bash腳本中調用perl腳本,然后在bash腳本中也有通過電子郵件發送日志的代碼。
但是我想知道還有其他更好的方法,我只能使用單個腳本(perl)來實現,而不能使用2個腳本,1個(perl腳本)來執行任務,其他2個(bash腳本)來通過電子郵件發送日志。
首先,您說過要將STDOUT重定向到日志文件。 查看此以前的文章以獲取有關以下內容的詳細信息:
# 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 :: Lite。
我前一段時間寫了這個函數,並且效果很好。
它要求在您的系統上安裝MIME :: Lite模塊-不確定這是否是絆腳石(它肯定在我的位置)
如果代碼沒有遵循最新的標准,我深表歉意,它已經使用了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,
"someone@somewhere.com",
"someoneelse@somewhere.com",
"me@mydesk.com",
"Subject in here",
"The Message in here",
"/full/path/to/attachment" );
(可選)您可以添加第7個參數,即附件(您需要提供完整路徑),並且附件必須是文本文件(我正在發送CSV文件)
編輯
我只是重新閱讀了您的文章,發現您確實要發送附件,所以我將該部分添加到了示例中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.