Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I want to send log files matching date format, e.g. YYYY-MM-DD-*.log as attachments in Perl script. In BASH, this can be easily done by:

[ -f $DIR/explog/$(date "+%Y-%m-%d")-*-host1.log ] && mutt -s "subject here" \
-a $DIR/explog/$(date "+%Y-%m-%d")-*-host1.log my@email.com </dev/null

For some reason, I need to do this in Perl script. Main part of the Perl script is as below, I want to add log files in the emails. Any idea?

BTW, this Perl script is called as a email alert from BASH shell script, so if there is any method to pass files to perl script e.g. /usr/bin/perl $DIR/emailAlert.pl file1.log file2.log would serve the purpose. Please advise, thanks.

#!/usr/bin/perl -w

use MIME::Lite;

$msg = MIME::Lite->new( 
    From => 'sender\@example.com', 
    To => 'recipient\@example.com', 
    Subject => 'subject here', 
    Type => 'multipart/mixed'
); 

$msg->attach(
    Type    => "text/plain",
    Path    => $tmpMsg,
    Filename    => $tmpMsg,
    Disposition => "attachment"
);

$msg->send('smtp', 'mailserver.example.com', Timeout => 60);
share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can use arguments in statements like $log1 = $ARGV[0] and $log2 = $ARGV[1] and assign them in your $msg->attach section. You can use it like this:

    while (@ARGV) {  
        $msg->attach(  
            'Type' => 'text/plain',  
            'Path' => shift @ARGV
        );  
    }
share|improve this answer
    
Great! Exactly what I want. –  beicha Mar 30 '12 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.