more bluCard Script Fixes

From: Jonathan Felder <felder_at_path.berkeley.edu>
Date: Thu, 13 Sep 2007 23:14:00 -0700

These do a better job grabbing the mime boundary marker.

Also when using blucardsendmail.pl we found it necessary to delete the
Message-Id header to get other systems (i.e. calmail, gmail) to deliver
the message.

-- 
Jonathan Felder
Network and System Administrator
California PATH, UC Berkeley
http://www.path.berkeley.edu/systemsupport/
Windows 9x (win-doze): a 32 bit Extension to a 16 bit Graphical Shell
of an 8 bit Operating System originally coded for a 4 bit Processor by
a 2 bit company that can't stand one bit of competition.

#!/usr/bin/perl

use strict;
use MIME::Base64;
use MIME::QuotedPrint;
use Mail::Message;
use Mail::Field;
use MIME::Head;

my($message, $body, $attachment, $from, $line, $i, $field, $boundary, $newmessage, $encoding, $mimeheaders);

$line = "0";
$message = "";

while (<STDIN>)
{
        if (/^From\s/ && $line eq "0")
        {
                $from = $_;
                $line = "1";
        }
        else
        {
                $line = "1";
                $message .= $_;
        }
}

$message = Mail::Message->read($message);

if ($message->body->isMultipart)
{
        $attachment = $message->body->part(1)->string;
        $attachment =~ s/\r//g;
        $attachment =~ s/(.*?)\n\n//s;
        $mimeheaders = $1;

        for $i (split("\n", $mimeheaders))
        {
                $i =~ s/^\s+//g;
                $i =~ s/\s+$//g;

                if ($i =~ /^Content-Transfer-Encoding:/i)
                {
                        $encoding = $i;
                }
        }

        if ($mimeheaders =~ /PRCR_.*?_Verification_Report\.txt/m || $mimeheaders =~ /PRCR_.*?_Download_File\.txt/m || $mimeheaders =~ /PRCR_.*?_Excel_CSV_File\.csv/m)
        {
                if ($encoding =~ /base64/i)
                {
                        $attachment = decode_base64($attachment);
                }
                elsif ($encoding =~ /quoted-printable/i)
                {
                        $attachment = decode_qp($attachment);
                }
                elsif ($encoding =~ /7bit/i || $encoding =~ /8bit/i)
                {
                        ;
                }
                else
                {
                        $attachment = "\nAttachment was not decoded.\nHeaders are:\n$mimeheaders";
                }
        }
        else
        {
                $message->head->delete('To');
                $message->head->delete('Message-Id');
                $message->head->delete('X-UIDL');
                $message->head->delete('Resent-From');
                $message->head->delete('Resent-To');
                $message->head->delete('Resent-Date');
                $message->head->delete('Resent-Message-Id');
                $message->head->delete('Resent-User-Agent');
                $message->head->add('To: putanemailaddresshere');

                $message->send;
                exit 0;
        }
        
        $field = Mail::Field->new('Content-type',$message->head->get('content-type'));
        $boundary = $field->boundary;
        
        $newmessage = $message->head->string;
        $newmessage .= "--".$boundary."\n";
        $newmessage .= $message->body->part(0)->string;
        $message->body->part(0)->delete;
        $newmessage .= "\n\n";
        $newmessage .= $attachment;
        $newmessage .= "\n\n\n";
        $newmessage .= $message->body->string;

        $newmessage = Mail::Message->read($newmessage);
        $newmessage->head->delete('To');
        $newmessage->head->delete('Message-Id');
        $newmessage->head->delete('X-UIDL');
        $newmessage->head->delete('Resent-From');
        $newmessage->head->delete('Resent-To');
        $newmessage->head->delete('Resent-Date');
        $newmessage->head->delete('Resent-Message-Id');
        $newmessage->head->delete('Resent-User-Agent');
        $newmessage->head->add('To: putanemailaddresshere');

        $newmessage->send;
}
else
{
        $message->head->delete('To');
        $message->head->delete('Message-Id');
        $message->head->delete('X-UIDL');
        $message->head->delete('Resent-From');
        $message->head->delete('Resent-To');
        $message->head->delete('Resent-Date');
        $message->head->delete('Resent-Message-Id');
        $message->head->delete('Resent-User-Agent');
        $message->head->add('To: putanemailaddresshere');

        $message->send;
}

#!/usr/bin/perl

use strict;
use MIME::Base64;
use MIME::QuotedPrint;
use Mail::Message;
use Mail::Field;
use MIME::Head;

my($message, $body, $attachment, $from, $line, $i, $field, $boundary, $encoding, $mimeheaders);

$line = "0";
$message = "";

while (<STDIN>)
{
        if (/^From\s/ && $line eq "0")
        {
                $from = $_;
                $line = "1";
        }
        else
        {
                $line = "1";
                $message .= $_;
        }
}

$message = Mail::Message->read($message);

if ($message->body->isMultipart)
{
        $attachment = $message->body->part(1)->string;
        $attachment =~ s/\r//g;
        $attachment =~ s/(.*?)\n\n//s;
        $mimeheaders = $1;

        for $i (split("\n", $mimeheaders))
        {
                $i =~ s/^\s+//g;
                $i =~ s/\s+$//g;

                if ($i =~ /^Content-Transfer-Encoding:/i)
                {
                        $encoding = $i;
                }
        }
        
        if ($mimeheaders =~ /PRCR_.*?_Verification_Report\.txt/m || $mimeheaders =~ /PRCR_.*?_Download_File\.txt/m || $mimeheaders =~ /PRCR_.*?_Excel_CSV_File\.csv/m)
        {
                if ($encoding =~ /base64/i)
                {
                        $attachment = decode_base64($attachment);
                }
                elsif ($encoding =~ /quoted-printable/i)
                {
                        $attachment = decode_qp($attachment);
                }
                elsif ($encoding =~ /7bit/i || $encoding =~ /8bit/i)
                {
                        ;
                }
                else
                {
                        $attachment = "\nAttachment was not decoded.\nHeaders are:\n$mimeheaders";
                }
        }
        else
        {
                print $from;
                print $message->string;
                exit 0;
        }

        $attachment =~ s/\r//g;
        
        $field = Mail::Field->new('Content-type',$message->head->get('content-type'));
        $boundary = $field->boundary;
        
        print $from;
        print $message->head->string;
        print "--".$boundary."\n";
        print $message->body->part(0)->string;
        $message->body->part(0)->delete;
        print "\n\n";
        print $attachment;
        print "\n\n\n";
        print $message->body->string;
}
else
{
        print $from;
        print $message->string;
}

------------------------------------------------------------------------
The following was automatically added to this message by the list server:

To learn more about Micronet, including how to subscribe to
or unsubscribe from its mailing list and how to find out
about upcoming meetings, please visit the Micronet Web site:

http://micronet.berkeley.edu/

Messages you send to this mailing list are public and world-viewable,
and the list's archives can be browsed and searched on the Internet.
This means these messages can be viewed by (among others) your bosses,
prospective employers, and people who have known you in the past.
Received on Thu Sep 13 2007 - 23:36:07 PDT

This archive was generated by hypermail 2.2.0 : Thu Sep 13 2007 - 23:36:14 PDT