8

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 process the E-Mails, but one day this all stopped, now the function returns false, but gives no error at all.

The site is on a shared host. When asked about this, they recommended I use the smtp relay xx.xxx.x.xxx

Correct me if I am wrong, but the mail() function does not provide provisions for this does it? Surely it is up to the HOST machine to have it's relays configured correctly?

My question is this: Does this seem like an error with the host config, or is it my code? Here is a sample of the mail code I have used:

$to = "[email protected]"; //to who?
$subject = "Website Contact: $mysubject";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "From: $fname<$email1>\r\n";
$headers .= "Reply-To: $email1\r\n";
$headers .= "Return-Path:$email1\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
$msg2 = nl2br($msg);

$send = mail($to, $subject, $msg2, $headers); //process mail

if(!$send):
  //error stuff here
endif;

Many thanks, Simon

@eisberg - I use a custom error handler like this:

//error handler function
function customError($errno, $errstr){
$err = "\n".date('Y-m-d H:m:s')." Error: [$errno] $errstr";
$fh = fopen("errlog.txt", 'a+');
fwrite($fh, $err);
fclose($fh);
}
set_error_handler("customError", E_ALL);

Would that mean I need to change set_error_handler("customError", E_ALL); to set_error_handler("customError", -1); ?

5
  • try error_reporting(-1); above your code. I hope you get a usable message. Source: php.net/manual/en/function.error-reporting.php
    – eisberg
    Mar 31, 2011 at 7:39
  • @eisberg - I have a custom error function logging all errors (E_ALL) to file, and still nothing! It picks up the most finicky errors, but still nothing mail() related. Mar 31, 2011 at 7:47
  • E_ALL is not -1 in all PHP versions :-)
    – eisberg
    Mar 31, 2011 at 7:50
  • @eisberg - See my response to error handling in my question above, I needed to format the code, hence added it to question. Mar 31, 2011 at 8:53
  • I just meant that -1 will give you control over every error in every PHP version. If you do not get an error with -1 there is a good chance something is wrong with your config.
    – eisberg
    Mar 31, 2011 at 10:54

7 Answers 7

21

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.

3
  • 1
    +1 for recommending SwiftMailer, without a doubt the most complete PHP Mailer library available and under active development!
    – ChrisR
    Mar 31, 2011 at 7:28
  • Black box of blackness, haha.
    – Adam Arold
    Mar 31, 2011 at 7:30
  • 1
    SwiftMailer is what I have decided to run with, taken a look at the lib and docs, and it is as simple to use as php's mail() function is to break! Mar 31, 2011 at 7:41
7

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.

1
  • thanks for that helpful resource. It sure seems like a better approach all round. At least I can rely on the clients SMTP account details asopposed to relying on a shared server that's buggy as hell. Thanks once again. Mar 31, 2011 at 7:28
2

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.

3
  • let's not even go there, they say that "They cannot directly access them" which means they're probably selling hosting space that's on a server at the other end of the world and they actually have no idea what they're doing. SMTP approach it will be. Mar 31, 2011 at 7:36
  • @webfac In that case SMTP is just fine :)
    – vbence
    Mar 31, 2011 at 7:52
  • 1
    in my case, I found in file \mail\dovecot.index.cache the error "Mail failure - no recipient addresses" (addresses were taken from DB, but were empty strings)
    – Atara
    Feb 25, 2021 at 11:39
1

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)

1

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.

1

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: [email protected]" ."\r\n" ;

And it fixed the problem! PHP mail() works fine after that.

0

I recently figured this out after months of head scratching.

Php mail() has a limit of 70 characters per line. use wordwrap() around your message and this will prevent it from over running the line and it will work every time (or atleast not fail because of this issue).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.