#/usr/local/bin/perl
# This program takes mime attachments and detaches them
# Typical usage:  perl mime.pl < temp
#1) in the mail message body search  for "base64" string. You will find
#something like
#--0__=9Q3Hgwtp9JgPGBmdF1NqR65fWqyRNkqbK0nXXCuerLbGJtxLVxRub9HA
#Content-type: application/octet-stream;
     #name="Fitting.zip"
#Content-transfer-encoding: base64
# 
#following by empty string and then sequence of solid 80 character strings.
#Last string will be terminated with '=' and then there will be empty line
#again.
#This sequence of strings is a base64-encoded attachement. Select the block
# starting with the first incomprehensible characters
#and paste it into file "attachment.txt", for instance.
# e.g. 
# PEhUTUw+DQ0KPEhFQUQ+DQ0KPE1FVEEgSFRUUC1FUVVJVj0iQ29udGVudC1U
# eXBlIiBDT05URU5UPSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9d2luZG93cy0xMjUy
# Ij4NDQo8TUVUQSBOQU1FPSJHZW5lcmF0b3IiIENPTlRFTlQ9Ik1pY3Jvc29m
# <some stuff>
# IGFzLCBhcnRpY2xlcyB0aGF0IHJlY2VpdmVkIGEgcG9zaXRpdmUgcmV2aWV3
# IGZyb20gTWlrZSBTdG9uZWJyYWtlci4gPC9QPjwvRElSPg0NCjwvRElSPg0N
# CjwvRk9OVD48L0JPRFk+DQ0KPC9IVE1MPg0NCg==       
use MIME::Base64;   # must have this library, if you don't,
                         # I wrote my own implementation of base64
                         # conversion for Perl, I can submit it to you
while(<>)  {
   $s = $s . $_;         # make big string from all attachment
}
$decoded = decode_base64($s);
open( handle, '>file_out') || die "Can't open file_out";
binmode handle;
print handle $decoded;

