update: Roger's
answer looks even better.
Parsing out the username is easy, if you have the
SMTP envelope, or the recipient email address. (If
you don't, it's not possible in the general case,
since the TO header frequently
does not contain the recipient's email address.)
if ($toaddress =~ /(\S+)[@]yourdomain|yourotherdomain/) {
$user = $1;
} else {
warn "Unknown user: $toaddress\n";
$user = 'postmaster'; # Or whoever should get slop.
}
If you don't have the envelope available, you can TRY
to get the recipient's address from the To or Cc fields.
Something like this might work most of the time:
# This is untested.
@users = map {
/^(\S+)[@]/ ? $1 : undef
} grep {
/(yourdomain|yourotherdomain|yourthirddomain)$/
} map {
s/\".*?\"//g;
(/[<](\S+?)[>]/) ? $1 : undef
} map {
split /,\s*/, $_
} $headers =~ m/^(?:To|Cc)[:]\s*(.*?)$/mig;
if (not @users) {
warn "No users.\n";
@users = ('postmaster');
}
For the attachments, you probably want to look at
MIME-related modules on CPAN, such
as MIME::Tools.
Placing the thing in the user's home directory should
be easy. Assuming *nix, you'd use something like
this:
my $filename = "attachment"; {
my ($y, $m, $d) = (localtime)[5,4,3];
$y += 1900; $m += 1;
$filename = join "-", $filename, $y, $m, $d;
}
# I assume $user and $ext are already set to the
# username and the filetype extension respectively.
while (-e "/home/$user/$filename.$ext") {
$filename .= ('a'..'z')[rand 26];
# It is possible to be more elegant than this.
}
open FILEHANDLE, ">/home/$user/$filename.$ext";
binmode FILEHANDLE; # Probably not necessary on *nix.
print FILEHANDLE $content;
close FILEHANDLE;
You'll need write permissions in the user's directory.
If that's a problem, you can change the /home/
prefix and instead use some neutral location where
all the users have directories but you have write
permissions to all of them (e.g., by belonging to
the group that owns them). The root user could if
desired then create a link to each user's
attachments directory within his home directory.
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
|