Wepex
Wepex

Reputation: 163

How to send a copy of email to senders address in php contact form

I have a contact form in my website. I need users to send messages to my email, and it does so. I also have a checkbox, which when checked, the users will have the message to the email indicated on the email textbox. My code sends it only to my email, but it doesn't send a copy when the checkbox is checked.

Contact.html

<input type="checkbox" name="sendcopy" value="Yes" checked/>Copy this message to your mail

Sendmail.php:

$messagebody="Name: ".$name."".PHP_EOL;
$messagebody.="email: ".$email."".PHP_EOL;
$messagebody.="website: ".$website."".PHP_EOL;
$messagebody.="message: ".nl2br($message)."".PHP_EOL;

mail($to,$subject,$messagebody,$headers)or die("The message failed to send");  

if(isset($_POST["sendcopy"]) && $_POST["sendcopy"]=="checked"){
mail($email,$subject,$messagebody,$headers)or die("The message failed to send a copy") 
    }

Upvotes: 0

Views: 1844

Answers (3)

The Real Coder
The Real Coder

Reputation: 138

In the snippet if(isset($_POST["sendcopy"]) && $_POST["sendcopy"]=="checked"){ value of $_POST["sendcopy"] you are trying to match with is wrong. Instead, you can use just if(isset($_POST["sendcopy"]){ to check if that checkbox is checked.

Upvotes: 1

purpletree
purpletree

Reputation: 1943

if (isset($_POST['sendcopy']))

Upvotes: 0

Marc B
Marc B

Reputation: 360682

if(isset($_POST["sendcopy"]) && $_POST["sendcopy"]=="checked"){
                                                    ^^^^^^^^^

wrong. it should be "Yes", because the value defined in the form:

if(isset($_POST["sendcopy"]) && $_POST["sendcopy"]=="Yes"){

Upvotes: 0

Related Questions