Perl重定向电子邮件中脚本的STDOUT

Perl重定向电子邮件中脚本的STDOUT,perl,email,stdout,Perl,Email,Stdout,我有一个perl脚本,可以做一些事情。脚本完成任务后,如何获取电子邮件以及日志(脚本执行的所有操作) 我计划从一个bash脚本调用perl脚本,然后在bash脚本中编写代码以通过电子邮件发送日志 但是我想知道还有没有其他更好的方法,我只能用一个脚本(perl)来实现这一点,而不是用两个脚本,一个(perl脚本)来执行任务,另一个(bash脚本)来发送日志。我不久前写过这个函数,它工作得非常好 它需要在您的系统上安装MIME::Lite模块-不确定这是否会成为一个绊脚石(当然是在我的位置上) 如果

我有一个perl脚本,可以做一些事情。脚本完成任务后,如何获取电子邮件以及日志(脚本执行的所有操作)

我计划从一个bash脚本调用perl脚本,然后在bash脚本中编写代码以通过电子邮件发送日志


但是我想知道还有没有其他更好的方法,我只能用一个脚本(perl)来实现这一点,而不是用两个脚本,一个(perl脚本)来执行任务,另一个(bash脚本)来发送日志。

我不久前写过这个函数,它工作得非常好

它需要在您的系统上安装MIME::Lite模块-不确定这是否会成为一个绊脚石(当然是在我的位置上)

如果代码没有遵循最新的标准,我深表歉意,因为它已经有3年的历史了,我相信它是在Perl5.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文件)

编辑

我刚刚重读了你的帖子,发现你确实想发送一个附件,所以我将该部分添加到了示例中


首先,您说希望将标准输出重定向到日志文件。查看上一篇文章,了解有关以下内容的详细信息:


如果您使用的是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";
#定义收件人、发件人和主题。
我的$to=;
我的$from=;
my$subject=“这是一个主题”;
#将日志文件的内容推送到电子邮件正文中

打开(LOG,'Perl可以发送邮件,如果这回答了您的问题。我如何发送登录电子邮件?通过使用一些模块,一个选项是使脚本将其操作打印到标准输出;如果您使用
cron
自动运行脚本,输出将自动发送给您。sendmail命令使用相对简单。当然rse如果您想将日志文件作为单独的附件而不是电子邮件正文,我会更深入地了解thonnor的回复并使用模块。您也可以使用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";