I am using php\'s mail() function for the simple process of E-Mailing the input of a contact form to the respective person. The strange thing is that the form always used to
mail() function returning false, but with no error
Welcome to PHP!
Does this seem like an error with the host config, or is it my code?
Who knows? mail()
is a black box from which you will find no useful information when something goes wrong.
When asked about this, they recommended I use the smtp relay...
Indeed, you probably should. Take a good look at SwiftMailer, an excellent, comprehensive, modern PHP mailing library that can speak directly to that SMTP server. It excels at building MIME messages, like the one you seem to have painstakingly put together above.
Other popular options include PEAR's Mail, Zend Framework's Zend_Mail, and the classic of classics, PHPMailer.
Sounds like your host disabled mail()
, you should look into using SMTP to send mail, a good PHP mail class such as SwiftMailer will allow you to send mail via SMTP easily.
You shoud take a look at the mail logs. They hold the answer. You probably have to wrestle with your hosting company for the logs though.
Check if web is allowed to send mail by giving
getsebool httpd_can_sendmail
from terminal. If the output is
httpd_can_sendmail --> off
give https the permission for sending mail by issuing setsebool httpd_can_sendmail 1
command. You must have root permission for issuing these commands.
I had a similar problem and my site is hosted by shared hosting service. I could figure out the issue by fixing the header. The problem was:
$headers .= "From: $fname\r\n";
You used user email in the from field. My hosting only accepts emails with my domain name for this field. So I replaced it with this:
$headers = "From: webmaster@yourdomain.com" ."\r\n" ;
And it fixed the problem! PHP mail() works fine after that.
More likely a host config. I think (but may be wrong) that mail() use the server mail command. So if you have no sendmail/postfix/ssmtp or other MTA installed on the server it cannot work.
If they told you to address a SMTP server directly you should use another library that implements the SMTP protocol and a Mail class to build mail and send it via SMTP directly (in PEAR or Zend Framework PHP classes you'll find that)