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 :-)
| [reply] |
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. | [reply] [d/l] |
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 $!;
| [reply] |
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.
| [reply] |