0

I'm trying to write a very simple php code that sends an e-mail to a given address.

I have this code:
<?php
mail('address@server.com','Test','Test OK');
echo 'Message sent!';
?>

Instead of 'address@server.com' I've used my own e-mail.

I´ve enabled php using the directions given in foundationphp, because I'm running a mac with snow leopard.

php seems to be working because I´m getting the "message sent!" echo, however i'm not receiving any e-mail.

Any ideas why is this happening? What am I doing wrong?

Thanks in advance!

deps_stats
  • 231
  • 1
  • 2
  • 9
  • Check that mail returns false first of all. To find the actual error check the system logs (/var/log/messages usually under Linux if using SendMail). Also ensure that error reporting is set to E_ALL in your php.ini so that you can see the errors. – bcoughlan Aug 10 '11 at 00:36
  • Sendmail would log to `/var/log/maillog` typically, not `messages`? We know this is not Linux though, it's OS X. – Dan Grossman Aug 10 '11 at 00:37
  • 1
    You may not have an smtp server installed and configured. Try this code (http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page/2748837#2748837) that uses the gmail smtp if you have a gmail account to see if that's the case. – Master_ex Aug 10 '11 at 00:43

3 Answers3

5

First of all, you didn't check the return value of mail at all, you'll echo "Message sent!" even if it returned false.

if (mail('address@server.com','Test','Test OK')) {
    echo 'Message sent!';
}

Second, even if mail returns true, that just means it handed the mail to the MTA, not that the mail reached its destination. If PHP is handing off the mail successfully, then you need to look at your mail logs and debug there, because the issue isn't in the PHP code/configuration.

Dan Grossman
  • 49,405
  • 10
  • 105
  • 95
2

If you're running this on your local machine, chances are your e-mails aren't going to go anywhere. PHP's mail function doesn't know if you've got a proper outgoing mail connection, it only knows if the mail entered the local queue successfully.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination. - http://php.net/mail

Your best bet for a non-server machine is to set up a remote SMTP server (usually, the one at your ISP). http://email.about.com/od/emailprogrammingtips/qt/Configure_PHP_to_Use_a_Remote_SMTP_Server_for_Sending_Mail.htm

ceejayoz
  • 165,698
  • 38
  • 268
  • 341
  • I'm running in my local machine. I think this is the actual problem. I'll try to configure the SMTP server. Thanks! – deps_stats Aug 10 '11 at 00:43
1

You receive the echo "Message sent!" becuase you did not ask if mail() was successful:

if ( mail(...) )
{
   echo 'Message sent!';
}
else 
{
   echo "Fail!";
}

That should give you a correct feedback about it.