View Full Version : email confirmation

11-22-2011, 09:01 AM
Hi and hope someone can help.

I'm using a free PHP contact form that I downloaded from here (http://www.freecontactform.com/email_form.php). It comprises of an html contact form page and a PHP sendmail page.

This works fine and almost meets my needs, however, on success or failure of sending the email, the sendmail page overwrites the contact form with either success or failure messages. Can anyone suggest a way that I can incorporate the success / failure messages onto the contact form itself, something like a hidden div that gets revealed on succes / fail.

Here's the sendmail PHP file (I've not included the contact.html page as it is relatively straightforward).

My thanks

if(isset($_POST['email'])) {

$email_to = "someone@somewhere.com";
$email_subject = "Feedback from the web page.";
$email_message2 = "Thank you for your feedback, I shall be in touch as soon as possible.\n\nKind regards.

function died($error) {
// your error code can go here
echo "I am sorry there were error(s) found with the form that you submitted. ";
echo "These errors appear below:<br /><br />";
echo $error."<br /><br />";
echo "Please try again.<br /><br />Use your 'Back' button to return to the contact form.";

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
if(strlen($error_message) > 0) {
$email_message = "User input to the contact form is shown below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
@mail($email_from, $email_subject, $email_message2, $headers);

Thank you for contacting me. I will be in touch as soon as possible.<br /><br />Use your 'Back' button to return to the contact form.

11-23-2011, 01:07 AM
a simple way to do it bro. actually, a function will return false if something wrong. function mail() too. so, try this:

if(!@mail($email_to, $email_subject, $email_message, $headers)){
$error = "there is a problem while sending first email";
if(!@mail($email_from, $email_subject, $email_message2, $headers)){
$error = "there is a problem while sending second email";

then, just print $error.

look at this:

!@mail() <---- use ! and @.

! mean as "without" or "false"
@ for hidden the error message from mail() function. so no error function displayed on browser.

I am sorry, my english is very bad. Hope it help.

11-23-2011, 07:39 AM
Thanks @XterM,

Appreciate that, though sadly I'm just about to go out so won't be able to play with your code till tomorrow. I'll let you know the outcome....