I read my mail in PINE, on a remote server. I've been doing it that way for years and it works for me. I never know where I might be or which computer I might be using, but I can usually telnet or ssh to this shell account.
Attachments, however, can be a pain in the butt. PINE is just a text thing. It isn't going to show me pictures or translate Word documents. Since I read this stuff on a remote account, I have to save the attachments then transfer them to my laptop. That can be a lot of steps, including choosing a file name then typing all the stuff that scp needs.
It really wasn't so much work that I felt very motivated to fix it, though. Well, until recently anyway. Lately I've received a lot of attachments that I want to save (opposed to the sorts that are trojans or virii).
Most of the mail with attachments ended up in my "to-do" mail folder, so I wrote a script to go through all of the messages in an mbox and extract all of the attachments. They end up in a sub-directory named after the email address of the sender.
#!/usr/bin/perl
use strict;
use warnings;
use ExtUtils::Command qw(mkpath);
use File::Spec::Functions qw(catfile);
use Mail::MboxParser;
ARGUMENT: foreach my $argument ( @ARGV )
{
unless( -r $argument )
{
warn "Cannot read from $argument: skipping\n";
next ARGUMENT;
}
my $mailbox = Mail::MboxParser->new( $argument );
unless( ref $mailbox )
{
warn "Could not parse mailbox $argument!\n";
next ARGUMENT;
}
MESSAGE: foreach my $message ($mailbox->get_messages)
{
my $from = from( $message );
my $attachments = $message->get_attachments;
next MESSAGE unless keys %$attachments; # no attachments
my $path = catfile( $ENV{HOME}, qw(Documents Attachments), $fr
+om );
{ local @ARGV = ( $path ); mkpath; }
ATTACHMENT: while( my( $name, $index ) = each %$attachments )
{
save( $message, $path, $name, $index );
}
}
}
sub from
{
my $message = shift;
my $address = $message->header->{from};
if( $address =~ m/<(.*@.*)>/ ) { $address = $1 };
return $address;
}
sub save
{
my( $message, $path, $name, $index ) = @_;
my $file = catfile( $path, $name );
my $last = (
sort { $b <=> $a }
grep { /^\d$/ }
map { s/^$file\.?//; $_ ? $_ : 0 }
glob( $file . "*" )
)[0];
$file .= "." . ($last + 1) if defined $last;
my $fh;
unless( open $fh, "> $file" )
{
warn "Could not write to $file: $!\n";
return;
}
unless( $message->store_entity_body( $index, handle => $fh ) )
{
warn "Could not save attachment for $file: " . $message->error
+ . "\n";
return;
}
}
That works, but then I thought of an easier way. Why not just pipe the message directly to a program to do it as I read them, rather than having to stop reading mail and process the mbox file?
With PINE this is fairly easy. I have to turn on the enable-full-header-and-text option to make this work: while reading a message, type "h" to show all the gory details, then "|" to pipe the message to an external program, then "a", the name of my attachment extracter (which has to be in my PATH). The "a" program uses MIME::Parser which does all of the work, including figuring out the right filenames and saving the bits.
#!/usr/local/bin/perl5.8.0
use strict;
use warnings;
use ExtUtils::Command qw(mkpath);
use File::Spec::Functions qw(catfile);
use MIME::Parser;
my $Base = $ENV{ATTACHMENT_ROOT} || $ENV{HOME};
my $message = do { local $/; <> };
my $from = from( \$message );
my $parser = MIME::Parser->new();
my $path = catfile( $Base, $from );
do { local @ARGV = ( $path ); mkpath; } unless -d $path;
$parser->output_dir( $path );
my $entity = $parser->parse_data( $message );
sub from
{
my $message = shift;
my( $from ) = $$message =~ m/^From:\s+(.*)/mg;
$from =~ s/\s* \(.*?\) \s*//x;
$from =~ s/.* < (.*@.*) > .*/$1/x;
return $from;
}
The files end up in one of my private web directories, so I go to that URL, which is just a directory listing, then choose the link to the email address of the message. There are my attachments. It's not as easy as clicking a link in web mail, but now everything that everyone sends me gets stored in an easily-accessible web directory.
The next step is a bit scary. Am I smart enough to let procmail do all of this for me? In that case I might consider ditching Perl in favor of munpack, which I saw in Wireless Hacks
--
brian d foy <bdfoy@cpan.org>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.