Jump to content

Send mail form - sending twice


Ricky55

Recommended Posts

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>');
}
 
 ?>
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.