简体   繁体   中英

Perl redirect STDOUT of a script in email

I have a perl script which will do some stuff. How can I get an email once the script completes its tasks, along with log (with all the actions the script performed) ?

I am planning to call the perl script from a bash script and then have the code to email the log as well in bash script.

But I want to know is there any other better way, I can achieve this only with single script(perl), rather than having 2 scripts, 1 (perl script)for performing tasks and other (bash script)for emailing the log.


First you said you wanted your STDOUT redirected to a log file. View this previous post for details on that:

How can I redirect standard output to a file in Perl?

# redirect STDOUT to file
my $log_file = "log.txt";
open STDOUT, '>', $log_file;

If you are using LINUX you should be able to issue a sendmail command to get an email with the log information:

# 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";

This is a very simple way to quickly send a message out.

If you are on Windows I would look into the different modules available for sending emails in Perl such as MIME::Lite

I wrote this function a while back and it works perfectly well.

It requires the MIME::Lite module to be installed on your system - not sure if this will be a stumbling block (it certainly was at my place)

Apologies if the code doesn't follow the latest standards, it is about 3 years old and runs on Perl 5.6.1 I believe.

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";

}

It looks like this when it is used

emailer( $exchange_server,
         "someone@somewhere.com",
         "someoneelse@somewhere.com",
         "me@mydesk.com",
         "Subject in here",
         "The Message in here",
         "/full/path/to/attachment" );

Optionally you can add the 7th parameter which is the attachment (you need to provide the full path) and the attachment must be a Text file (I'm sending a CSV file)

EDIT

I just re-read your post and saw that you do want to send an attachment so I added that part to the example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM