1

I have a form which works from my PHP document & which delivers the email perfectly: all information is sent correctly except for the attachment field, where it just ignores the uploaded file from my form.

I am expecting the file from which is uploaded into my form to be sent as an attachment via email - along with the rest of the information - the actual result is that all information is present and correct except for any sign of a document/file attached.

Here is the code. What am I missing here?

Thanks

<?php
if($_POST['button'] && isset($_FILES['attachment'])){
    
    $department = $_POST['department'];   
    $title = $_POST['title'];
   $first = $_POST['first'];
   $surname = $_POST['surname'];
   $email = $_POST['email'];
    $number = $_POST['number'];
    $userMessage = $_POST['userMessage'];
    
   //Get uploaded file data using $_FILES array 
    $tmp_name    = $_FILES['my_file']['tmp_name']; // get the temporary file name of the file on the server 
    $name        = $_FILES['my_file']['name'];  // get the name of the file 
    $size        = $_FILES['my_file']['size'];  // get size of the file for size validation 
    $type        = $_FILES['my_file']['type'];  // get type of the file 
    $error       = $_FILES['my_file']['error']; // get the error (if any) 
    
    //read from the uploaded file & base64_encode content 
    $handle = fopen($tmp_name, "r");  // set the file handle only for reading the file 
    $content = fread($handle, $size); // reading the file 
    fclose($handle);                  // close upon completion 
  
    $encoded_content = chunk_split(base64_encode($content)); 
  
    $boundary = md5("random"); // define boundary with a md5 hashed value 
  
    //header 
    $headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version 
    $headers .= "Reply-To: ".$email_from."\r\n"; // Email addrress to reach back 
    $headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type 
    $headers .= "boundary = $boundary\r\n"; //Defining the Boundary 
          
    //plain text  
    $body = "--$boundary\r\n"; 
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n"; 
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";  
    $body .= chunk_split(base64_encode($userMessage));  
          
    //attachment 
    $body .= "--$boundary\r\n"; 
    $body .="Content-Type: $file_type; name=".$file_name."\r\n"; 
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
    $body .="Content-Transfer-Encoding: base64\r\n"; 
    $body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";  
    $body .= $encoded_content; // Attaching the encoded file with email 

   $email_from = 'xxxxxxxxxxxxxxxxxxxx';

   $Password = 'xxxxxxxxxxxx';

   $email_subject = "New Email Form Submission from $first.";

   $email_body = 
        "User title: $title. \n".
      "The department this message is for is: $department.\n".
      "User First Name: $first.\n".
        "User Surname Name: $surname. \n".
      "User email: $email.\n".
      "User Contact Number: $number.\n".
        "User Message is: $userMessage.\n";
        
  $to = 'xxxxxxxxxxxx';

  $headers = "From: $email_from \r\n";

  $headers ="Reply to: $first $surname \r\n";

  mail($to,$email_subject,$email_body, $headers);
    
  header('Location: https://www.xxxxxxxxxxx.com/pages/thank-you.html');
}
?>
<form action="../php/contact-us-main.php" method="POST">
                         <label class="select">
                            <select name="department" required>
                                <option selected disabled>Who would you like to contact?</option>
                                <option>Fleet Services</option>
                                <option>Finance & Credit Control</option>
                                <option>Executive Team</option>
                                <option>Delivery Associates</option>
                                <option>Breakdown & Recovery</option>
                                <option>Invoice & Billing Queries</option>
                                <option>Compliance & Legal</option>
                                <option>Website Assistance & Feedback</option>
                                <option>IT Services</option>
                                <option>Complaints</option>
                            </select>
                        </label><br>
                        <label class="select">
                            <select name="title" required>
                                <option selected disabled>Choose your title</option>
                                <option>Mr</option>
                                <option>Mrs</option>
                                <option>Ms</option>
                                <option>Miss</option>
                                <option>Dr</option>
                                <option>Prof</option>
                                <option>Rev</option>
                                <option>Lord</option>
                                <option>Lady</option>
                            </select>
                        </label><br>
                        <input required name="first" class="select" type="text" placeholder="First name"/><br>
                        <input required name="surname" class="select" type="text" placeholder="Surname"/><br>
                        <input required name="email" class="select" type="email" placeholder="Email"/><br>
                        <input required name="number" class="select" type="number" maxlength="11" placeholder="Contact Number"/><br>
                        <div class="uploadFile">
                            <label onClick="../php/contact-us-file-upload.js" for="uploadBtn" class="uploadButtonLabel std_button" id="label_span">Want to send a file? Upload it here
                            </label>
                            <input type="file" name="attachment" id="uploadBtn" multiple = "false" />
            </div><br>
                        <textarea required name="userMessage" class="select" placeholder="Type your message...."></textarea><br>
                        <input type="submit" name="button" value="Send Message"/>
                    </form>
user3783243
  • 5,368
  • 5
  • 22
  • 41
Hunktydunkaty
  • 43
  • 1
  • 8

2 Answers2

0

add the enctype attribute in form

enctype = 'multipart/form-data'

enctype attribute specifies how the form-data should be encoded when submitting it to the server. multipart/form-data is one of the value of enctype attribute, which is used in form element that have a file upload. multi-part means form data divides into multiple parts and send to server.

Sher E Yr
  • 106
  • 6
-1

First of all, I will look in PHP configuration file php.ini in which you will find some useful informations about file uploads.

@see https://stackoverflow.com/a/2184541/390462

; Maximum allowed size for uploaded files.
upload_max_filesize = 20M

; Must be greater than or equal to upload_max_filesize
post_max_size = 20M

Don't forget to adjust max_execution_time, max_input_time, max_input_vars if you have a very big form with hundred fields or more. I don't recommand so much posted vars at a time, but sometimes you must deal with.

And <form ... enctype="multipart/form-data"> must be present in order to work. See the official documentation about php uploads at https://www.php.net/manual/fr/features.file-upload.post-method.php

BendaThierry.com
  • 2,080
  • 1
  • 15
  • 17
  • It depends all about the size of the file he is trying to send. And sometimes 40 MBs files are sent as posted files and not only as emails inside enterprises lan networks, with specific configurations. I know it is awful, but it exists. I will edit to 20MBs if you need to be fair with email clients ;) – BendaThierry.com Aug 30 '19 at 11:22
  • I am used to look everywhere, and since everythink start from configuration, I usually start at the beginning and go down : config webserver => config php => config code from libs => code to test. It works well, and forget no step. – BendaThierry.com Aug 30 '19 at 11:29
  • Because so many people never configure anything, just installing some stuff and running the stuff without looking the limits and they are expecting everything will work well. It is not Santa world. We need to know what we are doing and which boundaries we have to handle. – BendaThierry.com Aug 30 '19 at 12:10
  • If you need to have right, you are right. So much important things to do right now. – BendaThierry.com Aug 30 '19 at 13:42
  • Maybe doing things separetely will show your bad stuff. Think about some basic development principles. ISOLATION. And downvoting because of your errors is just insane... Quick and dirty is your work for my point of view to do so much things in a same process. First, download your file. Second, in another script, you can call from here, code the email part with a solid email library as PHPMailer or Swift are. Try to look at your logs, sometimes they could be usefull `/var/log/apache2/error.log` on debian9, and your virtualhosts logs too with the directive "ErrorLog". – BendaThierry.com Sep 08 '19 at 12:33
  • You can show all errors from your script with `error_reporting(E_ALL);` at the start of your php script. – BendaThierry.com Sep 08 '19 at 12:36
  • @user8709679 Look at the rights of your webuser to write in the uploaded file. Sometimes you can find a bug here when you are uploading some files. The webuser whom is running apache must have write right to do the job : `chown www-data:www-data -Rf /your-web-path/php/project/test/www/uploads/` is an example if you are uploading everything in these 'uploads' folder. – BendaThierry.com Sep 08 '19 at 12:38
  • Right, my errors, or my “bad stuff” aren’t the problem. The problem is the software. If I tell a piece of software to do something, it does just that. It’s not an I’ll-disciplined child, it’s software. Software is programmed to do what the user tells it do to. It does not tell me what to do, I tell it what do to. It either does as it’s told or the developers get a bolloking. Simple. Yes, I have anger issues but can you blame me when I’m dealing with pieces of shit all day? – Hunktydunkaty Sep 08 '19 at 16:27
  • Do better work following basic principles, and follow my advice about logs and displaying errors, and you will resolve your problem. I can not do your work. I don't want to do your work. I can not do your work. I can just help you to find the right way. – BendaThierry.com Sep 08 '19 at 21:04