我有一个perl脚本可以做一些事情。如何在脚本完成任务后获取电子邮件,以及日志(执行脚本的所有操作)?
我打算从bash脚本调用perl脚本,然后让代码通过bash脚本发送日志。
但是我想知道还有其他更好的方法,我只能用单个脚本(perl)实现这一点,而不是有2个脚本,1个(perl脚本)用于执行任务,而其他(bash脚本)用于通过电子邮件发送日志中。
答案 0 :(得分: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 :: Lite
答案 1 :(得分:0)
我曾经写过这个功能,它的效果非常好。
它需要在您的系统上安装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文件)
修改
我刚刚重新阅读了您的帖子,发现您确实要发送附件,因此我将该部分添加到示例中