My implementation of the PHP mail function is not sending email

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]

This is the same question as the previous one… under a different username??

is this the only script on the page? i just tested it and it works fine?

upon closer inspection i see that it sent me an email but it didn’t contain the info that the user entered…

change this bit:
[php]<?php
$fields{“name”} = “Name”;
$fields{“email”} = “Email”;
$fields{“subject”} = “Subject”;
$fields{“message”} = “Message”; ?>[/php]

to this:
[php]<?php
$fields[“name”] = “$name”;
$fields[“email”] = “$userEmail”;
$fields[“subject”] = “$subject”;
$fields[“message”] = “$message”; // could not see this in the script??
?>[/php]

Thanks, I’ll try what you posted and see if it works for me.

Yes I did post this twice because when I posted it as a guest it said that it failed to post, so I figured I would register and then maybe it would post correctly.

Looks like it posted twice anyway…

I’ll see about removing/closing the other one.

Thanks again for your suggestion

Thanks for the suggestion. The code that you posted actually does need to be the way that I originally had it because that is the “human readable” text that will appear on the email. so it will say look like this:

Contact Information:

           Name: Jason
          Email: [email protected]
        Subject: Test
        Message: blah blah

Turns out that my script was working the whole time. The problem appears to be with the mail server setup. It looks like the script that is running on my site is unable to send emails to an email address of the same domain. I’m not sure why that’s the case, but when I figure it out I’ll post the answer here for anyone that happens to run into this same issue.

Thanks again Redscouse for your suggestion!

Sponsor our Newsletter | Privacy Policy | Terms of Service