E-mail MIME attachments from the command line

Feb 18, '04 10:40:00AM

Contributed by: googoo

From time to time I need to send an E-mail with MIME attachments from the command line while logged on via an SSH connection. Thanks to Google, I found a shell script to do just that, and it only requires one modification (detailed below) to work on Mac OS X 10.3. (10.2 users must also install a perl package.) The original script is by Dave Wotton. My modified Mac OS X version is also available for download.

The shell script requires that your Mac be configured as a mail server that can send mail using the sendmail command (Mac OS X 10.3 uses postfix, but it still has a sendmail command for compatibility purposes). The script also requires perl with the MIME::Base64 package installed. This package comes with Mac OS X 10.3 (10.2.x users must install it, though). The one modification I made to the script is to change the /usr/bin/sendmail command to /usr/sbin/sendmail, the correct path on Mac OS X.

Here is how to make it work.

  1. Download the script.
  2. Save it to a directory in your path with an appropriate name (i.e., mail_files).
  3. Make the file executable by entering:
    % chmod u=rwx,go=rx mail_files
    
If you are using Mac OS X 10.3, you are done! Skip the rest of the steps, and see the example at the end.

Installing the MIME::Base64 package on Mac OS X 10.2.x
  1. Download the MIME::Base64 package from CPAN.
  2. Go to the directory where you downloaded the file. Decompress and unpack with the command:
    % tar -zxvf MIME-Base64-3.00.tar.gz
    
  3. Go to the new MIME-Base64-3.00 directory:
    % cd MIME-Base64-3.00
    
  4. Build a makefile, compile the package, and install it in the default location:
    % perl Makefile.PL
    % make
    % sudo make install
    
You are done. You can delete the MIME-Base64-3.00 directory if you like.

Using the Script

If you just installed the script into a directory in your path, be sure to enter the rehash command to make sure that your shell knows it is there. Enter the mail_files command with no arguments for a short description of the options. Here is an example of how to use the command:
% mail_files -t recipient@email.address -s subject -b attachedFileName
Enter your message text
here in as many lines as necessary
with control-D at the end.
[Ctrl-D]
Edit the script for a detailed description of all the options in the comments. E-mail away!

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040210105313440