Skip to main content
February 17, 2011
Question

How to stop duplicate emails?

  • February 17, 2011
  • 1 reply
  • 474 views

I've been using the following bit of code to send out two emails when a user completes a 'contact us' form. The first email is sent to the website owner, and the second to the person who completes the form. The code has been edited to remove any real email addresses.

<?php
$to = "joe@blogs.co.uk";
$subject = "Website enquiry". $_POST['Name'];
$body  = "Name: ".$_POST['Name']."\n"."\n"."Location:  ".$_POST['Location']."\n"."\n"."Email Address:  ".$_POST['emailAddress']."\n"."\n"."Telephone Number:  ".$_POST['TelNo']."\n"."\n"."Comments: ".$_POST['Comments']."\n"."\n";
$headers = "From: ". $_POST['emailAddress'] ."\n";
mail($to,$subject,$body,$headers);
//?>

//<?php
$to = $_POST['emailAddress'];
$subject = "Thank you for contacting Joe";
$body = "Name: ".$_POST['Name']."\n"."\n"."Location:  ".$_POST['Location']."\n"."\n"."Email Address:  ".$_POST['emailAddress']."\n"."\n"."Telephone Number:  ".$_POST['TelNo']."\n"."\n"."Comments: ".$_POST['Comments']."\n"."\n";
$headers = "From: "."joe@blogs.co.uk"."\n";
mail($to,$subject,$body,$headers);
?>

However, it appears that the first email is sent twice. Also, the first time the email is sent it contains all the information passed from the form, and the second time it contains blanks.

I don't think  that the second email is sent twice.

It was suggested that I get rid of the closing php tag at the end of the first chunk of code and the opening one at the start of the second chunk. Hence it is commented out, but this has made no difference.

Any ideas gratelly received.

This topic has been closed for replies.

1 reply

Participating Frequently
February 17, 2011

I can't answer your question, but I would not be sending the comments field back to the address in the form. What's to stop someone from using your form as a spam relay?

February 18, 2011

Thanks, but could you expand on that a bit more? And is it just the comments field or all of them?

Participating Frequently
February 18, 2011

Any form that sends email to an address specified in the form could be used by spammers. Usually, if you validate and strip out url's so they can't post links, your form won't be hijacked often. But it will still happen. If you allow free form text to be sent, it will happen more often.

It's usually best to just send a hardcoded reply that their request was received. Otherwise, if you must send the comments, use a validation technique like CAPTCHA or similar.