Forum Moderators: coopster

Message Too Old, No Replies

Sending a copy of a message to the sender

         

Spyce

4:53 pm on Jan 31, 2012 (gmt 0)

10+ Year Member



I've been asked to create a contact form that sends a copy of the message to the sender.

I already have a contact form and would like to continue using that same form, but I don't know where to even begin. I've been searching the 'net and I've tried some things out, but so far, nothing has worked. I've been working with something called $CCUser (which I found in this post [webmasterworld.com]), but I can't get to it to work.

As it stands now, I have this as my form's PHP code:

<?php if($_POST['url'] != "") {
echo('
<p class="error">
You may be using a text-only browser or you are a spambot. Your message has not been submitted.
</p>
');
}
else {
if(isset($_POST['name'])) {
if(($_POST['name'] == "") or ($_POST['email'] == "") or ($_POST['message'] == "")) {
echo('
<p class="error">
all fields are required.
</p>'
);
}
else {
$name = $_POST['name'];
$email = $_POST['email'];
$formmessage = ($_POST['message']);
$emailmessage = "You have received a submission from your contact form.
Name: $name
Email: $email
Message: $formmessage
";
//Defining mail settings
$to = "admin@somewebsite.com";
$CCUser = $_POST['email'];
$subject = "Message from $name via SomeWebsite.com";
$headers = "From: $email";
if(isset($name)) {
mail($to,$CCUser,$subject,$emailmessage,$headers);
}
}
}
}
?>

<?php if(isset($name)) {
echo('
<div class="contactnote">
<p class="success">
You message has been successfully sent.
<br /><br />
Please allow 24-48 hours for a response. If it has been more than 24 hours and you still have not received a response, please try sending your message again.
</p>
</div>'
);
} ?>


If I remove the $to from mail(), the message will send to the address specified in the 'email' field (which is the sender's email address). However, this then means the message does not get passed onto the person that should be receiving the mail (admin@somewebsite.com). If I remove the $CCUser from mail() and add $to back in, then @admin@somewebsite.com will receive the email, but then the sender doesn't get a copy of the message. If I keep both $to and $CCUser, then the message doesn't send to anybody, even though the form returns saying that the message was successfully sent.

What am I doing wrong... why is this not working?

rocknbil

6:13 pm on Feb 1, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You set the CC user, you just didn't put it in the headers. :-) You also have an extra parameter that doesn't exist for mail() [php.net].

//Defining mail settings
$to = "admin@somewebsite.com";
$CCUser = $_POST['email'];
$subject = "Message from $name via SomeWebsite.com";
$headers = "From: $email\r\n";
if ($CCUser and ! empty($CCUser)) { $headers .= "CC: $CCUser\r\n"; }
if(isset($name)) {
// note there's no "cc" parameter. to, subject, message, headers
if (! mail($to,$subject,$emailmessage,$headers) ) {
// Always error trap. This would have told you something was wrong
echo "<p>ERROR: Could not send the mail.</p>";
exit;
}

} // end if $name


This should give you a header like

From: here@example.com
CC: there@example.com

even though the form returns saying that the message was successfully sent.


Because mail() fails silently as you've done nothing to check it, and your programming continues. See the error trap above - error trapping is your FRIEND! :-)

Spyce

8:01 pm on Feb 1, 2012 (gmt 0)

10+ Year Member



Excellent- this worked perfectly. Thank you so much!