in reply to send mail with attachment using SMTP

i had to write a script for a company that led me towards MIME::Lite

The company wanted a form that would allow people to submit/upload resume files; and have the script email those resumes to their personnel department. sendmail was not a requirement, so i used the company's SMTP server (so that it would work on both Win2k/linux)

on the form, there is a field named 'resume' that is of type 'FILE'. when the user submits the form, i read in the data submitted in the form field 'resume' and put it in the hash key 'resume_data'.

Then, i break out MIME::Lite and attach the data contained in that hash key to the email message.

This enables me to send an email with the user's resume attached without ever having to save the resume to a file. ;)

keep in mind that the following code chunks are ripped out of the larger script... so it might not work as expected if you just cut-n-paste the code
#$i is an object i declared previously #read in all values (using CGI.pm) my @names = &param(); foreach my $field (@names) { $FORM{$field} = &param($field); } #read in the data uploaded, and save it into a hashkey if ($FORM{'resume'}) { #read in the resume's data my ($data,$size); while( $size = read( $FORM{'resume'}, $data, 1024) ) { $FORM{'resume_data'} .= $data; $FORM{'resume_size'} += $size; } } #---- #resume is the form field of type 'FILE' $FORM{'resume'} =~ m!([^/:\\]*)$!; #get file name/ext my $shortname = $1; #file name (with no slashes or colons +) #---- #create a new message my $msg = MIME::Lite->new( From => "$FORM{'email'}", To => "personnel\@company.com", Subject => "Resume", Type => 'TEXT', Data => "Resume is <attached> to this email as <$shortname>", ) || return $i->error("Cannot create new email message object!"); #attach resume data $msg->attach( Type => 'application/octet-stream', Encoding => 'base64', Data => $FORM{'resume_data'}, Filename => $shortname, ) || return $i->error("Cannot attach your resume to the email!"); #tell MIME::Lite how to send this email MIME::Lite->send('smtp', 'company.com', Timeout=>30) || return $i->error("Unable to set the email 'send' properties!"); #actually send it $msg->send() || return $i->error("Unable to send email with your resume!");