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

How do you send the data from a form to an email address using HTML in Perl? This is what I have using use Win32::ODBC;
print "<form method='post' action='mailto:someone\@somewhere.com'>"; print "<input type='hidden' name='CustomerNumber' value='$Custome +rNumber'>"; print "<input type='hidden' name='Name' value='$Name'>"; print "<input type='hidden' name='Address1' value='$Address1'>"; print "<input type='hidden' name='City' value='$City'>"; print "<input type='hidden' name='State' value='$State'>"; print "<input type='hidden' name='ZipCode' value='$ZipCode'>"; print "<input type='hidden' name='Email' value='$Email'>"; print "<center><input type='image' name='submit_change_passwo +rd' src='/images/graphics/change-password-button.jpg'>"; print "</center></form>";

Replies are listed 'Best First'.
Re: submit form data thru email
by footpad (Abbot) on Apr 17, 2001 at 00:02 UTC

    Check out merlyn's article on the subject; I found it very useful when working on a similar project.

    While working on the project, I made some observations you may find helpful (note that the following code has been simplified from the final, so certain typos may have been made):

    • You can add additional portions to the email header by calling the add method of your MIME::Lite object. For example, my project allowed optional copies sent to other recipients:

    if ( $copiesto ) { $msg->add( CC => $copiesto ); }
    • If you want the uploaded file as a separate attachment (as opposed to inline with the message body), control that with Disposition. For example:

    $msg->attach ( Disposition => 'attachment', Type => $sentinfo->{ 'Content-Type' }, Encoding => 'base64', Filename => $sentname, FH => $sentfile );
    • Also note that the above sets the attachment type according the the information provided by the browser, though be aware that you may get weird results when certain MIME types use the same extensions. For example, I uploaded a stylesheet during testing and it was described as a Comet Systems Cursor in the email message. Your mileage may vary.

    • During development, I enabled a Debug flag to help me troubleshoot problems. This resulted in a second attachment containing information I thought relevant at the time. For example:

    if ( defined( $settings{ "DEBUG" } ) ) { $msg->attach ( Type => 'TEXT', Encoding => 'base64', Filename => 'debug.txt', Disposition => 'attachment', Data => [ "Upload info\n\n", "File size = $sentsize\n", ( map { "$_ = $sentinfo->{ $_ }\n" } sort keys %$se +ntinfo ), "\n", "ENV Contents:\n\n", ( map { "$_ = $ENV{ $_ }\n" } sort keys %ENV ), "\n", "Configuration Settings:\n\n", ( map { "$_ = $settings{ $_ }\n" } sort keys %setti +ngs ) ] ); }
    • You may find it helpful to use File::Basename to set the file name of the attachment, especially if you're getting uploads from Win32 clients and people are uploading files using long file names. My quick and dirty hack for this was something along these lines:

    my $cgi = new CGI; my $sentfile = $cgi->upload( 'sentfile' ); my $sentsize; my $sentinfo; my $sentname; if ( $sentfile ) { $sentsize = -s ( $sentfile ); # file size $sentinfo = $cgi->uploadInfo( $sentfile ) ; fileparse_set_fstype( "MSDOS" ); # risky assumption? my ( $sentname ) = fileparse( $sentfile ); }

    It's not perfect by any means, but it worked well and was a reasonably easy job--once I had merlyn's starting point.

    In short, merlyn's article is very helpful, but you'll want to read the MIME::Lite docs pretty carefully and to experiment with the article's code to see how it works on your system.

    Also, there are several other threads in the archives that offer alternative approaches and other information.

    --f

Re: submit form data thru email
by THRAK (Monk) on Apr 16, 2001 at 23:41 UTC
    From your displayed fill in form (with the fields you list as hidden above) you use the form "action" of the Perl script you want to run (i.e. action='/cgi-bin/mailer.pl') Use the CGI module to retrieve the form fields, build the message you want to send then use Net::SMTP or Mail::Mailer to actually send it on it's way.

    -THRAK
    www.polarlava.com
Re: submit form data thru email
by dws (Chancellor) on Apr 16, 2001 at 23:35 UTC
    mailto: isn't a valid target for a form. You can't count on browsers honoring it, let alone them doing the right thing.

    You're going to have to post the form back to the server, and send the mail from there.