日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php获取错误信息函数,关于php:如何获取mail()函数的错误消息?

發(fā)布時(shí)間:2023/11/29 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php获取错误信息函数,关于php:如何获取mail()函数的错误消息? 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我一直在使用PHP mail()函數(shù)。

如果郵件由于任何原因未發(fā)送,我想回顯錯(cuò)誤消息。 我該怎么做?

就像是

$this_mail = mail('example@example.com', 'My Subject', $message);

if($this_mail) echo 'sent!';

else echo error_message;

謝謝!

當(dāng)mail()返回false時(shí),可以使用error_get_last()。

$success = mail('example@example.com', 'My Subject', $message);

if (!$success) {

$errorMessage = error_get_last()['message'];

}

使用print_r(error_get_last()),您將獲得如下內(nèi)容:

[type] => 2

[message] => mail(): Failed to connect to mailserver at"x.x.x.x" port 25, verify your"SMTP" and"smtp_port" setting in php.ini or use ini_set()

我認(rèn)為這僅在使用SMTP(Windows?)時(shí)有效。在Linux上,如果您使用" sendmail",則" mail()"函數(shù)只會(huì)返回該命令的退出狀態(tài):github.com/php/php-src/blob/PHP-5.6.25/ext/standard/mail.c# L404沒有可靠的方法來獲取錯(cuò)誤消息afaik。我嘗試使用以下腳本:gist.github.com/njam/a34ecd9ef195c37c8354ab58f7bfcc9b

error_get_last()返回NULL !!但是mail函數(shù)返回true!

為什么它的答案如此流行,但為什么它沒有引起人們的關(guān)注呢?我不知道人們會(huì)怎么完全想念它。

@ashleedawg-我什至不知道這怎么引起了這么多的投票。我從未見過-見過error_get_last()與phps本機(jī)mail()函數(shù)一起工作。實(shí)際上,我只是勉強(qiáng)設(shè)置了錯(cuò)誤的郵件,然后再嘗試一次以確保;我什么也沒得到。

用php發(fā)送郵件不是一個(gè)一步的過程。 mail()返回true / false,但是即使返回true,也并不意味著將要發(fā)送消息。所有mail()所做的就是將消息添加到隊(duì)列(使用sendmail或您在php.ini中設(shè)置的任何內(nèi)容)

沒有可靠的方法來檢查消息是否已在php中發(fā)送。您將不得不查看郵件服務(wù)器日志。

您可以使用具有相同接口的PEAR郵件程序,但是在出現(xiàn)問題時(shí)返回PEAR_Error。

就我而言,無論我做什么(error_get_last()或ini_set('display_errors',1);),我都無法在我的PHP腳本中收到錯(cuò)誤消息,也不顯示錯(cuò)誤消息

根據(jù)這篇文章

The return value from $mail refers only to whether or not your

server's mailing system accepted the message for delivery, and does

not and can not in any way know whether or not you are providing valid

arguments. For example, the return value would be false if sendmail

failed to load (e.g. if it wasn't installed properly), but would

return true if sendmail loaded properly but the recipient address

doesn't exist.

我確認(rèn)這一點(diǎn)是因?yàn)樵趪L試在我的PHP腳本中使用mail()失敗之后,結(jié)果發(fā)現(xiàn)我的計(jì)算機(jī)上未安裝sendmail,但是php.ini變量sendmail_path為/usr/sbin/sendmail -t -i

1-我從軟件包管理器shell> dnf install sendmail安裝了sendmail

2-我開始了它shell> service sendmail start

3-現(xiàn)在,如果任何PHP mail()函數(shù)失敗,我會(huì)發(fā)現(xiàn)/var/mail/目錄下記錄的sendmail程序錯(cuò)誤。每個(gè)用戶1個(gè)文件

例如,此片段摘自我的/var/mail/root文件

The original message was received at Sun, 29 Jul 2018 22:37:51 +0200

from localhost [127.0.0.1]

----- The following addresses had permanent fatal errors -----

(reason: 550 Host unknown)

我的系統(tǒng)是帶有apache2.4和PHP 7.2的linux Fedora 28

沒有與mail()函數(shù)關(guān)聯(lián)的錯(cuò)誤消息。關(guān)于是否接受電子郵件發(fā)送,僅返回true或false。不是最終決定是否交付,而是基本上域是否存在以及地址是否為有效格式的電子郵件地址。

$e=error_get_last();

if($e['message']!==''){

// An error function

}

error_get_last(); -返回上一次發(fā)生的錯(cuò)誤

您應(yīng)該在代碼中添加一些解釋,以免將來對他人有所幫助。如何回答

同意以前的評論。請修改您的答案以包含一些說明。純代碼的答案對教育未來的SO讀者幾乎沒有作用。您的答案在質(zhì)量不高的審核隊(duì)列中。

嘗試這個(gè)。如果我對任何文件有任何錯(cuò)誤,那么我的電子郵件ID上會(huì)出現(xiàn)錯(cuò)誤郵件。創(chuàng)建兩個(gè)文件index.php和checkErrorEmail.php,并將它們上傳到您的服務(wù)器。然后使用瀏覽器加載index.php。

的index.php

include('checkErrorEmail.php');

include('dereporting.php');

$temp;

echo 'hi '.$temp;

?>

checkErrorEmail.php

// Destinations

define("ADMIN_EMAIL","pradeep.callus7@hotmail.com");

//define("LOG_FILE","/my/home/errors.log");

// Destination types

define("DEST_EMAIL","1");

//define("DEST_LOGFILE","3");

/* Examples */

// Send an e-mail to the administrator

//error_log("Fix me!", DEST_EMAIL, ADMIN_EMAIL);

// Write the error to our log file

//error_log("Error", DEST_LOGFILE, LOG_FILE);

/**

* my_error_handler($errno, $errstr, $errfile, $errline)

*

* Author(s): thanosb, ddonahue

* Date: May 11, 2008

*

* custom error handler

*

* Parameters:

* ?$errno: ? Error level

* ?$errstr: ?Error message

* ?$errfile: File in which the error was raised

* ?$errline: Line at which the error occurred

*/

function my_error_handler($errno, $errstr, $errfile, $errline)

{

echo"errno".$errno.",errstr".$errstr.",errfile".$errfile.",errline".$errline;

if($errno)

{

error_log("Error: $errstr

error on line $errline in file $errfile

", DEST_EMAIL, ADMIN_EMAIL);

}

/*switch ($errno) {

case E_USER_ERROR:

// Send an e-mail to the administrator

error_log("Error: $errstr

Fatal error on line $errline in file $errfile

", DEST_EMAIL, ADMIN_EMAIL);

// Write the error to our log file

//error_log("Error: $errstr

Fatal error on line $errline in file $errfile

", DEST_LOGFILE, LOG_FILE);

break;

case E_USER_WARNING:

// Write the error to our log file

//error_log("Warning: $errstr

in $errfile on line $errline

", DEST_LOGFILE, LOG_FILE);

break;

case E_USER_NOTICE:

// Write the error to our log file

// error_log("Notice: $errstr

in $errfile on line $errline

", DEST_LOGFILE, LOG_FILE);

break;

default:

// Write the error to our log file

//error_log("Unknown error [#$errno]: $errstr

in $errfile on line $errline

", DEST_LOGFILE, LOG_FILE);

break;

}*/

// Don't execute PHP's internal error handler

return TRUE;

}

// Use set_error_handler() to tell PHP to use our method

$old_error_handler = set_error_handler("my_error_handler");

?>

什么是include(dereporting.php);?

正如其他人所說,發(fā)送郵件沒有錯(cuò)誤跟蹤,它返回將郵件添加到傳出隊(duì)列的布爾結(jié)果。如果要跟蹤真正的成功失敗,請嘗試將SMTP與郵件庫(如Swift Mailer,Zend_Mail或phpmailer)一起使用。

總結(jié)

以上是生活随笔為你收集整理的php获取错误信息函数,关于php:如何获取mail()函数的错误消息?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。