Mailing List CGatePro@mail.stalker.com Message #103016
From: Nicolas Hatier <nicolas.hatier@niversoft.com>
Subject: Re: Need help with filter
Date: Thu, 16 Feb 2012 19:37:43 -0500
To: CommuniGate Pro Discussions <CGatePro@mail.stalker.com>

What are the symptoms?

Try to add

      open STDOUT, ">&STDOUT";

at the end of the while(<>) loop to make sure STDOUT is flushed.

Nicolas

Nicolas Hatier, ing. <nicolas.hatier@niversoft.com>
Niversoft idées logicielles - http://www.niversoft.com


On 2012-02-16 18:28, Taylor, Jonn wrote:
All,

I am trying to write a filter to extract attachments from an email, rename the file and save it to a file share. The code below works at a bash shell but does not work as a filter. Any ideas????

#!/usr/bin/perl
use MIME::Tools;
use MIME::Parser;
use MIME::Decoder::QuotedPrint;
use MIME::Decoder::Base64;
use MIME::Decoder::Binary;
use MIME::Decoder::Gzip64;
use MIME::Decoder::NBit;
use MIME::Decoder::UU;
use MIME::Words qw(:all);
use MIME::Entity;
use MIME::Body;
use POSIX qw(strftime);
use strict;

sub Log
{
   print "* $_[0]\n";
}

$| = 1;

Log "Content-Filter is running";
Log "";

while (<>)
{
   my @line = split(/ /, $_);
        chomp($line[0]);

   Log "Attachment-Extractor-Filter starting";
   Log "";

   # Verifying the command send by the server. Only supports FILE for now
   if ($line[1] !~ /^file$/i)
   {
      print "$line[0] FAILURE\n";
   }
   else
   {
        $line[2] =~ s|\\|/|g; # Getting the filepath
        chomp($line[2]);
        my $file = "$line[2]";

         # extract attachment
        my (@body, $i, $subentity);
        my $parser = new MIME::Parser;
        #these are the types of attachments allowed
        my @attypes= qw(application/msword
                        application/vnd.ms-excel
                        application/pdf
                        application/octet-stream
        );

        my ($x, $newx, @attachment, $attachment, @attname, $bh, $nooatt);
        my $body;    #contains the message body

        $parser->ignore_errors(1);
        $parser->output_to_core(0);
        $parser->extract_nested_messages(1);
        $parser->use_inner_files(0);

    my $entity = $parser->parse_open("$file");
    my $error = ($@ || $parser->last_error);

    #get email body
    if ($entity->parts > 0){
        for ($i=0; $i<$entity->parts; $i++){
            $subentity = $entity->parts($i);
            if (($subentity->mime_type =~ m/text\/html/i) || ($subentity->mime_type =~ m/text\/plain/i)){
                $body = join "",  @{$subentity->body};
                next;
            }
            #this elsif is needed for Outlook's nasty multipart/alternative messages
            elsif ($subentity->mime_type =~ m/multipart\/alternative/i){
                $body = join "",  @{$subentity->body};
                #split html and text parts
                @body = split /------=_NextPart_\S*\n/, $body;
                #assign the first part of the message,
                #hopefully the text, part as the body
                $body = $body[1];
                #remove leading headers from body
                $body =~ s/^Content-Type.*Content-Transfer-Encoding.*?\n+//is;
                next;
            }
            #grab attachment name and contents
            foreach $x (@attypes) {
                if ($subentity->mime_type =~ m/$x/i){
                    $bh = $subentity->bodyhandle;
                    $attachment = $bh->as_string;
                    push @attachment, $attachment;
                    push @attname, $subentity->head->mime_attr('content-disposition.filename');
                }else{
                    #some clients send attachments as application/x-type.
                    #checks for that
                    $newx = $x;
                    $newx =~ s/application\/(.*)/application\/x-$1/i;
                    if ($subentity->mime_type =~ m/$newx/i){
                        $bh = $subentity->bodyhandle;
                        $attachment = $bh->as_string;
                        push @attachment, $attachment;
                        push @attname, $subentity->head->mime_attr('content-disposition.filename');
                    }
                }
            }
            $nooatt = $#attachment + 1;
        Log "* No Attchment $nooatt or Attchment name: $attname[$x] *\n";
        }
    }
    #Get current local time and format for file name output
    my $xdate;
    $xdate = strftime("%Y-%m-%d_%H-%M-%S", localtime());
    #write contents of each attachment to a file
    for ($x = 0; $x < $nooatt; $x++){
        open FH, ">/mnt/tts_fax/Incoming_FAX/Badge_Times_4014/$xdate-NTE-$attname[$x]";
        print FH "$attachment[$x]";
        close FH;
        Log "* Saved files to: /mnt/tts_fax/Incoming_FAX/Badge_Times_4014/$xdate-NTE-$attname[$x] *";
        Log "";
    }
    # Addheader
        print "$line[0] OK\n";
   }
}


#############################################################
This message is sent to you because you are subscribed to
 the mailing list <CGatePro@mail.stalker.com>.
To unsubscribe, E-mail to: <CGatePro-off@mail.stalker.com>
To switch to the DIGEST mode, E-mail to <CGatePro-digest@mail.stalker.com>
To switch to the INDEX mode, E-mail to <CGatePro-index@mail.stalker.com>
Send administrative queries to  <CGatePro-request@mail.stalker.com>
Subscribe (FEED) Subscribe (DIGEST) Subscribe (INDEX) Unsubscribe Mail to Listmaster