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

I am a novice programmer of perl and I would like to create a CGI script that reads form data and attaches an image. I am able to send data with $SMTP, and I can upload, open and store in an array the image. My problem is that when I send the scalar through $SMTP it comes out in machine language (probably hex or ascii). I am unable to send the image as an image. My server uses a windows based environment, not UNIX, and so I cannot use the sendmail function or MIME to transfer the data. I am a little bewildered. Any help would be great.

Edit kudra, 2001-12-02 Changed title

Replies are listed 'Best First'.
(Ovid) Re: Bewildered in a Land of Shiny Perls
by Ovid (Cardinal) on Nov 28, 2001 at 06:22 UTC

    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.

      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.

Re: Bewildered in a Land of Shiny Perls
by dws (Chancellor) on Nov 28, 2001 at 06:27 UTC
    I am a little bewildered. Any help would be great.

    It's often hard when bewildered to get your arms around exactly what it is you're trying to achieve, but going through that exercise will get you into a position where we can better help you. As it is, I've read your post three times, and am still unclear on what it is you're trying to do. I'm guessing that you're trying to upload an image and have the web site send email. Is that right?

    Try this: describe what you are trying to do in terms of what steps you expect to perform, and how you expect the system to respond. Something like "I want to upload an image to my web site, and have the web site send the image by email. I first bring up an 'upload' page in my browser, then I... and then I...," etc.

Re: Bewildered in a Land of Shiny Perls
by dug (Chaplain) on Nov 28, 2001 at 08:45 UTC
    Hello, What you were seeing wasn't machine code, it was an image (albiet not decoded into a pretty picture) in the body of the email. As IlyaM suggested, you will want to wrap the image in a MIME boundary in your email, probably with MIME::Lite . A google search for "mime rfc" will give you some good reading material on MIME (Multipurpose Internet Mail Extensions),
Re: Bewildered in a Land of Shiny Perls
by IlyaM (Parson) on Nov 28, 2001 at 06:25 UTC
    Can you be more clear? Where do you need to attach an image? Do you need to create email with attached image and send it?

    If former use MIME::Lite to create MIME message and send it via SMTP using Net::SMTP.