Fellow Monks,

I had a need to send email with MIME attachments from the UNIX command line. I searched the world over but could not find anything flexible enough to meet all my needs.

Below is the script I wrote which you may find useful. It requires MIME::Lite, but everything else is stock distribution.

I plan on future refinement and will post future enhancements.

Enjoy!

-uuoc



#!/usr/local/bin/perl # email.pl # # Allows flexible emailing from the UNIX command line # including the ability to send MIME email attachments. # # History Date Comments # V1.0 Nov. 8, 2001 Just out of Beta, works great :) use Getopt::Long; use MIME::Lite; $DEBUG=0; # 1 = Debug on, 0 = Debug off GetOptions ( "s=s" => \$subject, # -s = subject "f=s" => \$from, # -f = from "c=s" => \$cc, # -c = cc: "b=s" => \$body_file, # -b = body "a=s" => \$attachments # -a = attachment ); # Check to make sure there is at least on valid recipient. # If there isn't a valid recipient Display the man page. $num=@ARGV; if ( $num < 1 ) { &help(); exit(); } if ($DEBUG) { print "From: $from \n"; print "Subject: $subject \n"; print "Body File: $body_file \n"; print "Attachments: $attachments \n"; } # Create a new multipart message: $msg = new MIME::Lite; $msg->build( >From =>$from, To =>"@ARGV[0]", Cc =>$cc, Subject =>$subject, Type =>'multipart/mixed' ); for ($ct = 1; $ct < $num ; $ct++ ) { $msg->add(To => "@ARGV[$ct]"); } # Fill in the body of the email # # If -b contains a file name, populate the body with the contents of t +he file # If -b contains text and is NOT a filename, then fill the body in wit +h # that text # If -b is not defined, then put "No Message." in the body $nobody=0; if ($body_file) { unless (open BODY, $body_file) { $nobody=1; } # Is it a file ? if ( $nobody ) { # No, it's text $body = $body_file; } else { while ( <BODY> ) { # Yes, it's a file, +read it $body=$body.$_ ; } close (BODY); } } else { # -b is not specifie +d $body="No Message."; } # Add parts (each "attach" has same arguments as "new"): $msg->attach(Type =>'TEXT', Data =>$body, ); # Process the attachments and do base 64 mime encoding $attachments =~ s/\s+//g; # Strip all spaces @to_mime = split(/,/, $attachments); for ($ct=0; $ct < @to_mime; $ct++) { $_ = $to_mime[$ct]; $CONTENT_TYPE=""; if ($DEBUG) { print "Processing file $to_mime[$ct]\n"; print $_."\n"; } # Todo: Make this into a lookup table and add more MIME types if (/\.pcl$/i) { $CONTENT_TYPE="Content-Type: text/plain;"; } if (/\.txt$/i) { $CONTENT_TYPE="text/plain;"; } if (/\.ps$/i) { $CONTENT_TYPE="Content-Type: application/postscript;"; } if (/\.tif$/i) { $CONTENT_TYPE="image/tiff;"; } if (/\.doc$/i) { $CONTENT_TYPE="application/msword;"; } if (/\.xls$/i) { $CONTENT_TYPE="application/msexcel;"; } if (/\.csv$/i) { $CONTENT_TYPE="application/msexcel;"; } if ( $CONTENT_TYPE eq "" ) { die("Fatal Error: The content file $to_mime[$ct], must have an +extension of .doc .ps .txt .tiff .csv .doc .xls or .pcl .\n"); } if ($DEBUG) { print "Content Type: $CONTENT_TYPE, File Name: $to_mime[$ct]\n"; } $msg->attach(Type =>"$CONTENT_TYPE", Path =>$to_mime[$ct], Filename =>$to_mime[$ct] ); } # Send message in the "best" way (the default is to use "sendmail"): $msg->send; # # END OF MAIN CODE ################################################################### sub help { print <<"EOF"; ______________________________________________________________________ +______ email.pl -- Command line tool for sending email with attachments Usage ====== email.pl [-s subject] [-c cc-addr] [-b body-file || text message] [-a attach, attach, ...] [-f from-addr] to-addr ... Options: -s Specify subject on command line (only the first argument after the -s flag is used as a subject; be careful to quote subjects containing spaces.) -c Send carbon copy to email address -b File containing the body of the message, or if the file is not found, this text is used as the body of the message be careful to quote files names or text containing spaces. If this is left blank, the words "No message." will appear in the body of the email. -a File(s) to attach. If you are sending multiple files, comma delimit the file names and use double quotes. See valid attachment MIME types below. Attachment file names may NOT contain spaces. -f Specify the email address of the sender of the email message. If nothing is specified the current user name will be filled into this field. Examples: Sending multiple attachments email.pl -s "Statistcal Analysis" -f "me\@mydomain.com" -b file.txt + \\ -a "stats-10-5-2000.xls, stats-10-6-2000.xls, file.txt" \\ -c "test1\@domain.com" \\ user3\@domain.com Sending a single attachment and changing the body of the email email.pl -s "Spreadsheet" -b "See Attached.." -a file.csv \\ user\@domain.com Send the /etc/hosts file as the body of a message email.pl -b "/etc/hosts" user\@domain.com Valid Extensions and MIME type ============================== .doc -- msword .xls -- excel .txt -- text/plain ascii .ps -- application/postscript .tif -- image/tiff .pcl -- HP PCL5e (Hewlett Packard Printer Control Language V5e) EOF }

In reply to Sending email with MIME attachments from the UNIX command line by uuoc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.