deps_stats
deps_stats

Reputation: 264

Sending an e-mail with php

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('[email protected]','Test','Test OK');
echo 'Message sent!';
?>

Instead of '[email protected]' 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!

Upvotes: 0

Views: 360

Answers (3)

user852830
user852830

Reputation:

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.

Upvotes: 1

ceejayoz
ceejayoz

Reputation: 180177

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

Upvotes: 2

Dan Grossman
Dan Grossman

Reputation: 52372

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('[email protected]','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.

Upvotes: 5

Related Questions