0

Currently I have the code below which sends the contents of the html input field (email) to the welcome@domain.com address. There are two things which I am now struggling with. I have used the following bits of code to no avail -

$to = "welcome@domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thankyou.html');
  1. How can I automatically send an email response from welcome@domain.com?

  2. Instead of the page saying 'Thank you for contacting us, we will be in touch very soon' is there a way that they are redirected to domain.com/thankyou.html instead?

This is the rest of the code which seems to be working fine! If there are any suggestions for this though they are always more than welcome!

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

$email_to = "welcome@domain.com";
$email_subject = "Your spot has been saved!";
function died($error) {


echo "We are very sorry, but there were error(s) found with the form your submitted";
echo "These errors appear below<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors<br /><br />";
die();
}


if(
!isset($_POST['email'])
) {
died("We are sorry, but there appears to be a problem with the form your submitted!");
}
$email_from = $_POST['email']; 
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= "The Email Address you entered does not appear to be valid!<br />";
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= 'Email: '.clean_string($email_from)."\n";

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?
}
?>
  • 2
    What's the problem with your existing code? Does it give an error message? Not send an email? Send an email to the wrong address? – andrewsi Sep 17 '12 at 17:02
  • Hi andrewsi, the form sends the initial email to welcome@domain.com and no errors are shown, the page just shows 'thank you for contacting us, we will be in touch with you very soon'. The return email is just not sending, to my knowledge it isn't sending to the wrong address, just not sending at all. –  Sep 17 '12 at 17:16
  • you should remove the "@" prefix from your second `mail` call, else it will suppress error messages (http://stackoverflow.com/questions/4653358/what-does-the-prefix-do-in-php)! – codeling Sep 18 '12 at 10:43

1 Answers1

0

Regarding page being not redirected refer this post stackoverflow.com/questions/6396813/php-header-not-redirecting It basically says you must not output any value to stream before header call.

For mail issue please elaborate more what error you are getting.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47