http://www.perlmonks.org?node_id=20631

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

I need to accept a form submission, capture the name of a file (name is passed in the cgi param), extract the user's email address from the form data, save the form data to a log file, attach the file whose name corresponds to the name passed in the cgi param and email it to the user.

I think I've captured and regexed the form data properly, I've written the data to a hash, and opened the pipe to sendmail. The questions are: are there any obvious problems in the code so far AND how the heck do you attach a file to sendmail ??



here's the code I have now .. with comments removed for space

use strict; use CGI; $sendmail = '/usr/lib/sendmail'; $file = param('file'); $formdata=<STDIN>; $formdata=~s/\s+$//; foreach (split(/&/, $formdata)) { ($name, $value)=split(/=/, $_); $name=~s/\+/ /g; $name=~s/%([0-9|A-F]{2})/pack(C,hex($1))/eg; $value=~s/\+/ /g; $value=~s/%([0-9|A-F]{2})/pack(C,hex($1))/eg; $formash{$name} = $value; } $receiver = $formash{email}; open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!"; print SENDMAIL "To: $receiver \n"; print SENDMAIL "From: info\@constellar.com \n"; print SENDMAIL "Subject: Whitepapers Request \n"; close(SENDMAIL);

Replies are listed 'Best First'.
Re: extract data from a form .. and attach a file in sendmail
by KM (Priest) on Jun 30, 2000 at 21:52 UTC
    Look at the MIME::* (like MIME::Lite) modules, as well as Net::SMTP. These modules will help you with attachments.

    Cheers,
    KM

Re: extract data from a form .. and attach a file in sendmail
by merlyn (Sage) on Jun 30, 2000 at 21:49 UTC
Re: extract data from a form .. and attach a file in sendmail
by csorensen (Beadle) on Jul 01, 2000 at 00:22 UTC
    from the sendmail faq

    Subject: Q4.15 -- How do I create attachments with sendmail?
    You don't. Sendmail is a mail transfer agent (MTA). Creating e-mail messages, including adding attachments or signatures, is the function of a mail user agent (MUA). Some popular MUAs include mutt, elm, exmh, Netscape, Eudora and Pine. Some specialized packages (metamail, some Perl modules, etc.) can also be used to create messages with attachment.

    I guess that puts my idea of attaching files to rest .. I was really hoping to avoid using more modules !

Re: extract data from a form .. and attach a file in sendmail
by chromatic (Archbishop) on Jul 01, 2000 at 04:34 UTC
    I'm confused about the $formdata transformation. If you know that you will be receiving form fields like 'email' and 'name', why not just get at them with:
    $name = param('name'); $email = param('email');
    Even if you don't know them, in list context CGI::param() will return the names of all of the submitted fields. You can loop over them:
    foreach (param()) { $fields{$_} = param($_); }
      there's alot of other stuff going on in the script that I'm not showing (since the rest of it works fine)
Re: extract data from a form .. and attach a file in sendmail
by Anonymous Monk on Jul 02, 2000 at 02:18 UTC
    Look at MIME::Lite. Attachments are just a mime type. I used MIME::Lite to send emails with HTML attachments. With MIME::Lite you can send any type of file as it is just encoded and sent at the end of the email.
Re: extract data from a form .. and attach a file in sendmail
by Anonymous Monk on Jul 01, 2000 at 05:39 UTC
    I may be wrong but aren't attachments just part of a multipart/mixed mime type or something like that?