Decoding Mime Attachments

This page outlines several methods of generating email messages from command line and shell scripts using both popular email processing tools such as metamail and mpack, and home grown scripts. Most of these methods use the MIME specification, as outlined in RFC 2045, RFC 2046, RFC 2047, RFC 2048 and RFC 2049.

Please send mail to me with any comments, suggestions, criticism or additions.

All of the below examples use the following shell variables. I use MIME type application/octet-stream just as an example. Actual type used will vary depending upon attachment file type. Remember, these are simple examples of the different tools available.
 

TXTFILE=/tmp/textfile		# A text message with a simple preface message
ATTFILE=/tmp/binary_file	# File to be attached and generally requiring encoding
SUBJECT="Your attachment"	# Change as needed
[email protected]		# Ditto

Here is some Bourne shell code (taken out of a deliver enabled autoresponder) for creating email messages with multiple attachments of varying types. This script uses metamail's mmencode program for encoding and sendmail for sending.

Check out this far more advanced shell script written by Dave Wotton that does much more than my simple script. It uses a encoder called encdec that I am not familiar with but will soon address on this page. In the meantime, you could use mmencode mentioned previously for the encoding.

And here is some basic, fairly static Perl code using the MIME::Lite module to send an attachment along with a text note to a sender. This is almost verbatim from the modules man page and could use some tweaking to get it more useful but it certainly works as a "plugin" piece of code.

Here is a useful page by Heiner Steven with several examples of shell coding using metamail and mpack in particular.

For an excellent guide to creating proper text files and for the formatting of your message if you write your own script, please see Keith Moore's style guide.

Recommended Links

freshmeat.net Project details for MHonArc

MHonArc is a Perl program for converting mail or news messages into HTML archives. It can also be used to convert individual messages into HTML documents. Other capabilities include robust MIME support and powerful user customization features.

freshmeat.net Project details for ripMIME

ripMIME extracts attachment files out of a MIME-encoded email pack.

Author:
Paul L Daniels [contact developer]

Quick Links
 

Welcome to ripMIME.

ripMIME has been written as part of the (commercial) Xamime package, but because there was such a problem trying to attain a easy to use, openSource MIME decoder which the everyday person with some limited C skills could try and play with, we decided to release the MIME decoding components of Xamime as its own package which you are now seeing as "ripMIME".

ripMIME has a single sole pupose, to extract the attached files out of a MIME package (as say produced by programs such as AMaViS, Inflex and Xamime). ripMIME deliberately has no other operating purpose (if you find another use, then that's great).

ripMIME has been written (at this point) purely by PLDaniels, it is being released under a BSD style licence so that other commercial ventures may if they so desire use this product without the headaches normally associated with using GPL software.

 

Usage
 

ripMIME -i <mime file> -d <directory>[-p prefix] [-e [header file]] [-vVh] [--version]
		[--no_nameless] [--unique_names [--prefix|--postfix|--infix]][--paranoid] [--mailbox] 
		[--formdata] [--debug][--no-tnef] [--no-quotedprintable] [--no-uudecode]

Options available :
	-i : Input MIME encoded file (use '-' to input from STDIN)
					If <mime file> is a directory, it will be recursed
	-d : Output directory
	-p : Specify prefix filename to be used on files without a filename (default 'text')
	-e [headers file name] : Dump headers from mailpack (default '_headers_')
	-v : Turn on verbosity
	-q : Run quietly, do no report non-fatal errors

	--verbose-contenttype : Turn on verbosity of file content type
	--verbose-oldstyle : Uses the v1.2.x style or filename reporting
	--verbose-defects: Display a summary of defects in the email
	--verbose-mime: Displays 'Email is MIME' if the email is MIME

	--stdout : All reporting goes to stdout (Default)
	--stderr : All reporting goes to stderr
	--syslog : All reporting goes to syslog

	--no-paranoid : [ Deprecated ] Turns off strict ascii-alnum filenaming
	--paranoid: Converts all filenames to strict 7-bit compliance
	--name-by-type: Saves a given attachment by its content-type if it has no other name
	--no-nameless : Do not save nameless attachments
	--overwrite : Overwrite files if they have the same name on extraction
	--unique-names : Dont overwrite existing files (This is the default behaviour)
	--prefix : rename by putting unique code at the front of the filename
	--postfix : rename by putting unique code at the end of the filename
	--infix : rename by putting unique code in the middle of the filename

	--mailbox : Process mailbox file
	--formdata : Process as form data (from HTML form etc)

	--no-tnef : Turn off TNEF/winmail.dat decoding
	--no-ole : Turn off OLE decoding
	--no-uudecode : Turns off the facility of detecting UUencoded attachments in emails
	--no-quotedprintable : Turns off the facility of decoding QuotedPrintable data
	--no-doublecr : Turns off saving of double-CR embedded data
	--no-mht : Turns off MHT (a Microsoft mailpack attachment format ) decoding
	--no-multiple-filenames : Turns off the multiple filename exploit handling

	--disable-header-fix : Turns off attempts to fix broken headers
	--disable-qmail-bounce : Turns off qmail bounced email testing
	--recursion-max  : Set the maximum recursion level to 'level'
	--timeout      : Set the maximum number of CPU seconds ripMIME can run for

	--debug : Produces detailed information about the whole decoding process
	--extended-errors:      Produces more return codes, even for non-fatals
	-V --version : Give version information
	--buildcodes : Give the build information (tstamp, date and system information)
	-h : This message (help)

	

 

OS Compatibility
Because ripMIME is written to ANSI/POSIX specifications as much as possible, it should be portable across most *NIX's. If you find ripMIME doesn't work on your *NIX system, then please contact us about it and we'll see what can be done.

 

 

Rating:
8.57/10.00 (7 votes)

Homepage:
http://www.pldaniels.com/ripmime/
Tar/GZ:
http://www.pldaniels.com/ripmime/ripmime-1.4.0.7.tar.gz
Changelog:
http://www.pldaniels.com/ripmime/CHANGELOG
 

Trove categories: [change]

[Development Status]   5 - Production/Stable
[Environment]   Console (Text Based)
[Intended Audience]   End Users/Desktop, System Administrators
[License]   OSI Approved :: BSD License (original), OSI Approved :: BSD License (revised)
[Operating System]   OS Independent
[Programming Language]   C
[Topic]   Communications :: Email, Communications :: Email :: Filters, Software Development :: Libraries

Dependencies: [change]
No dependencies filed