[英]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.