images not posting to email from form

I have no clue why this isnt working. I have read and read, then done alot of Trial and Error and I cannot seem to get it.

Basically I am trying to send a quote with 5 product images that I attach via the form. all is good but the images are not being sent.

If anyone can tell me what I am missing or doing wrong I would greatly appreciate it.

HTML

[code]

Email:
<label for="quote">Quote #: </label>
<input type="text" id="quote" name="quote" /><br/>

<label for="item">Item: </label>
<input type="text" id="item" name="item" /><br/>

<label for="inventory">Inventory: </label>
<input type="text" id="inventory" name="inventory" /><br/>

<label for="category">Category: </label>
<input type="text" id="category" name="category" /><br/>

<label for="manu">Manufacturer: </label>
<input type="text" id="manu" name="manu" /><br/>

<label for="model">Model: </label>
<input type="text" id="model" name="model" /><br />

Condition:

<label for="dimension">Dimension: </label>
<input type="text" id="dimension" name="dimension" /><br />




<label for="desc">Description: </label>
<textarea id="desc" name="desc"></textarea><br/>

<label for="img1">Image 1:</label>

Image 2:

Image 3:

Image 4:

Image 5:

<label for="skidprice">Skid Price: </label>
<input type="text" id="skidprice" name="skidprice" /><br/>

Unit Price:

<label for="totalprice">Total Price: </label>
<input type="text" id="totalprice" name="totalprice" /><br/>

<input name="submit" type="submit" class="submit-button" value="Submit" />
[/code]

PHP
[php]<?php

$email = $_POST ['email'];
$quote = $_POST ['quote'];
$item = $_POST ['item'];
$inventory = $_POST ['inventory'];
$category = $_POST ['category'];
$manu = $_POST ['manu'];
$model = $_POST ['model'];
$condition = $_POST ['condition'];
$dimension = $_POST ['dimension'];
$desc = $_POST ['desc'];
$skidprice = $_POST ['skidprice'];
$unitprice = $_POST ['unitprice'];
$totalprice = $_POST ['totalprice'];
$img1 = $_FILE ['img1'];
$img2 = $_FILE ['img2'];
$img3 = $_FILE ['img3'];
$img4 = $_FILE ['img4'];
$img5 = $_FILE ['img5'];

function imgReisize($uploadedfile, $Destination, $Thumb){
//this is the function that will resize and copy our images

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 150 pixels high, and maintain the original aspect
// ratio. This prevents the image from being “stretched”
// or “squashed”. If you prefer some max height other than
// 150, simply change the $newheight variable
$newheight=150;
$newwidth=($width/$height)*150;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = $Thumb;
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.

echo "Successfully Uploaded: ";
}

if(isset($_POST[submit])){
 $imgNumb=1; //This the "pointer" to images
 $DestinationDir="./quote/images/";  //Place the destination dir here
 $ThumbDir="./quote/images/thumbs/";  //Place the thumb dir here

   while($_FILES["img".$imgNumb][tmp_name]){
          $Unique=microtime(); // We want unique names, right?
          $destination=$DestinationDir.md5($Unique).".jpg";
          $thumb=$ThumbDir.md5($Unique).".jpg";
          imgReisize($_FILES["img".$imgNumb][tmp_name], $destination, $thumb);
          $imgNumb++;
   }
}

foreach ($_FILES[“img”][“error”] as $key => $error)
{
$tmp_name = $_FILES[“img”][“tmp_name”][$key];
if (!$tmp_name) continue;

   $name = basename($_FILES["img"]["name"][$key]);

if ($error == UPLOAD_ERR_OK)
{
    if ( move_uploaded_file($tmp_name, "/tmp/".$name) )
        $uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";
    else
        $errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";
}
else $errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";

}

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: All Food Equipment<[email protected]>' . "\r\n";

$headers .= "Message-ID: <".$now." sales@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

$subject = "Your All Food Equip Quote";

$msg = 
		"	
		<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">

Your All Food Equipment Quote:


Quote #:......................................................... $quote
Item:.............................................................. $item
Inventory:....................................................... $inventory
Category:....................................................... $category
Manufacturer:................................................. $manu
Model:........................................................... $model
Condition:...................................................... $condition
Dimension:.................................................... $dimension
Description:................................................... $desc
Skid Price:................................................. $skidprice
Unit Price:................................................. $unitprice
Total Price:................................................ $totalprice
\"Img\"\"Img\"\"Img\"\"Img\"\"Img\"

How to contact us:

  • Visit us Online: www.allfoodequip.com
  • Contact: Dino Roberts
  • Direct Line: 615-788-2953
  • Email: [email protected]
  • All Food Equipment
  • 1240 Industrial Park Road
  • Columbia, TN 38401
  • Main Office: 931-490-1977
";
// MAIL SUBJECT
 $subject = "Your All Food Equip Quote";
// TO MAIL ADDRESS
$to="$email";

mail($to, $subject, $msg, $headers);

// Redirect
header(“Location: Admin.php?Msg=Sent”);

?>[/php]

First of all, you have been using $_FILE, when the correct array is $_FILES

[php] $img1 = $_FILE [‘img1’];
$img2 = $_FILE [‘img2’];
$img3 = $_FILE [‘img3’];
$img4 = $_FILE [‘img4’];
$img5 = $_FILE [‘img5’];[/php]

should be

[php] $img1 = $_FILES[‘img1’];
$img2 = $_FILES[‘img2’];
$img3 = $_FILES[‘img3’];
$img4 = $_FILES[‘img4’];
$img5 = $_FILES[‘img5’];[/php]

You’ve also then tried to use these variables as the “src” of the image. The “src” of the image needs to be the full path to where it has been uploaded, e.g.:

http://your-website.com/images/some-img.jpg

In addition to this (at least in my tests), using the img[] syntax produces an array of:

[code]
Array
(
[img] => Array
(
[name] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)

        [type] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 
                [4] => 
            )

        [tmp_name] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 
                [4] => 
            )

        [error] => Array
            (
                [0] => 4
                [1] => 4
                [2] => 4
                [3] => 4
                [4] => 4
            )

        [size] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 0
            )

    )

)[/code]

Yet you are accessing the $_FILES as each one “img1”, “img2” at the start of the code and in:

[php]while($_FILES[“img”.$imgNumb][tmp_name]){[/php]

Further to this, you are moving all of the images to the /tmp/ directory when they are being uploaded. They need to be moved to a web-viewable directory (e.g. the one that you set in $DestinationDir). Note that your imgResize function doesn’t actually use the $Destination variable so the image won’t have been uploaded to the correct place.

I’ve attempted to correct it. See if this works:

[php]<?php

if(!isset($_POST['email']) || !isset($_POST['desc']) || 
!isset($_POST['quote']) || !isset($_POST['skidprice']) || 
!isset($_POST['item']) || !isset($_POST['unitprice']) || 
!isset($_POST['inventory']) || !isset($_POST['totalprice']) || 
!isset($_POST['category']) || !isset($_FILES['img']) || 
!isset($_POST['manu']) || !isset($_POST['model']) ||
!isset($_POST['condition']) || !isset($_POST['dimension'])) {
	die('ERROR!');
}

$email = $_POST['email'];
$quote = $_POST['quote'];
$item = $_POST['item'];
$inventory = $_POST['inventory'];
$category = $_POST['category'];
$manu = $_POST['manu'];
$model = $_POST['model'];
$condition = $_POST['condition'];
$dimension = $_POST['dimension'];
$desc = $_POST['desc'];
$skidprice = $_POST['skidprice'];
$unitprice = $_POST['unitprice'];
$totalprice = $_POST['totalprice'];

/*$img1 = $_FILES['img1'];
$img2 = $_FILES['img2'];
$img3 = $_FILES['img3'];
$img4 = $_FILES['img4'];
$img5 = $_FILES['img5'];*/

/* Cleaned this up a little */
function imgReisize($uploadedfile, $Thumb) { // I removed $Destination as it was never used!
	$src = imagecreatefromjpeg($uploadedfile);
	list($width, $height) = getimagesize($uploadedfile);

	$newheight = 150;
	$newwidth = ($width / $height) * 150;
	$tmp = imagecreatetruecolor($newwidth, $newheight);

	imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	$filename = $Thumb;
	imagejpeg($tmp, $filename, 100);

	imagedestroy($src);
	imagedestroy($tmp);

	echo "Successfully Uploaded: <img src='".$filename."'>";
}

/*
	Here we're making an array ready to display
	the images inside of the e-mail
*/
$image_src = array(
	'1' => 'http://some-site.com/NOT_FOUND_IMAGE.jpg',
	'2' => 'http://some-site.com/NOT_FOUND_IMAGE.jpg',
	'3' => 'http://some-site.com/NOT_FOUND_IMAGE.jpg',
	'4' => 'http://some-site.com/NOT_FOUND_IMAGE.jpg',
	'5' => 'http://some-site.com/NOT_FOUND_IMAGE.jpg'
);

foreach($_FILES["img"]["error"] as $key => $error) {
	$tmp_name = $_FILES["img"]["tmp_name"][$key];
	
	if(!$tmp_name) {
		continue;
	}

	$name = basename($_FILES["img"]["name"][$key]);

	if($error == UPLOAD_ERR_OK) {
		/*
			
			At this point we know the file was upload OK
			yet you had already resized the images, I moved
			the resize code here
			
		*/
		$DestinationDir = "quote/images/";
		$ThumbDir = "quote/images/thumbs/";

		// Resize code
		$Unique = microtime(); // We want unique names, right?
		$destination = $DestinationDir . md5($Unique) . ".jpg";
		$thumb = $ThumbDir . md5($Unique) . ".jpg";
		imgReisize($tmp_name, $thumb); // Fixed the array reference here
		
		// Original code
		if(move_uploaded_file($tmp_name, $destination)) { // I changed /tmp/ to the right directory
			
			// Adding the src to the array
			$image_src[$key] = 'http://your-website.com/' . $destination;
			
			$uploaded_array[] .= "Uploaded file '".$name."'.<br/>\n";
		} else {
			$errormsg .= "Could not move uploaded file '".$tmp_name."' to '".$name."'<br/>\n";
		}
	} else {
		$errormsg .= "Upload error. [".$error."] on file '".$name."'<br/>\n";
	}
}

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: All Food Equipment<[email protected]>' . "\r\n";

$headers .= "Message-ID: <".$now." sales@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

$subject = "Your All Food Equip Quote";

$msg = 
		"	
		<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">

Your All Food Equipment Quote:


Quote #:......................................................... $quote
Item:.............................................................. $item
Inventory:....................................................... $inventory
Category:....................................................... $category
Manufacturer:................................................. $manu
Model:........................................................... $model
Condition:...................................................... $condition
Dimension:.................................................... $dimension
Description:................................................... $desc
Skid Price:................................................. $skidprice
Unit Price:................................................. $unitprice
Total Price:................................................ $totalprice
\"Img\"\"Img\"\"Img\"\"Img\"\"Img\"

How to contact us:

  • Visit us Online: www.allfoodequip.com
  • Contact: Dino Roberts
  • Direct Line: 615-788-2953
  • Email: [email protected]
  • All Food Equipment
  • 1240 Industrial Park Road
  • Columbia, TN 38401
  • Main Office: 931-490-1977

Copyright © 2010 - All Food Equipment
";
// MAIL SUBJECT
 $subject = "Your All Food Equip Quote";
// TO MAIL ADDRESS
$to="$email";

mail($to, $subject, $msg, $headers);

// Redirect
header(“Location: Admin.php?Msg=Sent”);

?>[/php]

I truley appreciate you taking the time to do this for me, Here is what I am getting now.

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'quote/images/thumbs/3f73835c1b72b6c7fa73216447cbefa2.jpg' for writing: Permission denied in /home/allfoode/public_html/admin/go.php on line 45

I see the place holders for the images in the email, but no images.

Are the directory permissions set correctly?

This code here. should it be a path to the image because the images are going to be changed per quote depending on the product.

[php]$image_src = array(
‘1’ => ‘http://some-site.com/NOT_FOUND_IMAGE.jpg’,
‘2’ => ‘http://some-site.com/NOT_FOUND_IMAGE.jpg’,
‘3’ => ‘http://some-site.com/NOT_FOUND_IMAGE.jpg’,
‘4’ => ‘http://some-site.com/NOT_FOUND_IMAGE.jpg’,
‘5’ => ‘http://some-site.com/NOT_FOUND_IMAGE.jpg
);[/php]

Yes and I see the 3 images I tried to attach on the server,

Sponsor our Newsletter | Privacy Policy | Terms of Service