Send Attachment from command line

The script below allows you to send an email with attachment from command line. Use it to automate sending logfiles, images, documents, etc.

Dependencies

  • Getopt::Long
  • Pod::Usage
  • MIME::Lite
  • File::Type
  • File::Basename
  • #!/usr/bin/perl
    #
    #
    #
    # $Author: taco $
    # $Revision: 94 $
    # $Date: 2011-04-19 23:33:46 +0200 (Tue, 19 Apr 2011) $
     
    use Getopt::Long;
    use Pod::Usage;
     
    my $help        = '';
    my $man         = '';
    my $ver         = '';
    my $from        = '';
    my $to          = '';
    my $subject     = '';
    my $text        = '';
    my $filetype    = '';
    my $file        = '';
    my $filename    = '';
    my $smtp        = 'localhost';
     
    GetOptions ( 'help|h'           => \$help,
                 'man'              => \$man,
                 'version|V'        => \$ver,
                 'from|f=s'         => \$from,
                 'to|t=s'           => \$to,
                 'subject|s=s'      => \$subject,
                 'text|body=s'      => \$text,
                 'file=s'           => \$file,
                 'filetype|ft=s'    => \$filetype,
                 'filename|fn=s'    => \$filename,
                 'smtp=s'           => \$smtp
               );
     
    if(!-e "$file") { print "File not found: $file\n"; 
                   $help=1; 
                  }
    else          { $filetype = fType($file); }
    if(!$filename){ $filename = getFilename($file); }
    if(!$from)    { print "No from address set\n";
                    $help=1;
                  }
    if(!$to)      { print "No to address set\n";
                    $help=1;
                  }
    if(!$subject) { print "Subject is empty\n";
                    $help=1;
                  }
     
    if($help)     { pod2usage( -verbose =>  1) && exit;               }
    if($man)      { pod2usage({-verbose =>  2, -output => \*STDOUT}); }
     
    if(!$text)    { print "Enter your message below, ^d to end.\n";
                    while(<STDIN>) { $text.=$_; } }
     
    # for debugging only
    #print "$text";
    #print "mimetype = $filetype\n";
    #print "filename = $filename\n";
     
    use MIME::Lite;
    ### Create a new multipart message:
    $msg = MIME::Lite->new(
                 From    =>$from,
                 To      =>$to,
                 Subject =>$subject,
                 Type    =>'multipart/mixed'
                 );
     
    ### Add parts (each "attach" has same arguments as "new"):
    $msg->attach(Type     =>'TEXT',
                 Data     =>$text
                 );
    $msg->attach(Type     =>$filetype,
                 Path     =>$file,
                 Filename =>$filename,
                 Disposition => 'attachment'
                 );
     
    #$msg->print(\*STDOUT);
    $msg->send("smtp",$smtp);
     
    sub fType{
      my $file=shift;
      use File::Type;
      my $ft = File::Type->new();
      my $filetype = $ft->mime_type($file);
      return $filetype;
    }
     
    sub getFilename{
      my $file=shift;
      use File::Basename;
      return basename($file);
    }
     
    __END__
     
    =pod
     
    =head1 NAME 
     
    send-attachment - send attachments from command line
     
    =head1 SYNOPSIS
     
      send-attachment -from me@example.com \
                      -to you@example.com \
                      -subject "Some message" \
                      [-body "Hey, here is that file you wanted"] \
                      -file ./image.gif \
                      [-filename image.gif] \
                      [-filetype "image/gif"]
     
    =head1 OPTIONS
     
    =over 8
     
    =item B<-help> B<-h>
     
    displays a brief help message
     
    =item B<-man>
     
    displays the full manual
     
    =item B<-version> B<-V>
     
    displays version information
     
    =item B<-f(rom)> <from address>
     
    The 'sender' address of the message
     
    =item B<-t(o)> <to address>
     
    The recipient of the message
     
    =item B<-s(ubject)> "<subject>"
     
    Subject of the message
     
    =item B<-text|-body> "<body text of the message>"
     
    If this option isn't set STDIN will be used, this allows you to pipe text in to the command.
     
    =item B<-file> </path/to/file>
     
    File to attach to the message
     
    =item B<-ft -filetype> "<filetype>"
     
    Use only if the mimetype of the file can't be detected.
     
    =item B<-fn -filename> 
     
    Filename you want the attachment to have if different from it's current name
     
    =item B<-smtp>
     
    smtp server to use, defaults to localhost
     
    =back
     
    =head1 DESCRIPTION
     
    send-attachment is a perl script that uses MIME::Lite to send an email with an attachment from command line
     
    =head1 DEPENDENCIES
     
    This script depends on the following perl modules:
     
     Getopt::Long
     Pod::Usage
     MIME::Lite
     File::Type
     File::Basename
     
    =head1 EXAMPLES
     
      send-attachment -from me@example.com \
                      -to you@example.com \
                      -subject "Some message" \
                      -body "Hey, here is that file you wanted" \
                      -file ./image.gif \
                      -filename image.gif \
                      -filetype "image/gif"
     
    or
     
      (cat<<EOF
      Hey dude,
     
      This is the file you wanted.
     
      Cheers,
      Me
      EOF
      ) | send-attachment -from me@example.com \
                    -to you@example.com \
                    -subject "Some message" \
                    -file ./image.gif \
                    -filename image.gif \
                    -filetype "image/gif"
     
    =head1 VERSION
     
    0.4
     
    =head1 REVISION
     
    $Revision: 94 $
     
    =head1 AUTHOR
     
    Taco Scheltema <taco_at_scheltema.org>
     
    =cut

Leave a Reply