Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 </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/ file1.log file2.log would serve the purpose. Please advise, thanks.

#!/usr/bin/perl -w

use MIME::Lite;

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

    Type    => "text/plain",
    Path    => $tmpMsg,
    Filename    => $tmpMsg,
    Disposition => "attachment"

$msg->send('smtp', '', Timeout => 60);
share|improve this question
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) {  
            'Type' => 'text/plain',  
            'Path' => shift @ARGV
share|improve this answer
Great! Exactly what I want. – Andrew Mar 30 '12 at 9:00

Your Answer


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.