Jump to content

Email PHP script - creating duplicate emails.


jpopuk

Recommended Posts

Hi, I have recently been using a simple enquiry form with some pretty basic PHP.

 

The problem I am getting is when some one submits an enquiry it sends an email as it should but a couple of hours later it is sending a duplicate email. I have contacted my hosts and they say each email has a different ID so it must be within the script.

 

Below is the code, if anyone can spot what could be causing this I would be most grateful.

 

<?php

$my_email = "[email protected]";

$continue = "/";

$errors = array();

// Remove $_COOKIE elements from $_REQUEST.

if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

// Build message.

function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

$message = build_message($_REQUEST);

$message = stripslashes($message);

$subject = "General Enquiry";

$subject = stripslashes($subject);

$from_Name = "";

if(isset($_REQUEST['Name']) && !empty($_REQUEST['Name'])){$from_Name = stripslashes($_REQUEST['Name']);}

$headers = "From: {$from_Name} <{$_REQUEST['Email']}>";

mail($my_email,$subject,$message,$headers);

$message = "Thank you for your enquiry. We will be in touch with you as soon as possible.

Regards,

Website Name
http://www.domain.com";

$subject = "General Enquiry";

$headers = "From: Website Name <[email protected]>";

mail($_REQUEST['Email'],$subject,$message,$headers);

?>

 

Many thanks,

 

Paul

I couldn't see how that script is causing an email to be resent 4 hours later...unless what micah is saying is the case.  If not, then I would think it would be some sort of issue with your hosting provider and they may be just telling you its your script because they just want to assume that nothing could possibly be wrong with their system.

I am not 100% sure to be honest. I would imagine it to be $_GEt. I am fairly new to PHP and learning a lot pretty quick.

 

My host is Dreamhost which have been fantastic for as long as I've been using them.

 

Cheers for the help guys, much appreciated! :)

This is the javascript that I use with this, doesn't have a formCheck() function.

 

function formCheck(formobj){
// Enter name of mandatory fields	
var fieldRequired = Array("Name", "Email", "Enquiry");	
// Enter field description to appear in the dialog box	
var fieldDescription = Array("Name", "Email", "Enquiry");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
	var obj = formobj.elements[fieldRequired[i]];
	if (obj){
		switch(obj.type){
		case "select-one":
			if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
			break;
		case "select-multiple":
			if (obj.selectedIndex == -1){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
			break;
		case "text":
		case "textarea":
			if (obj.value == "" || obj.value == null){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
			break;
		default:
		}
		if (obj.type == undefined){
			var blnchecked = false;
			for (var j = 0; j < obj.length; j++){
				if (obj[j].checked){
					blnchecked = true;
				}
			}
			if (!blnchecked){
				alertMsg += " - " + fieldDescription[i] + "\n";
			}
		}
	}
}

if (alertMsg.length == l_Msg){
	return true;
}else{
	alert(alertMsg);
	return false;
}
}[code]

Cheers for the help!!

ok, rather than trying to guess, I've decided to try it this way:

<?php
$my_email = "[email protected]";
$continue = "/";
$errors = array();
$method = "_POST"; // <-- If this fails, change to _GET
// Build message.
function build_message($request_input){
if(!isset($message_output)){
	$message_output ="";
}
if(!is_array($request_input)){
	$message_output = $request_input;
}
else{
	foreach($request_input as $key => $value){
		if(!empty($value)){
			if(!is_numeric($key)){
				$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;
			}
			else{
				$message_output .= build_message($value).", ";
			}
		}
	}
}return rtrim($message_output,", ");
}

$message = build_message($$method);
$message = stripslashes($message);
$subject = "General Enquiry";
$subject = stripslashes($subject);
$from_Name = "";
if(isset($$method['Name']) && !empty($$method['Name'])){
$from_Name = stripslashes($$method['Name']);
}
$headers = "From: {$from_Name} <{$$method['Email']}>";
mail($my_email,$subject,$message,$headers);
$message = "Thank you for your enquiry. We will be in touch with you as soon as possible.
Regards,
Website Name
http://www.domain.com";
$subject = "General Enquiry";
$headers = "From: Website Name <[email protected]>";
mail($$method['Email'],$subject,$message,$headers);
?>

follow the instructions listed in the top.  I didn't really change much in your code.  It works fine. I just cleaned it up so it is standards-compliant.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.