21

I want to script a simple registration form with activation mail and so on. But for some reason mail() doesn't send the emails, or my 3 different email accounts (hotmail,gmail,yahoo) don't receive them and therefore don't even put them in the spam folder.

Code:

<?php
    $mailto = 'xxx@example.com';
    $subject = 'the subject';
    $message = 'the message';
    $from = 'system@example.net';
    $header = 'From:'.$from;

    if(mail($mailto,$subject,$message,$header)) {
        echo 'Email on the way';
    }
?>

Everytime it outputs 'Email on the way' so mail() returns true, right? I really don't get it, I've even tried to turn off my little snitch (although I didn't block SMTP).

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Rivers
  • 211
  • 1
  • 2
  • 3
  • 3
    Are you testing this on your local computer or on a real web host? If it's on your local machine, the chances are that it's not configured to send/receive mail. – itsols Jun 19 '11 at 16:53
  • How is sendmail set up in your php.ini file? And is this on *nix or Windows? – ETWW-Dave Jun 19 '11 at 16:54
  • @ETWW-Dave: I dare you to find actual `sendmail` on a recent computer ;) But yes, I'd suspect that the e-mail is getting rejected by the next SMTP server. – Piskvor left the building Jun 19 '11 at 17:01
  • 1
    @Piskvor I manage about 30 recent-ish servers that all run actual `sendmail`... :) However, yes, regardless of what the SMTP agent in use is, the `php.ini` setting is still to this day called `sendmail_path` – ETWW-Dave Jun 19 '11 at 18:18

7 Answers7

14

See this article by Jeff Atwood.

In short: Just because your code has handed the e-mail to a Mail Transfer Agent, it doesn't mean it will be delivered. Yes, mail() returning true means "accepted for delivery" - which means "Looks like an e-mail, I'll try to deliver this", not "It is delivered". Even the manual for mail() says:

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.

Soooo: check your MTA (is the e-mail sent from your local computer?), try to send to a local address (if the address is local, does it get delivered?), try to send an e-mail from your mail client, using the same settings as your PHP script, try to send to a smaller mail-hoster which allows you tu switch off antispam (is it delivered outside your network?). Also, read that article, and check the points mentioned there.

Edd
  • 3,724
  • 3
  • 26
  • 33
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
5

Maybe your server is not configured to handle mail().

<?php
    print phpinfo();  
?>

and look at sendmail_path

Community
  • 1
  • 1
DENIEL
  • 185
  • 1
1

You may need to add correct end of line characters to the Headers. It may be \n or \r\n

Brombomb
  • 6,988
  • 4
  • 38
  • 57
0

Check your phpinfo and/or php.ini for your mail settings and make sure you can send mail with whatever program php is trying to use. The function will succeed if the command executes but doesn't know if the mail actually went out.

ldg
  • 9,112
  • 2
  • 29
  • 44
0

Check your mail server's mail log. On Unix-ish systems, it's generally /var/log/maillog. On Windows, who knows, but there should be a log somewhere. If mail is returning TRUE, then whatever mail server it's connecting to has accepted the mail for eventual delivery. After that, mail() is no longer involved in any way and it's up to the SMTP servers to do the actual delivery.

In real world terms, mail() is you walking a letter down the block and dropping it into a mail box. Everything after that is utterly outside of PHP's scope and control.

Marc B
  • 356,200
  • 43
  • 426
  • 500
-1

I had the same problem on Ubuntu and I resolved it following the next tutorial:

http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/

I hope it works for you also.

mgrdiez
  • 67
  • 1
  • 10
-1

If this is a linux server it's probably set up to send to the local mail queue. When I had this problem I got it working by adding an MX entry on the DNS server used by the linux servers which pointed to our ISP's mail server.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148