Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

email - PHP mail() function returning false, but with no error

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
";
$headers .= "From: $fname<$email1>
";
$headers .= "Reply-To: $email1
";
$headers .= "Return-Path:$email1
";
$headers .= "Content-Type: text/html; charset=UTF-8
";
$headers .= "Content-Transfer-Encoding: quoted-printable
";
$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 = "
".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); ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...