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$/

In reply to Re: Remove Email Attachments by jonadab
in thread Remove Email Attachments 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.