0

So I have searched through a lot of questions similar to this but cannot seem to find a solution.

I have a contact form and I want it to be able to send multiple images. The code I have now has three input fields for pictures but right now I have only figured out how to get one of the pictures to upload and send. I am pretty sure I need some kind of loop or array but that is not a strong point of mine.

HTML

        <form action="test.php" method="post" autocomplete="on" enctype="multipart/form-data" accept-charset='UTF-8'>
        <fieldset><label for="Name">Name <sup>*</sup></label>
        <input name="name" placeholder="full name" id="Name" required="" autofocus><br>

        <label for="email">E-mail <sup>*</sup></label>
        <input type="email" name="email" placeholder="youremail@domain.com" id="email" required=""><br>

        <label for="phoneNumber">Phone # <sup>*</sup></label>
        <input type="tel" name="phone" placeholder="XXX-XXX-XXXX" id="phoneNumber" required=""><br>
        </fieldset>
        <fieldset>
        <label for="year">Year <sup>*</sup></label>
        <input type="text" name="year" placeholder="year of car" id="year" required=""><br>

        <label for="make">Make <sup>*</sup></label>
        <input type="text" name="make" placeholder="make of car" id="make" required=""><br>

        <label for="model">Model <sup>*</sup></label>
        <input type="text" name="model" placeholder="model of car" id="model" required=""><br>
        </fieldset>
        <label for="State" class="state">State <sup>*</sup></label><br>
        <input type="text" class="state" name="state" id="state" placeholder="Do not enter if you are human">

        <fieldset>
        <legend>Add Photos <sup>*</sup></legend>
        <input type="file" name="attachment" required=""><br>
        <input type="file" name="attachment"><br>
        <input type="file" name="attachment"><br>
        </fieldset>

        <label for="comments">Comments</label><br>
        <textarea  style="float: left;" rows="4" name="comment" id="comments" placeholder="additional comments"></textarea>
        <br>

        <input class="button" type="submit" name="submit" value="Send" class="button">

        <p class="required"><sup>*</sup> denotes a required field.</p>

    </form>

PHP

<?php

$to = '';
$name = $_POST['name'];
$email  =   $_POST ['email'];
$phone = $_POST['phone'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$comment = $_POST['comment'];
$state = $_POST['state'];
$message = "

Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment
";

if($_POST['state'] != ''){
echo "It appears you are a bot!";
exit();
}
else{
//process the rest of the form
}

/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];

/* Start of headers */
$headers = "From: $name $email";

if (file($tmpName)) {
/* Reading file ('rb' = read binary)  */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);

/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";

/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";

/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

/* Encoding file data */
$data = chunk_split(base64_encode($data));

/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}


$flgchk = mail ("$to", "$subject", "$message", "$headers");

if($flgchk){
echo "A email has been sent. We will get back to you as soon as
possible.";
}
else{
echo "Error in Email sending";
}
?>
tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

1

All three of your fields are named "attachment". Give them unique names like "attachment1", "attachment2", etc.

Then use this on your upload:

for ($x = 1; $x <= $numberOfFiles; ++$x) {
    $tmpName = $_FILES['attachment'.$x]['tmp_name']; 
    $fileType = $_FILES['attachment'.$x]['type']; 
    $fileName = $_FILES['attachment'.$x]['name'];
    // do your upload here
}

You could even do it in a loop like this, which would allow you to add more file fields (maybe with JavaScript) without changing the backend code, as long as you kept the naming pattern consistent.

$x = 1;
while (isset($_FILES['attachment'.$x])) {
        $tmpName = $_FILES['attachment'.$x]['tmp_name']; 
        $fileType = $_FILES['attachment'.$x]['type']; 
        $fileName = $_FILES['attachment'.$x]['name'];
        // do your upload here
        ++$x;
}

That just increments x on each cycle, and checks to see if a file was sent with that name.

You should probably also include some checking on the size and type of files, before someone fills your inbox with huge files and/or cyber-nasties.


UPDATE: This should be a mostly-complete solution for you. I'm not able to test it for you at the moment so it may not work straight out of the box, but it should at least point you in the right direction. In this code snippet it sets up the initial part of the email first, then loops through and adds each file one at a time.

<?php
$to = '';
$name = $_POST['name'];
$email  = $_POST ['email'];
$phone = $_POST['phone']; 
$year = $_POST['year']; 
$make = $_POST['make']; 
$model = $_POST['model']; 
$comment = $_POST['comment']; 
$state = $_POST['state']; 
$message = "Name: $name
E-mail: $email
Phone: $phone
Year: $year
Make: $make
Model: $model
Message: $comment";

if($_POST['state'] != ''){
    echo "It appears you are a bot!";
    exit();
}

/* Start of headers */ 
$headers = "From: $name $email"; 

/* a boundary string */
$randomVal = md5(time()); 
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n"; 
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\""; 

/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" . 
"--{$mimeBoundary}\n" . 
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
"Content-Transfer-Encoding: 7bit\n\n" . 
$message . "\n\n";

/* Loop through files */
$x = 1;
while (isset($_FILES['attachment'.$x])) {
    /* GET File Variables */ 
    $tmpName = $_FILES['attachment']['tmp_name']; 
    $fileType = $_FILES['attachment']['type']; 
    $fileName = $_FILES['attachment']['name']; 

    /* Skip invalid files */
    if (!file($tmpName)) {
        ++$x;
        continue;
    }

    /* Reading file ('rb' = read binary)  */
    $file = fopen($tmpName,'rb'); 
    $data = fread($file,filesize($tmpName)); 
    fclose($file); 

    /* Encoding file data */
    $data = chunk_split(base64_encode($data)); 

    /* Adding attchment-file to message*/
    $message .= "--{$mimeBoundary}\n" . 
    "Content-Type: {$fileType};\n" . 
    " name=\"{$fileName}\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . 
    $data . "\n\n";

    ++$x;
}

/* Close the message */
$message .= "--{$mimeBoundary}--\n";

$flgchk = mail ("$to", "$subject", "$message", "$headers"); 

if($flgchk){
    echo "A email has been sent. We will get back to you as soon as possible.";
} else {
    echo "Error in Email sending";
}
?>
Mark Ormesher
  • 2,289
  • 3
  • 27
  • 35
  • Thanks for responding! I thought this made sense to me but now my message isn't even sending. I would just put this code right before my upload correct? – user3746432 Jun 23 '14 at 18:29
  • @user3746432 - Close, but not quite. In each of my code samples, you need to replace the line `// do your upload here` with your process of reading the file and then adding it as a part of the email. Let me know if that makes sense; if not, I can update my question with some more code for you. – Mark Ormesher Jun 23 '14 at 18:40
  • @user3746432 - I've posted a near-complete solution for you. It's untested, but it should at least point you in the right direction :) – Mark Ormesher Jun 23 '14 at 18:49
  • Thank you! This definitely points me in the right direction. I think I can get it from here. – user3746432 Jun 24 '14 at 02:29
  • Great stuff! Let me know if there were any bugs - I can correct them in the answer so future users can get a complete solution. – Mark Ormesher Jun 24 '14 at 10:21