I’m trying to set up a contact page that allows the user to enter their name, email address, a subject and a message. Then have that form send the information to a php script that uses the PHP mail() function to send me an email as well as a confirmation email to them. The problem that I am having is that the email to them is sent and they think that everything worked correctly, however, I never get their email. I’m using the same code to send both emails and so I’m not sure why one works and the other doesn’t. I’ve included my code below.
Any help would be greatly appreciated.
[php]<?php
// Variables for sending email to me from contact form
$to = ‘[email protected]’;
$userEmail = $_POST[‘email’];
$name = $_POST[‘name’];
$headers = “From: [email protected]”;
$subject = “Email from Contact Page”;
$fields = array();
$fields{“name”} = “Name”;
$fields{“email”} = “Email”;
$fields{“subject”} = “Subject”;
$fields{“message”} = “Message”;
$body = “Contact Information:\n\n”; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
// Variables to send thank you auto-reply email
$headersAutoreply = “From: [email protected]”;
$subjectAutoreply = “Thank you for contacting me!”;
$bodyAutoreply = “Thank you for contacting me. I will get back to you as soon as possible, usually within 24 hours. If you have any more questions, please consult my website at www.mydomain.com”;
// If there is no email for the user then we can’t send them an email so notify them.
if($userEmail == ‘’)
{
print “Email not sent. There was no email address.”;
}
else {
// Send email to me
$send = mail($to, $subject, $body, $headers);
// Send autoreply thank you email
$sendAutoreply = mail($userEmail, $subjectAutoreply, $bodyAutoreply, $headersAutoreply);
//DEBUGGING:
/*
$send = ‘To: ’ . $to .’
’. ‘Subject: ’ . $subject .’
’. ‘Body: ’ . $body .’
’. ‘Headers: ’ . $headers;
$sendAutoreply = ‘To: ’ . $userEmail .’
’. ‘Subject: ’ . $subjectAutoreply .’
’. ‘Body: ’ . $bodyAutoreply .’
’. 'Headers: ’ . $headersAutoreply;
echo 'Contact Form email: <br />' . $send .'<br /><br />';
echo 'Auto-reply email: <br />' . $sendAutoreply;
*/
// If the email was sent, redirect the user to the thank you page.
if($send)
{
header( "Location: http://www.mydomain.com/thankyou.html" );
}else // Otherwise notify them that the email was not sent and provide them options.
{
print "We encountered an error sending your mail, please notify [email protected]";
}
}
?>
[/php]