0

I can't seem to figure out why my contact form is not working properly. Submitted info such as phone #, name, and email is coming through just fine but Attachments are not coming through. Can you please help! Thank you so much in advance!

HTML CODE

<form id="contact_form" action="mail_handler.php" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-md-12">
<input class="bg-gray text-dark" type="text" required name="name" placeholder="Full name">
</div>
<div class="col-md-12">
<input class="bg-gray text-dark" type="tel" required name="phone" placeholder="Mobile number" >
</div>
<div class="col-md-12">
<input class="bg-gray text-dark" type="email" required name="email" placeholder="Email address" >
</div>
<div class="col-md-12">
<div class="upload-btn-wrapper">
<button class="btn-attach " type="file" required name="attachment" accept="*" >UPLOAD YOUR RESUME</button>
<input type="file" name="myfile" />
</div>
</div>
<div class="col-md-12 text-md-right">
<input class="btn btn-solid round btn-bordered border-thin btn-block" style="background-color: black; border-color: black; color:#f9c313; font-size: 16px; font-style: bold;" type="submit" value="Submit Now">
</div>
</div>
</form>

JQUERY

<script>
jQuery(document).ready(function($){
    $("form#contact_form").submit(function(e) { 
    e.preventDefault();    
    var formData = new FormData(this);

    $.ajax({
        url: '/mail_handler.php',
        type: 'POST',
        data: formData,
        success: function (data) {
            alert(data);
            $("form#contact_form")[0].reset();
        },
        cache: false,
        contentType: false,
        processData: false
    });
});
});

</script>

PHP

<?php


$to2 = 'name@domain.com'; 
$to = 'name@domain.com';
$from = 'donotreply@domain.com'; 
$fromName = 'My Contact Form'; 

$form_name = isset($_POST['name'])?$_POST['name']:'';
$form_email = isset($_POST['email'])?$_POST['email']:'';
$form_phone = isset($_POST['phone'])?$_POST['phone']:'';


$subject = "Contact Form on domain.com"; 

$htmlContent = " 
    <html> 
    <head> 
        <title>Contact Form on domain.com</title> 
    </head> 
    <body> 
        <table cellspacing='0' style='border: 1px solid gray; width: 100%;'> 
            <tr> 
                <th>Name:</th><td>".$form_name."</td> 
            </tr> 
            <tr style='background-color: #e0e0e0;'> 
                <th>Email:</th><td>".$form_email."</td> 
            </tr> 
            <tr> 
                <th>Phone:</th><td>".$form_phone."</td> 
            </tr> 
        </table> 
    </body> 
    </html>"; 



$uploaddir = dirname(__FILE__).'/tmpfiles/';
$uploadfile = $uploaddir . basename($_FILES['attachment']['name']);

if (move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadfile)) {
   $file = $uploadfile;
} else {
    $file = null;
}

$headers = "From: $fromName"." <".$from.">"; 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
$message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 
if(!empty($file) > 0){
    if(is_file($file)){
        $message .= "--{$mime_boundary}\n";
        $fp =    @fopen($file,"rb");
        $data =  @fread($fp,filesize($file));

        @fclose($fp);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: application/octet-stream; name=\"".basename($file)."\"\n" . 
        "Content-Description: ".basename($file)."\n" .
        "Content-Disposition: attachment;\n" . " filename=\"".basename($file)."\"; size=".filesize($file).";\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    }
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$mail = @mail($to2, $subject, $message, $headers, $returnpath); 
$mail = @mail($to, $subject, $message, $headers, $returnpath); 
echo $mail?"Thank you for your application":"Email sending failed. Please try again";


Again, thank you so much for your review and help. I really appreciate it.

chrish83
  • 3
  • 3
  • 1
    You need to manually append the file(s) to the formdata, see this for an example https://stackoverflow.com/questions/23980733/jquery-ajax-file-upload-php – RiggsFolly Mar 28 '20 at 17:56
  • This would be trivial in raw PHP because `mail_handler.php` will get attachments in the `$_FILES` variable for free. The extra complexity of manually adding file fields is the price to pay for writing your PHP apps in JavaScript ;-) – Álvaro González Mar 28 '20 at 18:50
  • @ÁlvaroGonzález unfortunately I'm not a developer and have zero clue how to do this. I was hoping someone would post the correction that I could copy and paste... – chrish83 Mar 28 '20 at 19:28
  • Sorry about that but Stack Overflow aims to be a collaborative knowledge base rather than a code writing service. Whatever, I suspect that everything your jQuery code accomplishes is to prevent the form from working and removing it altogether would probably fix it. – Álvaro González Mar 29 '20 at 09:50
  • Ohh I see. Thank you for your responses. – chrish83 Mar 29 '20 at 15:06

0 Answers0