I have a sub that extracts email attachments (found the code in perlmonks :-)) ... but when i send a mail in html format in the output directory along with the attachments 2 files (msg-3708-1.txt and msg-3708-2.html) are created , i need to delete the files ,if i send the mail in text format i can do this with $subentity->bodyhandle->path; but when the mail is sent in HTML i get error saying cant call method path on a undefined value ..can someone help me on this
sub parse_email { my $parser = new MIME::Parser; $userdir=$dir.$userpin; $parser->output_dir($userdir); $entity = $parser->parse_data($message); ###################################################################### +######################## if ($entity->parts > 0){ for ($i=0; $i<$entity->parts; $i++){ $subentity = $entity->parts($i); my $path; print " MIME Type is ",$subentity->mime_type, "\n"; if (($subentity->mime_type =~ m/text\/html/i) || ($subentity-> +mime_type =~ m/text\/plain/i)){ $body = join "", @{$subentity->body}; $path=$subentity->bodyhandle->path; print " DEL path is $path \n"; if ($path =~ /msg-\d+.*\.txt/ || $path =~ /msg-\d+.*\.html +/ ) { unlink ($path) || ($ulnerr = -1); if ($ulnerr == -1) { print "Couldn't unlink $path\n"; &writelog("Cannot delete file $path"); } } #new attachment code start next; #new attachment code end } #this elsif is needed for Outlook's nasty multipart/alternativ +e messages elsif ($subentity->mime_type =~ m/multipart\/alternative/i){ print " HERE in ELSIF path is $path \n"; $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; #new attachment code start next; #new attachment code end } push @attname, $subentity->head->mime_attr('content-dispositio +n.filename'); #new attachment code end } } else { print " In Else \n"; $body = join "", @{$entity->body}; } #body may contain html tags. they will be stripped here $body =~ s/(<br>)|(<p>)/\n/gi; #create new lines $body =~ s/<.+\n*.*?>//g; #remove all <> html tages $body =~ s/(\n|\r|(\n\r)|(\r\n)){3,}//g; #remove any extra new lines $body =~ s/\&nbsp;//g; #remove html &nbsp characters #remove trailing whitespace from body $body =~ s/\s*\n+$//s; print " BODy is $body \n"; ###################################################################### +######################### }

In reply to Mime::Parser Help by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.