Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

web form email attachment

by koryw (Novice)
on Oct 22, 2004 at 20:03 UTC ( [id://401662]=perlquestion: print w/replies, xml ) Need Help??

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

I've started to write a cgi perl script that will parse a web email form. This web form will need the capability of sending email attachments that could be a MS Word, Excel, jpeg, Hmtl or text file, to recipients. On the web form I have the following "To" input box, "Textarea" box to type the body of the message, "Subject" box, Email attachment using <input type="file" name="filename"> I've heard that the mime Module is needed to proceed with this project. I was wondering if anyone has sample code that has already accomplished this task.

Replies are listed 'Best First'.
Re: web form email attachment
by Joost (Canon) on Oct 22, 2004 at 20:22 UTC
    The easiest module I've found for sending emails with attachments is MIME::Lite. The standard CGI module will handle file-uploads well. Sorry, I have no ready script laying about that does exactly this.

    Also note that you might not want any random person on the net to send email to any other random email address (I'm referring to the "To" field in your description) - I've seen too many spammers abuse mail scripts like that. The web is not as friendly as it was when I started my programming career :-)

Re: web form email attachment
by csuhockey3 (Curate) on Oct 23, 2004 at 07:30 UTC
    For a file upload form, MIME::Lite is the way to go, but I found a really old one I did a while back that uses MIME::QuotedPrint and MIME::Base64 if you are interested -- it's old, but should give you a good place to start and some ideas. Sort of the full calorie MIME::Lite. I echo joost about the danger of spammer abuse with a script like this -- is this form going to be wide open to the net?

    #!/usr/bin/perl -w use MIME::Base64; use MIME::QuotedPrint; use Mail::Sendmail; use CGI qw(:standard); use CGI::Carp qw/ fatalsToBrowser /; # remove for production my $cgi = new CGI; print $cgi->header; print $cgi->start_html(); #get values as below my $a_File = $cgi->param('image'); ... if($a_File =~ m!/!){ @data = split /\//, $a_File; $a_Filename = pop @data; }elsif($a_File =~ m!\\!){ @data = split /\\/, $a_File; $a_Filename = pop @data; }else{ $a_Filename = $a_File; } if ($a_Filename){ open(a_tmpFile, "</tmp/$a_Filename") or die $!; binmode a_tmpFile; undef $/; while(<a_tmpFile>){ $base64_a_File = $base64_a_File . encode_base64($_); } close a_tmpFile; } ########### MESSAGE ##################### %mail = ( SMTP => '', from => '', to => '', subject => '', ); $boundary = "====" . time() . "===="; $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\""; $boundary = '--'.$boundary; $mail{body} = <<END_OF_BODY; $boundary Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable if($a_Filename){ $mail{body} = $mail{body} . <<END_OF_A_FILE; $boundary } #would list all params here #param: $value as examples below Contact Name: $contactName Contact Phone: $contactPhone Contact Email: $contactEmail if($a_Filename){ $mail{body} = $mail{body} . <<END_OF_A_FILE; $boundary Content-Type: application/octet-stream; name="$a_Filename" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="$a_Filename" $base64_a_File END_OF_A_FILE } END_OF_BODY sendmail(%mail) || print "Error: $Mail::Sendmail::error\n"; print "<p><b>Thank You.</b>"; #END
    note: This is an old script and I stripped out the extraneous code and left what is important, thus it is untested.
      I tried running the code above, modifying it to suit my needs and I'm receiving a "SOFTWARE ERROR:" which is produced from the CGI::Carp qw/ fatalsToBrowserr module on line 30 of the code. Here is that line open(a_tmpFile, "</tmp/$a_Filename") or die $!;
        post that snippet so we can have a look at it...Like I said, I stripped it pretty good to get to the meat of the deal, but I am happy to be more help if you post some of the code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://401662]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found