Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

extracting email attachments

by jrtaylor (Initiate)
on Apr 01, 2014 at 19:29 UTC ( #1080630=perlquestion: print w/replies, xml ) Need Help??

jrtaylor has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl program that I put together and I am having problems extracting attachments with file names with a "/". I just get a blank name. Here is the code I am using.
#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) || ($subenti +ty->mime_type =~ m/text\/plain/i)){ $body = join "", @{$subentity->body}; next; } #this elsif is needed for Outlook's nasty multipart/altern +ative 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('conten +t-disposition.filename'); }else{ #some clients send attachments as application/x-ty +pe. #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('co +ntent-disposition.filename'); } } } $nooatt = $#attachment + 1; } }
Here is the header
MIME-Version: 1.0 Importance: Normal X-Priority: 3 (Normal) X-Mailer: SAP NetWeaver 7.02 Content-Type: application/pdf; name="Payment Advice Note from 03/31/2014.PDF" Content-Transfer-Encoding: base64 Content-Description: Payment Advice Note from 03/31/2014 Return-Path: X-OriginatorOrg: X-FOPE-CONNECTOR: Id%0$Dn%*$RO%0$TLS%0$FQDN%$TlsDn%

Replies are listed 'Best First'.
Re: extracting email attachments
by toolic (Bishop) on Apr 01, 2014 at 19:40 UTC
    if ($subentity->mime_type =~ m/\Q$x/i){ if ($subentity->mime_type =~ m/\Q$newx/i){
      OK it is now getting the file name. How do get rid of the "/" in the file name so I can save it?
        Figured it out. This is what I added before I open the file for writting.
        $attname[$x]=~ s/[^A-Za-z0-9\-\.]//g;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1080630]
Approved by sweetblood
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-12-07 19:09 GMT
Find Nodes?
    Voting Booth?

    No recent polls found