Attaching image to email from form

Hi!
We have a form that’s been used for years, and now we would like to have the added capability of attaching an image. (So a consumer can send us an image that might help with their problem.) Currently, a “confirmation” email is sent to the consumer with all of the info they gave us; a copy of the same information also goes to us. We would like the consumer’s image to be saved onto our server, so when we look at the completed form, the “Image:” line has a link to that page.
I.e. a consumer attaches an image named “picture.jpg”. The copy of the information we get would have a line that reads: “Image: http://www.example.com/test/uploads/picture.jpg”. I’m assuming I would use a variable to get the image name to display in order to complete the link. I’m lost about which variable I would use… but that might be because I can’t get the image to even save in the folder (I think) it’s supposed to. Obviously there’s something messed up, but I am lost as to what that is. Any help would be greatly appreciated!

The field to attach the image on the form:
[php]If you have an image that would help us assist you, please attach it here.
[/php]

The process file:
[php]<?php
//Collect contact form data
//Check Special Field
//Email ASC & Webmaster
//Email Sender
//Redirect to thank you page

require_once($_SERVER['DOCUMENT_ROOT'].'/includes/functions.php');


/********  CONTACT DATA **********/
$name = stripslashes($_POST['name']);
$company = stripslashes($_POST['company']);
$address = stripslashes($_POST['address']);
$city = stripslashes($_POST['city']);
$state = stripslashes($_POST['state']);
$zipcode = stripslashes($_POST['zipcode']);
$country = stripslashes($_POST['country']);
$website = $_POST['website'];
$phone = stripslashes($_POST['phone']);
$fax = stripslashes($_POST['fax']);
$email = stripslashes($_POST['contact']);
$Referred = stripslashes($_POST['referred']);
$CustomerType = stripslashes($_POST['CustomerType']);
$Comments = stripslashes($_POST['comments']);
$ConsumerHelp = stripslashes($_POST['ConsumerHelp']);
$UPC = stripslashes($_POST['UPC']);
$Describe = stripslashes($_POST['Describe']);
$uploaded_file = ($_FILES['uploaded_file']);
/********  CHECK SPECIAL FIELD **********/
$spamcheck = stripslashes($_POST['email']);


//if spamcheck isnt blank exit page, no need for error message to user, as its a spam bot
if ($spamcheck!=="") {

    exit;

}

    
    /********  EMAIL ASC & WEBMASTER  **********/
    $message = "
-----------------------------------------------------------------------------
   Information Inquiry
-----------------------------------------------------------------------------

$name has visited the web site and would like some information.
The details they entered on the website are:

Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email

Referred to web site: $Referred

CustomerType: $CustomerType

Comments: $Comments

I need help with: $ConsumerHelp

UPC code or Item #: $UPC

What I am looking for: $Describe

Image: $uploaded_file

Kind Regards,

";
    $email_address = "example@example";

    $subject = "Information Inquiry";
    $headers = "From: $name <$email>";
    $message = str_replace("\r",'',$message); //fixes postfix php bug that double spaces messages





    /********  EMAIL SENDER **********/
    $message2 = "
-----------------------------------------------------------------------------
  Re: Information Inquiry
-----------------------------------------------------------------------------

Thank you $name for visiting the web site. We will be using the details you entered to contact you.

Name: $name
Company: $company
Address: $address
City: $city
State: $state
Zip Code: $zipcode
Country: $country
Website: $website
Phone: $phone
Fax: $fax
Email: $email

Referred to web site: $Referred

CustomerType: $CustomerType

Comments: $Comments

I need help with: $ConsumerHelp

UPC code or Item #: $UPC

What I am looking for: $Describe


Kind Regards,

";
    $email_address2 = "$email";
    $subject2 = "Re: Information Inquiry";
    $headers2 = "From:  <[email protected]>";
    $message2 = str_replace("\r",'',$message2); //fixes postfix php bug that double spaces messages

//send message 1 and save result in success var (either true for success, or false for fail
$success = mail($email_address, $subject, $message, $headers);

//conditionally send message2, no need to check success on this one
if (strpos($email,'@aol.com') == false) {
    mail($email_address2, $subject2, $message2, $headers2);
}

if (!$success) {
// What happens when the form does not validate
header("Location: sorry.php");
die ();
} else {
  // Your code here to handle a successful verification
  header("Location: thanks.php");
    $success;

}

?>

<?php $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["uploaded_file"]["name"]); $extension = end($temp); if ((($_FILES["uploaded_file"]["type"] == "image/gif") || ($_FILES["uploaded_file"]["type"] == "image/jpeg") || ($_FILES["uploaded_file"]["type"] == "image/jpg") || ($_FILES["uploaded_file"]["type"] == "image/pjpeg") || ($_FILES["uploaded_file"]["type"] == "image/x-png") || ($_FILES["uploaded_file"]["type"] == "image/png")) && ($_FILES["uploaded_file"]["size"] < 20000) && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["uploaded_file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["uploaded_file"]["name"] . "
"; echo "Type: " . $_FILES["uploaded_file"]["type"] . "
"; echo "Size: " . ($_FILES["uploaded_file"]["size"] / 1024) . " kB
"; echo "Temp file: " . $_FILES["uploaded_file"]["tmp_name"] . "
"; if (file_exists("upload/" . $_FILES["uploaded_file"]["name"])) { echo $_FILES["uploaded_file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "upload/" . $_FILES["uploaded_file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> [/php]

Well, there is a lot of explanations on this all over the web. I will attempt to explain how I do it.

First, you never store a picture inside a database. It takes too much overhead to store and retrieve it.
You have the user upload the picture which is uploaded to the server’s temporary folder. Then, you move
it somewhere. Normally, you need a way to tell it apart from other user’s pictures. Many many ways to
do this. The easiest is to add their user ID from the database in the name of the picture. Or, a lot of sites
use a separate folder for each user. This decision would depend on how many users and pictures you
might end up with. A lot of pictures from one user, they should have their own folder. Occasional pictures
from various users, just one picture folder with a unique ID in them.

So, you can do this when you move the temporary file to the storage folder. Just add their user-ID in
front of the name for the picture. In this way, the pictures might be named, sample1.jpg / my-error.jpg
and they would become 99-sample1.jpg / 13-my-error.jpg… Get the idea? So, the way I do it is add
the user’s ID with a dash. Easy to know who the owner is. Just have to grab the ID and look it up in
the user-table.

So, for emails… Since you should also store the new name of the picture inside the database for this
user’s email message, you have it’s name stored and can pull it out easily… Since it is just a picture on
the server, you can link to it link any other picture. Or, you can attach it as needed.

Links back to pictures are just hyperlinks (HREF’s) to the picture. Of course this would be in the form
of “www.my-domain.com/images/email-messages/99-sample1.jpg” or something like that depending
on your sites layout and where you save the picture. Also, note that when you place a link into an email
or post on your site, you have two parts. The first is what the reader sees and the underneath part which
is the actual address. So, you can have the email and site post say “view your picture here” and under
that is the link to the picture. They only see the text part but when they click on it, it shows the picture.
Hope that makes sense…

Now a couple of items to think about while you are designing this. First is to make sure you think out how
the files are named. The reason is clear. If two different users send in pictures and they are named the
same, havoc will appear. One or the other user would get responses of the wrong picture. Also, if you
save the same named picture into one folder such as “/upload/” in your sample, the second will overwrite
the first. That should be clear.

Also, if you send out emails with the picture in it, the easiest way is to just send a link to the live pix in the
server’s folder. You could “attach” the picture, but, that slows down the server, takes longer for the user
to load the email and can be corrupted along the way. Much easier to just use a link. And, if the user or
your admin people want to save the picture for using, they can right-click on the viewed picture and save
it as needed.

Hope all this helps… If you have further issues solving it, let us know… Good luck

Wow! I honestly forgot I had posted on this forum! Since my original post on 4/29, I have figured out how to make it work! Thank you for your extensive help though!!
I used the code below in the process file.
[php]// there is a file
$allowedExts = array(“gif”, “jpeg”, “jpg”, “png”);
$temp = explode(".", $_FILES[“uploaded_file”][“name”]);
$extension = end($temp);
$userImage = $FILES[“uploaded_file”][“name”];
$location = str_replace(’ ', '
’, $location);
if ((($_FILES[“uploaded_file”][“type”] == “image/gif”)
|| ($_FILES[“uploaded_file”][“type”] == “image/jpeg”)
|| ($_FILES[“uploaded_file”][“type”] == “image/jpg”))
&& ($_FILES[“uploaded_file”][“size”] < 2300000 )
&& in_array($extension, $allowedExts)) {
if ($_FILES[“file”][“error”] > 0) {
echo "Return Code: " . $_FILES[“uploaded_file”][“error”] . “
”;
} else {
header(“Location: thanks.php”);

            if (file_exists("uploads/" . $_FILES["uploaded_file"]["name"])) {
              echo $_FILES["uploaded_file"]["name"] . " already exists. ";
            } else {
                
             $location = "uploads/" . $temp[0].".".time().".".$extension;
             $location = str_replace(' ', '_', $location);
             move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $location);
             echo "Stored in: " . $location;
            }
          }[/php]

In the message portion of the process form I added the code below to allow us to view the image the user attached.
[php]Image: http://www.example.com/contact/$location[/php]
Since a slightly different email is sent to the user, they don’t need to see where we have the image at… they can just see what the image was called. Their message uses the code below:
[php]Image: $userImage[/php]

Great! I was just cleaning up unanswered posts… Glad to hear you solved it.

I will mark this one solved. Have a nice day…

Sponsor our Newsletter | Privacy Policy | Terms of Service