PHP mail () function returns false, but without errors

I use the php mail () function for a simple e-mailing process of entering the contact form to the appropriate person. The strange thing is that the form has always been used to process E-Mails, but once it all stopped, now the function returns false, but does not give any error.

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

Correct me if I am wrong, but the mail () function does not provide a provision for this? Surely the HOST device needs the correct relay configuration?

My question is: does this look like an error with the host configuration, or is this my code? Here is an example of the postal code used:

$to = " xxx@xxx.co.za "; //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; 

Thanks a lot, Simon.

@eisberg . I use my own error handler as follows:

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

This means that I need to change set_error_handler("customError", E_ALL); on set_error_handler("customError", -1); ?

+6
php email
source share
6 answers

The mail () function returns false, but without errors

Welcome to PHP!

Does this look like an error in the host configuration, or is this my code?

Who knows? mail() is a black box from which you will not find any useful information if something goes wrong.

When asked about this, they recommended using smtp relays ...

Indeed, you probably should do this. Take a look at SwiftMailer , a great, comprehensive, state-of-the-art PHP mailing list library that can speak directly to this SMTP server. It excels at creating MIME messages, like the one you seem to have carefully compiled above.

Other popular options include PEAR Mail , Zend Framework Zend_Mail, and the classic classics PHPMailer .

+17
source share

It looks like your host has mail() disabled, you should study using SMTP to send mail, a good PHP mail class like SwiftMailer will let you send mail through SMTP easily.

+7
source share

Most likely the host configuration. I think (but it may be wrong) that mail () uses the mail server command. Therefore, if you do not have a sendmail / postfix / ssmtp server or another MTA installed on the server, it will not be able to work.
If they told you to contact the SMTP server directly, you should use another library that implements the SMTP protocol and the Mail class to create mail and send it via SMTP directly (you will find this in the PEAR or Zend Framework PHP classes)

+1
source share

You can see mail logs. They answer. However, you may have to fight with your hosting company for magazines.

+1
source share

Check if the web is allowed to send mail, getsebool httpd_can_sendmail from the terminal. If output

 httpd_can_sendmail --> off 

grant https permission to send mail by sending the setsebool httpd_can_sendmail 1 . You must have root permission to issue these commands.

0
source share

I had a similar problem and hosting was hosted on my site. I could figure out the problem by setting the header. The problem was this:

$ headers. = "From: $ fname \ r \ n";

You used the user's email in the from field. My hosting only accepts emails with my domain name for this field. So I replaced it with the following:

 $headers = "From: webmaster@yourdomain.com " ."\r\n" ; 

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

0
source share

All Articles