pauldreed Posted November 3, 2006 Share Posted November 3, 2006 I have a 'contact me' page with a form for visitors to enter details. The underlying code works great (thanks to earlier help in this forum), but to improve it, I want to add a new bit of code to email a confirmation email back to the visitor as well. I have attached a chunk of code below, but when I add the code between the 'send confirmation email' comments, the form errors. Can anyone see what the problem is please. I am very new to php and finding it difficult to remedy errors.I have defined $sendnotification and $websitetitle earlier in the code.[code]if(!($error = error())){$sender_name = ucwords($_POST['name']);$sender_email = $_POST['email'];$headers = "From: $sender_name <$sender_email>\r\n";$headers .= "Reply-To: $sender_name <$sender_email>\r\n";$headers .= "Return-Path: $sender_name <$sender_email>\r\n";$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];$message ="Sender: $sender_name \r\n\r\n"."Email: $sender_email \r\n\r\n"."Telephone: {$_POST['tel']} \r\n\r\n"."Prefered Contact time: {$_POST['time']} \r\n\r\n"."Message: {$_POST['comments']} \r\n\r\n".$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];if(mail($recipient, $subject, $message, $headers))# send confirmation email if($sendnotification == true) { $notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; $notification_subject = "Thanks for your message to $websitetitle."; mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); }# end of confirmation email code[/code] Quote Link to comment Share on other sites More sharing options...
alpine Posted November 3, 2006 Share Posted November 3, 2006 You are missing some closing brackets, try:[code]<?phpif(!($error = error())){$sender_name = ucwords($_POST['name']);$sender_email = $_POST['email'];$headers = "From: $sender_name <$sender_email>\r\n";$headers .= "Reply-To: $sender_name <$sender_email>\r\n";$headers .= "Return-Path: $sender_name <$sender_email>\r\n";$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];$message ="Sender: $sender_name \r\n\r\n"."Email: $sender_email \r\n\r\n"."Telephone: {$_POST['tel']} \r\n\r\n"."Prefered Contact time: {$_POST['time']} \r\n\r\n"."Message: {$_POST['comments']} \r\n\r\n".$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];if(mail($recipient, $subject, $message, $headers)){# send confirmation email if($sendnotification == true) { $notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; $notification_subject = "Thanks for your message to $websitetitle."; mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); }# end of confirmation email code}}?>[/code] Quote Link to comment Share on other sites More sharing options...
pauldreed Posted November 3, 2006 Author Share Posted November 3, 2006 Thanks Alpine for your patience, but I'm afraid that doesnt work either.I have included a slightly bigger chunk of code below, from the start up to the next section - function error() which may may make it clearer.[code]<?php # fill this in with the address to which the email will be sent$recipient = 'paul@gmail.com';# Send notification to sender (use false if not required)$sendnotification = true;# Your web site title (John Doe's Site)$websitetitle = 'mysite';session_start();define('CAPTCHA_PATH', $_SERVER['DOCUMENT_ROOT'].'/captcha/'); if(isset($_GET['i'])){ captcha_image();}elseif(isset($_POST['submit'])){if(!($error = error())){$sender_name = ucwords($_POST['name']);$sender_email = $_POST['email'];$headers = "From: $sender_name <$sender_email>\r\n";$headers .= "Reply-To: $sender_name <$sender_email>\r\n";$headers .= "Return-Path: $sender_name <$sender_email>\r\n";$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];$message ="Sender: $sender_name \r\n\r\n"."Email: $sender_email \r\n\r\n"."Telephone: {$_POST['tel']} \r\n\r\n"."Prefered Contact time: {$_POST['time']} \r\n\r\n"."Message: {$_POST['comments']} \r\n\r\n".$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];if(mail($recipient, $subject, $message, $headers))# send confirmation email if($sendnotification == true) { $notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; $notification_subject = "Thanks for your message to $websitetitle."; mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success'); } else { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure'); } } }[/code]Now if I comment out this section;[code]<?php# send confirmation email #if($sendnotification == true) { #$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; #$notification_subject = "Thanks for your message to $websitetitle."; #mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); #}[/code]the form is error free. Quote Link to comment Share on other sites More sharing options...
alpine Posted November 3, 2006 Share Posted November 3, 2006 Ok, there u have one bracket too much. [color=orange]# send confirmation email #if($sendnotification == true) { #$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; #$notification_subject = "Thanks for your message to $websitetitle."; #mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); #[/color]}Try:[code]<?php# fill this in with the address to which the email will be sent$recipient = 'paul@gmail.com';# Send notification to sender (use false if not required)$sendnotification = true;# Your web site title (John Doe's Site)$websitetitle = 'mysite';session_start();define('CAPTCHA_PATH', $_SERVER['DOCUMENT_ROOT'].'/captcha/');if(isset($_GET['i'])){ captcha_image();}elseif(isset($_POST['submit'])){if(!($error = error())){$sender_name = ucwords($_POST['name']);$sender_email = $_POST['email'];$headers = "From: $sender_name <$sender_email>\r\n";$headers .= "Reply-To: $sender_name <$sender_email>\r\n";$headers .= "Return-Path: $sender_name <$sender_email>\r\n";$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];$message ="Sender: $sender_name \r\n\r\n"."Email: $sender_email \r\n\r\n"."Telephone: {$_POST['tel']} \r\n\r\n"."Prefered Contact time: {$_POST['time']} \r\n\r\n"."Message: {$_POST['comments']} \r\n\r\n".$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];if(mail($recipient, $subject, $message, $headers))# send confirmation email if($sendnotification == true) { $notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; $notification_subject = "Thanks for your message to $websitetitle."; mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success'); } else { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure'); } } }?>[/code] Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted November 3, 2006 Share Posted November 3, 2006 The code you last pasted is still malformed.The opening bracket ( [b]{[/b] ) before this line:[code]header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success');[/code]should be moved up to be right after this line:[code]if(mail($recipient, $subject, $message, $headers))[/code]So your code block should change from this:[code]<?phpif(mail($recipient, $subject, $message, $headers))# send confirmation email if($sendnotification == true) { $notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; $notification_subject = "Thanks for your message to $websitetitle."; mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success'); } else { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure'); } }?>[/code]... to this ...[code]<?phpif(mail($recipient, $subject, $message, $headers)){ # send confirmation email if($sendnotification == true) { $notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly"; $notification_subject = "Thanks for your message to $websitetitle."; mail($sender_email, $notification_subject, $notification_message, "From: $recipient"); header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success'); } else { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure'); }}?>[/code] Quote Link to comment Share on other sites More sharing options...
pauldreed Posted November 3, 2006 Author Share Posted November 3, 2006 AlpineYour code works, but I am still not sure what you have added/changed and why, I'll get my glasses and have another look! But thanks again, youve been a great help.HeyraySorry your'e amendment errors EDIT>> Found the missing bracket at the end! but how did you find it? Did you work through the code line by line, or somehow find the error by using an application? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.