Topic: mail() issues

I'm working on a contact form, and the mail() function is PHP seems to always fail.  What am I doing wrong here?

Here's some code snippets of what's relevant:

$sendTo = "example_email@gmail.com";
$fromName = 'Form';
$fromEmail = 'DO_NOT_REPLY@whatever.com';
$subject = 'Registration Form';

$headers  = "MIME-Version: 1.0 \r \n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r \n";
$headers .= "From: ".$fromName." <".$fromEmail."> \r\n";
$headers .= "To: ".$sendTo;
$headers .= 'X-Mailer: PHP/' . phpversion();
		
$message = "this is the message body";
		
if (mail($sendTo,$subject,$message,$headers)) {
	$status = "Your registration was sent!";
}else {
	$status = "Your message failed to be sent!  Please try again later.";
}

It always triggers the else statement when I check to see if mail() worked or not.  What's going on here?

Re: mail() issues

Hey Chris,

Try taking out the space between the \r and \n in the following lines:

$headers  = "MIME-Version: 1.0 \r \n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r \n";

Hope that helps.

Exercise your faith!

Re: mail() issues

Nope, still no good.

Re: mail() issues

Is it a Linux Server or Windows Server? Hosting with a company or on your local box at the moment? Just curious.

Try it without the headers and see if it works. If it does, then try including the headers as it appears in the example on php.net, which is one long string that has concatenations. I'm thinking that maybe it has something to do with the headers. Probably an error there or an extra space somewhere.

Exercise your faith!

Re: mail() issues

Well, turning off the headers works, so I narrowed it down line by line and it turns out the "To:" line was breaking it.  I removed that from the headers and since that information is included in the mail() variables it gets sent.

Well, whatever, but it seems to work now.  Thanks!

Re: mail() issues

You're welcome. I'm guessing that since you already have a parameter in the mail() function for 'To' then it would take care of adding that to the header and so there is no need to do it a second time. In any case I'm glad you got it working.

Exercise your faith!