pauldreed Posted November 1, 2006 Share Posted November 1, 2006 I have a contact form on my website which sends the contacts details to my email.However, when the email arrives it indicates that it has been sent from my server, whereas I want it to show that it has been sent from the email address entered on the form.Example; I get email which includes the sender address as serverone.jabweb.co.uk, but the email of the person submiting the form is [email protected]. If I then try to reply to the message, it would be sent to serverone.jabweb.co.uk instead of [email protected]. As being new to php, what do I need to alter to change this. I have attached part of the code below.<?php # fill this in with the address to which the email will be sent$recipient = '[email protected]';session_start();define('CAPTCHA_PATH', $_SERVER['DOCUMENT_ROOT'].'/captcha/'); if(isset($_GET['i'])){ captcha_image();}elseif(isset($_POST['submit'])){ if(!($error = error())) { $subject = 'Mail from '.$_SERVER['HTTP_HOST'].' webform'; $message = "Sender: ".ucwords($_POST['name'])." \r\n\r\n". "Email: {$_POST['email']} \r\n\r\n". "Happy: {$_POST['happy']} \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)) { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success'); } else { header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure'); } } }function error(){ if(empty($_POST['name'])) { return 'ERROR: The name field is empty.'; } if(preg_match('/[^a-zA-Z ]/', $_POST['name'])) { return 'ERROR: The name field contains invalid characters.'; } if(empty($_POST['email'])) { return 'ERROR: The email field is empty.'; } if(!eregi('^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,6})$', $_POST['email'])) { return 'ERROR: The email field contains invalid syntax.'; } if(empty($_POST['comments'])) { return 'ERROR: The comments field is empty.'; } if(!captcha_validate()) { return 'ERROR: Incorrect security code entered.'; } return false;The rest of the code goes onto the Captcha code. Link to comment https://forums.phpfreaks.com/topic/25859-emailer-question/ Share on other sites More sharing options...
alpine Posted November 1, 2006 Share Posted November 1, 2006 You can accomplish this by adding some appropriate headers on the email.Try this inbetween your current code, the headers is now at minimum:[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 = 'Mail from '.$_SERVER['HTTP_HOST'].' webform';$message ="Sender: $sender_name \r\n\r\n"."Email: $sender_email \r\n\r\n"."Happy: {$_POST['happy']} \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))// continue your current code?>[/code] Link to comment https://forums.phpfreaks.com/topic/25859-emailer-question/#findComment-118084 Share on other sites More sharing options...
pauldreed Posted November 2, 2006 Author Share Posted November 2, 2006 Alpine, thanks I used your code and I now get the email exactly as I want it!To take the code one stage further, to also send a confirmation email to the person submitting the form, I have added on the code below, but there is some error which prevents the page from displaying in the browser.I have set the variables $sendnotification and $websitetitle earlier in the code, and if I remove everything between the 'send confirmation email' comments, I go back to a working page. Is there something stupid creating an error with my addition?[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] Link to comment https://forums.phpfreaks.com/topic/25859-emailer-question/#findComment-118672 Share on other sites More sharing options...
alpine Posted November 6, 2006 Share Posted November 6, 2006 It looks like you have misplaced sendnotification outside the brackets of the if(mail testOn these errors please post full code.Try:[code]<?phpif(mail($recipient, $subject, $message, $headers)){# send confirmation emailif($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 etc..?>[/code] Link to comment https://forums.phpfreaks.com/topic/25859-emailer-question/#findComment-120163 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.