Hacker, author, trainer
Technorati Profile [technorati.com]
I love it when Perl makes my life easier.
I read my email in mutt on a remote server. This is fine, but can be a bit of a problem when I want to access a MIME attachment. Then I need to save the attachment to the filesystem and use scp to copy it to my local system.
But after watching Casey's talk on filtering email on the Perl Whirl, I spent 20 minutes knocking up this program which saves attachments to a directory in my web server's document tree. Now I can just go to a (password protected) page on my web site and read any attachment.
Email::MIME is a great module.
#!/usr/bin/perl
use strict;
use warnings;
use Email::MIME;
use constant DUMP => '/path/to/some/directory';
my $email = Email::MIME->new(join '', <>);
save_parts($email);
sub save_parts {
my ($mime) = @_;
return unless $mime->content_type;
if ($mime->content_type !~/^(text|multipart|message)/) {
my $file = DUMP . $mime->filename(1);
open FILE, '>', $file or die $!;
print FILE $mime->body;
close FILE;
chmod 0644, $file;
}
my @parts = $mime->parts;
if (@parts > 1) {
save_parts($_) for @parts;
}
}
Separated by a common language (Score:1)
I don't think any American writer would have used that phrase in quite that fashion.
I remember reading in a Sherlock Holmes story, when Holmes wakes up Dr. Watson by knocking on his door early in the morning, he apologizes by saying:
(I may not have the quote 100%, but it's very close.)
Re:Separated by a common language (Score:2)
-Dom
Similar to what I have (Score:1)
Nine is quite a bit more elaborate though. Notable differences:
so I don't get stuff inadvertantly overwritten. Also,
so I don't have to look at MIME types that way only seemed to lead to madness. (What're you passing a parameter to
->filename()for btw?)Other reaso
Re:Similar to what I have (Score:1)
Re:Similar to what I have (Score:2)
The docs say 'Normally it will return the filename from the headers, but if filename is passed a true parameter, it will generate an appropriate "stable" filename if one is not found in the MIME headers.' So it's a safety feature. It invents a filename if one isn't already given.
Re:Similar to what I have (Score:1)
Same thing, but different (Score:2)
Path Attacks? (Score:1)
Davorg knocked up a script that contained:
What happens when I send you an attachment with a filename of ../../../../../home/davorg/.ssh/authorized_keys, or perhaps more innocently .htaccess?
This is an excellent use for File::Basename, and Aristotle's previous sysopen() involving O_EXCL|O_CREAT.
No prizes for guessing which course [perltraining.com.au] I've been recently reviewing.
Re:Path Attacks? (Score:2)
It's also been pointed out that an attachment called