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

Have been trying to create a script to strip an email of all binary attachments. The test email has 25 parts, encompassing 24 photos as attachments, which leaves the email headers and the first 'part', the plain text body.

#!/usr/bin/env perl # use strict; use warnings; use Data::Dumper; use File::Slurp qw( read_file ); use Email::Address; use Email::MIME::Attachment::Stripper; use File::Slurp qw(slurp write_file); my $infile = '/home/****/Mail/.family.directory/1277709595.16641.pRLS +b:2,S'; my $outfile = 'output4.txt'; my $intext = File::Slurp::read_file( $infile ); my $stripper = Email::MIME::Attachment::Stripper->new($intext); my $email_mime = $stripper->message; print $email_mime; # displays "Email::MIME=HASH(0x1601e60)" my @emails = Email::Address->parse( $email_mime ); File::Slurp::write_file( $outfile, join("\n", @emails) ); # no outp +ut ? my $parsed = Email::MIME->new($intext); #print "Parsed content :\n". Dumper( $parsed) . "\n"; my $parts = $parsed->parts; print "Number of email parts : $parts\n"; # displays "Number of ema +il parts : 25" my @parts = $parsed->parts; foreach my $attachment ( $stripper->attachments ) { write_file( $attachment->{filename}, { buf_ref => \$attachment->{payload} } ) or die "Can't write $attachment->{filename}: $!\n"; }

The code just above (the "write_file")works okay, and all 24 photos are extracted and written to disk. But the part I want to process is the email headers plus the first part. Have been looking at the docs for this module, and it seems all I have to do is ..

my $stripper = Email::MIME::Attachment::Stripper->new($intext); my $email_mime = $stripper->message;

to get the message part. I can't get it to extract the first part, the 'message' though. Have even downloaded the source for this module; the sub 'message' does a 'detach_all. Any clues please ?

Replies are listed 'Best First'.
Re: Stripping emails of attachments
by Anonymous Monk on Dec 07, 2015 at 16:10 UTC
    Hello Peter, I tried to run your script and I had an error. Since I am newbie, I would like to ask your help. Number of email parts : 3 write_file '' - sysopen: No such file or directory at ./test2.pl line 33 Thank you!

      Have you looked at line 33? What is in line 33? Maybe some filename given there is not writable on your system. Note also, that this heavily depends on the attachments of your mails.