-1

I have a basic working PHP contact form that uses a static constant image as captcha - I don't know much PHP but want to be able to add an upload/send attachment section, so the user has a choice of being able to attach a file which will be attached to the email the recipient receives. I have no idea how to do this and can find no help googling. Any ideas? I have simple working upload code - but can't seem to combine the two.

contact form HTML

<form name="contactform" method="post" action="contact_send.php">
        <div class="contactdetails">
           <label for="name"> Name or Company Name<span class="mandatory"> *</span> </label> <input type="text" name="name" />  <br /> 
           <label for="telephone_number"> Telephone Number<span class="mandatory"> *</span> </label>  <input type="text" name="telephonenumber" /> <br />
           <label for="email_address"> Email Address </label> <input type="text" name="emailaddress" /> <br /> 

            <div class="commentarea">
              <p><label for="comments"> Comments </label> </p>
              <textarea name="comments" rows="auto" cols="auto"> </textarea> <br />
            </div>               

           <div id="typecont"> 
            <label for="type_see">Please type what you see in the box</label>   
              <div id="typecontimg"> 
                <img src="images/num.png" alt="captcha" style="width:100%;"/> <!-- style="width:65px;height:28px" -->
               </div>        
                  <div id="typein">
                   <input type="text" id="type_see" name="typesee"/>     
                  </div>
           </div>

               <div class="submitbutton">
                 <input type="submit" value="Send" name="submit" />
               </div>                 
        </div> 

AND THE PHP

<?php
        if(isset($_POST['submit'])) {

            $name = $_POST['name']; // required
            $telephone_number = $_POST['telephonenumber']; // required
            $email_address = $_POST['emailaddress'];                                        
            $comments = $_POST['comments'];                                                        
            $type_see = $_POST['typesee']; // required     

            if($name=='' || 
           $telephone_number=='' ||                                                                            
           $type_see != 8567 )    

            {  
                   die ('<p style="margin-top:40px;">You have not filled in all of the required fields or the captcha is wrong  - <br /> <br /> please go back and try again!</p>');  

           }

            else{
                $email_to = "info@companyname.com";
                $email_subject = "Contact from Company Name";
                $email_message = "Form details below.\n\n";

            function clean_string($string) {
              $bad = array("content-type","bcc:","to:","cc:","href");
              return str_replace($bad,"",$string);
            }

            $email_message .= "Name or Company Name: ".clean_string($name)."\n";
            $email_message .= "Telephone Number: ".clean_string($telephone_number)."\n";                
            $email_message .= "Email Address: ".clean_string($email_address)."\n";
            $email_message .= "Comments: ".clean_string($comments)."\n";


            // Create email headers
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "From: info@companyname.com" . "\r\n";
            $headers .= "Reply-To: info@companyname.com" . "\r\n";
            $headers .= "Content-type:text;charset=iso-8859-1" . "\r\n";
            mail($email_to, $email_subject, $email_message, $headers);                 
        }
        }

?>

And I have this as a simple upload/send form:

HTML (relevant part)

    <div class="files">
<label>Attachment <input type="file" name="my_file" /></label>
</div>

And PHP for the upload:

<?php   

if($_POST && isset($_FILES['my_file'])) {

$from_email         = 'inf@services.com'; //from mail, it is mandatory with some hosts
$recipient_email    = 'inf@services.com'; //recipient email (most cases it is your personal email)
$subject      = "Upload from company";

//Capture POST data from HTML form and Sanitize them, 
$sender_name    = filter_var($_POST["sender_name"], FILTER_SANITIZE_STRING); //sender name
$reply_to_email = filter_var($_POST["sender_email"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header

/* //don't forget to validate empty fields 
if(strlen($sender_name)<1){
    die('Name is too short or empty!');
} 
*/

//Get uploaded file data
$file_tmp_name    = $_FILES['my_file']['tmp_name'];
$file_name        = $_FILES['my_file']['name'];
$file_size        = $_FILES['my_file']['size'];
$file_type        = $_FILES['my_file']['type'];
$file_error       = $_FILES['my_file']['error'];

if($file_error > 0)
{
    die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Upload error or no files uploaded. <br /> Please hit your back button and try again.</p>');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));

    $boundary = md5("sanwebe");
    //header
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "From:".$from_email."\r\n"; 
    $headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

    //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($message)); 

    //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; 

$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{       
    die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Thank you for uploading your CV. <br /> We will be in touch shortly.</p>');
}else{
    die('<p style="text-align:center; padding:20px; background: #d6de34; width: 500px; margin: 30px auto 200px auto;">Could not send mail! Please check your PHP mail configuration</p>');  
}

} ?>

1 Answers1

-1

You can use an <input type="file" name="fl">, or name it however you want and handle the file using: $name = $_FILE['fl'].

$_FILE[] is an associative array, so, to see its contents, use print_r($name);

You can see details like its temporary location, and use a function like move_uploaded_file() to upload the file in a permanent folder on the server.

mmateas
  • 50
  • 8
  • Why has this question been closed and why are comments from users being removed? I am trying to resolve an issue I have for a client - by using this help forum! – Fizz Web Design May 12 '20 at 17:18
  • @FizzWebDesign I don't know. But did my answer help you? – mmateas May 12 '20 at 17:24
  • Sorry, was a general comment - not directed to you. Not as such I doubt, as I code in HTML/CSS and can tweak PHP but don't know too much about it. I recognise the field formats and will play around with it tomorrow. Thanks – Fizz Web Design May 12 '20 at 17:38
  • @FizzWebDesign You're welcome! If you found my answer helpful, you can mark it as an answer. If you need more details regarding it, just feel free to comment. Have fun! – mmateas May 12 '20 at 19:20