in reply to (Ovid) Re: Bewildered in a Land of Shiny Perls
in thread Attaching an image to mail

Uh...yeah I was asking if I upload an image from a form via CGI how can I send an image that is an attachment to an e-mail address. Here is the relevant code :
sub SendMail { $smtp = Net::SMTP->new($mailserver); $smtp->mail ($ENV{USER}); $smtp->to($to); $smtp->data(); $smtp->datasend("To: Wheels and Deals \n"); $smtp->datasend("From: $client \n"); $smtp->datasend("Subject: $subject \n"); $smtp->datasend("\n"); $smtp->datasend("Ad Type $choice \n"); $smtp->datasend("Client Phone Number $phone \n"); $smtp->datasend("Ad Info : \n"); $smtp->datasend("Year $year "); $smtp->datasend("Make $make "); $smtp->datasend("Model $model \n"); $smtp->datasend("Description $description \n"); $smtp->datasend("Ad Phone Number $adphone \n"); $smtp->datasend("Client Will Pay by : $payment \n"); $smtp->datasend("Submitted on $datesent \n"); $smtp->datasend("Attachments : \n"); if (@attach) { foreach (@attach) { $value ++; $photoad .= $attach[$value]; $smtp->datasend("$photoad"); } } else { $smtp->datasend("none \n"); } $smtp->data(@attach); $smtp->dataend(); $smtp->quit; } ###################################################################### +### sub AttachMe { #Check to see if an allowed file type $info = uploadInfo($photo); $kind = $info->{'Content-Type'}; open (UPLOAD,">attach") || Error('File could not be opened'); my ($data, $length, $chunk); while ($chunk = read ($photo, $data, 1024)) { print UPLOAD $data; $length += $chunk; if ($length > 1048576) { $erMsg = "The file is too big. Maximum File size is 1MB"; unlink ("/c:/inetpub/wwwroot/wheelsdeals/cgi-bin/attach") || Error('Fi +le does not exist'); Error ($erMsg); exit; } } @attach = <UPLOAD>; close (UPLOAD); print "$photo of $kind was uploaded to server."; if ($erMsg) { Error ($erMsg); } } ###################################################################### +### sub Error { if ($_[0]) { print "$_[0] \n"; print "$! \n"; exit; } else { print "ERROR of Unknown type. Please contact system administrator."; print "$! \n"; exit; } }
I am able to get a picture to open, be parsed into the @attach array, then I can read the @attach array, but as garbled m achine language. My site URL is http://www.wheelsdeals.net/wswd_private_ad_form.html for the html and an example of what happens. If you have time to glance through it or test it out that would be great. Thanks OVID

Replies are listed 'Best First'.
Re: Re: (Ovid) Re: Bewildered in a Land of Shiny Perls
by IlyaM (Parson) on Nov 28, 2001 at 06:43 UTC
    Don't read UPLOAD to @attach. Pass it directly to MIME::Lite to create MIME message. Here bare bones of untested code:

    # create message use MIME::Lite; $msg = MIME::Lite->new( From =>'me@myhost.com', To =>'you@yourhost.com', Subject =>'Helloooooo, nurse!', Type =>'image/gif', Encoding =>'base64', FH => \*UPLOAD, ); # send it using Net::SMTP my $smtp = Net::SMTP->new($mailserver); $smtp->mail ('me@myhost.com'); $smpt->to ('you@yourhost.com'); $smtp->data($msg->as_string);

    See docs for relevant modules.

    Update: fixed a couple of bugs and removed Cc: header from MIME message.

Re: Re: (Ovid) Re: Bewildered in a Land of Shiny Perls
by dws (Chancellor) on Nov 28, 2001 at 06:35 UTC
    If you are on a Win32 box, you'll need to add   binmode(UPLOAD); after you open it for writing. Otherwise it'll get garbled.