Log in

View Full Version : PHP Mail Not Working



onestopplay
05-02-2010, 08:37 PM
Code:

$to = "me@me.com";
$subject = "Question from the Fact or Fiction Forum";
$from = "you@you.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from.'';

$message = '
<html>
<head>
<title>Question from the Fact or Fiction Forum</title>
</head>
<body>
This question is from <b>'.$name.'</b> with the email address <b>'.$email.'.<br>
Here\'s the question:<br>
'.$question.'<br><br>
<a href="">Click here to answer the question.</a><br><br>
</body>
</html>';
mail($to,$subject,$message,$headers);

This isn't sending.
Any help would be appreciated! THANKS!

djr33
05-02-2010, 08:59 PM
Nothing in that looks specifically wrong. Have you confirmed that mail() functions on your server?

Try:
mail('me@me.com','Test','Testing an automated email...');

If that works, then something is wrong with the code above. It might be something simple or it might be a conflict with the format of the text and/or headers. I haven't looked into it much.

But if it doesn't work, then mail() is not properly configured. It may mean you need to change a setting or it may mean that you don't have a mail server configured on the server and will need to set that up-- if you don't know how or don't have access to do so, the best answer really is to just switch hosts and use a decent host that has the basics like that setup-- it's pretty much standard.
You can use <?php phpinfo(); ?> and look through the information given (it'll be a full page of text, so you can just use that on a new/blank page), and see if you can find something about a mail server. If so, it's probably configured so that's not the problem.


And before you spend a lot of time trying to fix all of this, 1. be sure that you typed your email address correctly and 2. the message isn't getting sent to your spam folder instead. It's possible it'll be seen as spam because it's automated. These might seem obvious, but it's not worth putting the time in to fix the code without double-checking this.
Also, I have heard that sometimes particular email companies/accounts just don't get emails from PHP for some reason. While I don't really believe this is a definitive answer and it can't be fixed, it's not a bad idea to try a few different emails (at a different .com, etc). If it doesn't work on 2 of your emails, then you can be pretty sure it's not working.


Finally, the first thing in the code itself to start debugging is to figure out if PHP thinks that it's working or thinks that there was an error.
Surround mail() with an if like this:

if (mail(....SAME AS YOUR CODE.....)) {
echo 'PHP thinks it is working';
}
else {
echo 'PHP knows it is broken, not sure why';
}


The only thing that looks suspicious in your code is that you're trying to send an html email. That should be possible, but it's something to eliminate as a possible problem while testing.

crobinson42
05-03-2010, 03:58 PM
I also had the same problem. I was using Godaddy.com and they have a special link in your hosting control panel to enable/allow php email script on your site. They do this to prevent spammers from manipulating code. I'm pretty sure it would be your host.

Hope that helps:)