Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

E-mail MIME attachments from the command line UNIX
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!
    •    
  • Currently 3.20 / 5
  You rated: 3 / 5 (5 votes cast)
 
[22,476 views]  

E-mail MIME attachments from the command line | 5 comments | Create New Account
Click here to return to the 'E-mail MIME attachments from the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Mutt can also do that.
Authored by: improbability on Feb 18, '04 02:24:02PM
Maybe overkill but mutt also can do this. And allow you to edit the mime filename/content-type. fink has version 1.4

[ Reply to This | # ]
E-mail MIME attachments from the command line
Authored by: 47ronin on Feb 18, '04 05:36:19PM
you could just do this:
uuencode filename.sit filename.sit | mail -s "subject" recipient@emailaddress.com
Just replace the words above as needed! That will allow you to send a binary file over the built-in mailserver in OSX in just one line in the Terminal.

[ Reply to This | # ]
E-mail MIME attachments from the command line
Authored by: chitay on Apr 13, '04 04:42:21PM

this idea worked great using 'do shell script' in an applescript I'm working on - thanks! Just wondering if there's a way to use this to send multiple attachments. I need the script to e-mail 2 separate text file to a user (can't cat the two files into one - user is easily confused :-/ ) thanks for the help.



[ Reply to This | # ]
E-mail MIME attachments from the command line
Authored by: easco on Feb 18, '04 08:54:28PM

No need to use Perl to do the encoding. The openssl command will encode base 64 for you just fine.

% openssl enc -base64 -in <somefile> -out <destination>

I believe that openssl is included by default so you can skip the download-and-install-a-PERL-module step.



[ Reply to This | # ]
E-mail MIME attachments from the command line
Authored by: googoo on Feb 19, '04 09:45:52AM

Thanks! I replaced the Perl part of the Base64 conversion section in the script with the following command, and it works like a charm.

openssl enc -base64 -in $F

(Of course, this command only works in the context of the script.)

-Mark

[ Reply to This | # ]