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).
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.You may need to add correct end of line characters to the Headers. It may be
\n
or\r\n
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 formail()
says: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.
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.
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.Maybe your server is not configured to handle
mail()
.and look at
sendmail_path