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 = "name@domain.com";

$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 <noreply@domain.com>";

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

?>

 

Many thanks,

 

Paul

Link to comment
Share on other sites

It's doing it everytime. I have used it before on my server and it's worked fine. Just seems to be now, I contacted my host and they suggested seeing if I could get some help with the script but I thought it seemed ok.

 

Thanks for the reply.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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

<?php
$my_email = "name@domain.com";
$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 <noreply@domain.com>";
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.

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.