in reply to Attaching an image to mail

I'm a little bewildered by what you are asking. I read your post several times and I still don't get it. What I think you're asking is "If I upload an image from a form via CGI, how can I send that image as an attachment on an email?".

Is that correct? If so, post some of your code so we can see what you've done so far and we can help you out. Heck, even if that's not what you meant, post your code anyway and we'll see if we can figure it out :) Just be sure to wrap your code in <CODE></CODE> tags.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

  • Comment on (Ovid) Re: Bewildered in a Land of Shiny Perls

Replies are listed 'Best First'.
Re: (Ovid) Re: Bewildered in a Land of Shiny Perls
by Segreto (Initiate) on Nov 28, 2001 at 06:33 UTC
    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
      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.

      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.