Jump to content

Recommended Posts

Hi, I have an issue that has been perplexing me for a few days.  I have the following code on an action page for a contact form.  The problem is that whether I run the file through the AJAX call that processes the contact form or directly in the browser, I get duplicate emails. 

 

I have confirmed that the query is only retrieving one email address to send to.  It's not in any kind of loop that could call mail() multiple times.  I've confirmed that it's only called once with "alert()" in my Javascript as well.  The first email comes almost immediately and the second is usually 5-10 minutes later. 

 

			$strMessageBody = "Name: " . $_GET['from'] . "<br/>Phone: " . $_GET['phone'] . "<br/>Email: " . $_GET['email'] . "<br/>" . $_GET['body'];
			$strHeaders  = "MIME-Version: 1.0\r\n";
			$strHeaders .= "Content-type: text/html; charset=iso-8859-1\r\n";
			$strHeaders .= "From: " . $_GET['email'] . "\r\n";

			if ($_GET['subject'] == "") {
				$strSubject = "Contact Form Response";
			}
			else {
				$strSubject = $_GET['subject'];
			}

			$binMailSent = mail(mysql_result($qryGetEmail,0,health_pro_email), $strSubject, $strMessageBody, $strHeaders);

 

Any thoughts on why this is happening? 

Link to comment
https://forums.phpfreaks.com/topic/186726-duplicate-emails-being-sent/
Share on other sites

There's not enough information there to really provide an answer. You should probably try some simple debugging approaches to try and isolate various parts of the functionality to try and determine the cause.

 

My first thought would be to take just the code above for actually sending the email and put it into a page all by itself (just hard code the variables). Then call the page through your browser. Do you get two emails? If so, then the problem sounds as if it might be server related and not PHP.

 

If the above works correctly (one email) then I would probably set up a test taking the AJAX component out of the equation.

 

And so on. The idea is to try and remove all variable but one or remove only one variable at a time. And, validate, validate, validate: in addition to using the JavaScript alert, have it alert some useful information and add thers as needed so you can see the values of variables as they are set or changed. And do the same thing in the PHP code to echo values to the page.

 

 

Hmmm... after looking closer at the code I'm not understanding the line that sends the email. Where is $qryGetEmail defined and where are you actually running the query? I would suggest running the query and saving the result to a string variable - THEN run the mail() function. You can then verify that the address is correct by echoing it to the page.

Hey, thanks for the response.  I stripped out the query as well as the input cleansing, etc in the previous code that I posted...just posted the actual email part.  But I took your advice and hard-coded everything...and I only received one email.  But as soon as I pass a variable on the URL, such as adding "?body=Testing" and incorporate it into the email, I get two.  I've tried it with both body and subject and I end up getting duplicates. 

 

Here is every bit of the code that I have running on my test page (excluding my configuration and function calls).  This will send me two identical emails.  (Or if I hard code $strMessageBody and set $strSubject = $_GET['subject'].)

			$strEmail = "xxxxxxxx@xxxxxx.com"; // I used my valid email, but blanked it here for obvious reasons
			$strSubject = "Testing";
			$strMessageBody = $_GET['body'];
			$strHeaders  = "MIME-Version: 1.0\r\n";
			$strHeaders .= "Content-type: text/html; charset=iso-8859-1\r\n";
			$strHeaders .= "From: test@aol.com\r\n";

			echo "Email: " . $strEmail . "<br/><br/>";
			echo "Subject: " . $strSubject . "<br/><br/>";
			echo "Body: " . $strMessageBody . "<br/><br/>";
			echo "Headers: " . $strHeaders . "<br/><br/>";

			$binMailSent = mail($strEmail, $strSubject, $strMessageBody, $strHeaders);

			if($binMailSent) {
				$strFormValidated = "Your email has been sent.";
			}
			else {
				$strFormValidated = "There was an error sending your email. Please try again.";
			}

echo $strFormValidated;

 

However, if I hardcode all of the variables, I only get one email.  Thoughts?

Yeah, are you sure there is no other code on the page  - especially JavaScript? I'm thinking there might be something going on where the form is double posting or the page is refreshing. How exactly are you setting those $_GET values? Are you doing it through an AJAX call? Have you tried just typing in the url with the appropriate parameter into the address bar?

I went ahead and stripped out the includes as well, but yeah, that's it.  I've been calling the file directly from the URL rather than through AJAX.  I've just been appending the variables onto the end of the URL like .../testemail.php?body=Test 202 (I can post the entire URL if you want, but trying to keep from filling my inbox with test emails).

 

What I posted before is exactly what my code looks like now, except I have the echo of the email blanked out to keep it off the page.  I've tried it with the variables hardcoded, but still passing variables on the URL, which also kicked two emails, even though I completely ignored the URL variables.  If I take off the URL parameters, only one email....now that's confusing!  :wtf: 

 

Am I using reserved keywords on the URL?  I'm using: from, email, subject, phone, and body.  I wouldn't think PHP would puke on that passed as a URL parameter.  Thoughts?

I'm using FF 3.5.7, but IE 6 also causes a double send.
That pretty much eliminates the actual browser causing it and confirms it is something in your server Edit: or in your code in the browser.

 

I do have some URL rewriting going on, but not on the AJAX call.
Just because the specific URL is not being rewritten, does not mean that the url rules are not being evaluate. This is the most likely cause, especially because it acts different with and without GET parameters on the end of the URL. Post your .htaccess file.

 

You have also not posted the form that is requesting that page so it is also likely that it is doing the double request. Once in your javascript and once when the browser requests it due to the normal form submission.

PFM, here is what's in my htaccess file.  It's pretty simple rewriting. 

 

Options +FollowSymLinks
RewriteEngine On

# If the user is trying to confirm their account, send the code with the URL
RewriteRule ^(confirm)?/([a-z0-9_-]{32})/?$ http://www.bodyfitburn.com/fitnessdirectory/index.php?site_area=confirm&action=home&code=$2 [L,NC]

# If the user is accessing a profile or address, send the ID with the URL
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([0-9]+)/?$ http://www.bodyfitburn.com/fitnessdirectory/index.php?site_area=$1&action=$2&id=$3 [L,NC]

# Regular rewrite rules
RewriteRule ^([a-z0-9_-]+)/?$ http://www.bodyfitburn.com/fitnessdirectory/index.php?site_area=$1&action=home [L,NC]
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/?$ http://www.bodyfitburn.com/fitnessdirectory/index.php?site_area=$1&action=$2 [L,NC]

 

However, even if I call the page that sends the email directly in my browser, I'm getting two emails, but only when there are query variables. 

 

Here is the form code that you requested:

			<form name="contactform">
			<table width="100%" id="contactform">
				<tr>
					<th colspan="4">Contact Me!</th>
				</tr>
				<tr>
						<td id="formerrors" colspan="4"><?php echo $strFormValidated ?></td>
				</tr>
				<tr>
					<td id="fieldheader">
						Name: <span class="requiredfield">*</span>
					</td>
					<td id="inputfield">
						<input type="text" name="contactform_name" size="25" value="<?php echo $_POST['contactform_name']; ?>" />
					</td>
					<td id="fieldheader">
						Email: <span class="requiredfield">*</span>
					</td>
					<td id="inputfield">
						<input type="text" name="contactform_emailto" size="25" value="<?php echo $_POST['contactform_emailto']; ?>" />
					</td>
				</tr>
				<tr>
					<td id="fieldheader">
						Phone:
					</td>
					<td id="inputfield">
						<input type="text" name="contactform_phone" size="25" value="<?php echo $_POST['contactform_phone']; ?>" />
					</td>
					<td id="fieldheader">
						Subject:
					</td>
					<td id="inputfield">
						<input type="text" name="contactform_emailsubject" size="25" value="<?php echo $_POST['contactform_emailsubject']; ?>" />
					</td>
				</tr>
				<tr>
					<td id="fieldheader">
						Message:
					</td>
					<td id="inputfield" colspan="3">
						<textarea name="contactform_body" rows="3" cols="50"><?php echo $_POST['contactform_body']; ?></textarea>
					</td>
				</tr>
				<tr>
					<td colspan="4">
						<input type="button" name="submitform" value="Send Email" onclick="submitContactForm();" />
						<input type="reset" name="reset" value="Reset" />
						<input type="hidden" name="id" value="<?php echo mysql_result($qryAddressProfile,0,health_pro_id) ?>" />
					</td>
				</tr>
			</table>
		</form>

 

Not sure of anything in there that could be causing the issue.  When the user clicks "Send Email", Javascript validates the inputs and, assuming all req'd fields are entered and inputs are the proper format, it kicks everything to the php file, where the variables are scrubbed of any HTML inputs, etc, then the email is sent.  As you can see, I have only a single call to mail() and I've confirmed that the Javascript isn't looping or being called twice.  It's something about adding query variables to the end of the URL.  Is that a server issue that I should contact my host about or something I can handle?

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.