【问题标题】:Perl redirect STDOUT of a script in emailPerl 重定向电子邮件中脚本的 STDOUT
【发布时间】:2014-11-27 13:18:00
【问题描述】:

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

我打算从 bash 脚本调用 perl 脚本,然后在 bash 脚本中也有代码通过电子邮件发送日志。

但我想知道有没有其他更好的方法,我只能使用单个脚本(perl)来实现这一点,而不是有 2 个脚本,1 个(perl 脚本)用于执行任务,其他(bash 脚本)用于通过电子邮件发送记录。

【问题讨论】:

  • 如果回答了你的问题,Perl 可以发送邮件。
  • 如何发送登录邮件?
  • 通过使用一些模块,stackoverflow.com/q/2530154/223226
  • 一种选择是让脚本将其动作打印到标准输出;如果您使用cron 自动运行脚本,输出将自动邮寄给您。

标签: perl email stdout


【解决方案1】:

我不久前编写了这个函数,它运行得非常好。

它需要在您的系统上安装 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 文件)

编辑

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

【讨论】:

    【解决方案2】:

    首先您说您希望将 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

    【讨论】:

    • sendmail 命令使用起来比较简单。当然,如果您希望将日志文件作为单独的附件而不是电子邮件正文,我会更多地查看 thonnor 的回复并使用模块。
    • 您也可以参考本网站获取使用 sendmail 的帮助:tutorialspoint.com/perl/perl_sending_email.htm
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多