remove 'Content-length' and 'Lines' fields from multipart messages

Koen Zeilstra <[email protected]> Tue, 1 Mar 2005 13:29:58 +0000 (UTC)
Newsgroups gmane.comp.lang.perl.modules.mail-box
Message-ID <[email protected]>
Hi List,

With the following code I am trying to send a multipart message. 
Unfortunately the receiver is very strict on receiving such messages. 
Therefor some (at first sight) strange options are chosen here.

[code]

  	# create headers
         my %headers;
         $headers{'From'} = $from;
         $headers{'To'} = $recipient;
         $headers{'Subject'} = $subject;
         my $head = Mail::Message::Head->build(%headers);



         # create body with text (first part of multipart message)
         my $body = Mail::Message::Body::Lines->new
                 ( data => \@text
                 );


         # create disposistion field for attachment
         my $disp = Mail::Message::Field->new
                 ('Content-Disposition'  => 'attachment'
                 , filename              => $file
                 );


         # create attachment (is a also a body and second part of multipart
message)
         my $att = Mail::Message::Body::Lines->new
                 ( file                  => $fullPath
                 #, mime_type            => 'text/plain'
                 , mime_type             => 'application/octet-stream'
                 , disposition           => $disp
                 );

         $att = $att->encode
                 ( transfer_encoding     => 'base64'
                 );



         # attach att to first body (ie create a multipart)
         my $multipartBody = $body->attach($att);


         $multipartBody->boundary('ThisIsABoundaryLine');

         my $msg = Mail::Message->buildFromBody
                 ( $multipartBody
                 , $head
                 );



         $msg->print;

         $msg->send;

[/code]

Unfortunately two fields are added to the multipart body when the 
attachment gets attached. Those fields are 'Content-length' and 'Lines'. 
In my case the sender doesn't know how to handle those and refuses the 
message. I would like to remove those fields before I sent the message. 
The method I use is to use the removeFields function on each body->head. 
Since each body has a header with those 2 fields in it. Find the code 
below:

[code]
 	# remove 'Lines' and 'Content-Length' from message
         $msg->head->removeFields('Lines','Content-Length');


         # remove 'Lines' and 'Content-Length' from each part
         foreach my $part ($msg->parts) {
                 $part->head->removeFields('Lines','Content-Length');
         }

[/code]


Hope it helps.

greetz,

Koen



-----------------------
Economists state their GNP growth projections to the nearest tenth of a
percentage point to prove they have a sense of humor.
 		-- Edgar R. Fiedler