Ricky55 Posted March 7, 2014 Share Posted March 7, 2014 Hi guys I've cobbled together this script to process a form with a file attachment. It works fine but it doesn't have any validation for the form or the file which isn't great and I'd like to add, currently I'm just using Javascript validation. Before I move on to the validation, can you see why the form processes twice? I get one email with the processed info and one or two blank. My knowledge of PHP is beginner at best. My code. Thanks in advance. <?php $recipient = "richard@domain.com"; $subject = "Job Application"; $message .= "Name: {$_POST['name']} \n\n"; $message .= "Date of Birth: {$_POST['dob']} \n\n"; $message .= "Telephone: {$_POST['telephone']} \n\n"; $message .= "Email: {$_POST['email']} \n\n"; $message .= "Home Address: {$_POST['address']} \n\n"; $message .= "The Question Section\n\n"; $message .= "Showroom answer: {$_POST['showroom']} \n\n"; $message .= "Latex cushions answer: {$_POST['latex']} \n\n"; $message .= "Window seat answer: {$_POST['cushion']} \n\n"; $message .= "Directions answer: {$_POST['directions']} \n\n"; $message .= "Competitors answer: {$_POST['competitors']} \n\n"; $message .= "Benefits answer: {$_POST['benefit']} \n\n"; $headers .= "From: {$_POST['email']}"; // GET FILE VARIABLES $tmpName = $_FILES['attachment']['tmp_name']; $fileType = $_FILES['attachment']['type']; $fileName = $_FILES['attachment']['name']; if (file($tmpName)) { // READING IN FILE 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 ATTACHMENT 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 ($recipient, $subject, $message, $headers); if($flgchk) { echo ('<p style="font-family: sans-serif; font-size: 1.2em; padding-top: 2em; text-align: center;">Thanks your application was sent successfully. You can now close this window.</p>'); } else { echo ('<p style="font-family: sans-serif; font-size: 1.2em; padding-top: 2em; text-align: center;">Sorry but there was an error sending your application, try emailing andrew@foamforcomfort.co.uk instead.</p>'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/286792-send-mail-form-sending-twice/ Share on other sites More sharing options...
jairathnem Posted March 7, 2014 Share Posted March 7, 2014 From which page is data POST'ed to this page called? You will have to check there to see if it is calling this page with blank data. Quote Link to comment https://forums.phpfreaks.com/topic/286792-send-mail-form-sending-twice/#findComment-1471741 Share on other sites More sharing options...
Ricky55 Posted March 10, 2014 Author Share Posted March 10, 2014 OK I'll have a look thanks Quote Link to comment https://forums.phpfreaks.com/topic/286792-send-mail-form-sending-twice/#findComment-1471977 Share on other sites More sharing options...
ginerjm Posted March 10, 2014 Share Posted March 10, 2014 I see nothing in this code snippet that would cause a second transmission of your mail message. I do however see at least one php syntax error which if you turned on error checking would be revealed to you. ini_set('display_errors', '1'); ini_set('log_errors','1'); // turn this off once done with development. Quote Link to comment https://forums.phpfreaks.com/topic/286792-send-mail-form-sending-twice/#findComment-1472015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.