php獲取錯誤信息函數,關于php:如何獲取mail()函數的錯誤消息?

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

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

就像是

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

if($this_mail) echo 'sent!';

else echo error_message;

謝謝!

當mail()返回false時,可以使用error_get_last()。

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

if (!$success) {

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

}

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

[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()

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

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

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

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

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

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

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

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

根據這篇文章

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.

我確認這一點是因為在嘗試在我的PHP腳本中使用mail()失敗之后,結果發現我的計算機上未安裝sendmail,但是php.ini變量sendmail_path為/usr/sbin/sendmail -t -i

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

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

3-現在,如果任何PHP mail()函數失敗,我會發現/var/mail/目錄下記錄的sendmail程序錯誤。每個用戶1個文件

例如,此片段摘自我的/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)

我的系統是帶有apache2.4和PHP 7.2的linux Fedora 28

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

$e=error_get_last();

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

// An error function

}

error_get_last(); -返回上一次發生的錯誤

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

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

嘗試這個。如果我對任何文件有任何錯誤,那么我的電子郵件ID上會出現錯誤郵件。創建兩個文件index.php和checkErrorEmail.php,并將它們上傳到您的服務器。然后使用瀏覽器加載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);?

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388657.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388657.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388657.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

關于夏季及雷雨天氣的MODEM、路由器使用注意事項

每年夏季是雷雨多發季節,容易出現家用電腦因而雷擊造成電腦硬件的損壞和通訊故障,為了避免這種情況的的發生,保護您的財產不受損失(一般雷擊照成損壞的設備是沒得保修的),建議您繼續閱讀下面內容&#xff1…

創建Console應用程序,粘貼一下代碼,創建E://MyWebServerRoot//目錄,作為虛擬目錄,親自測試通過,

創建Console應用程序,粘貼一下代碼,創建E://MyWebServerRoot//目錄,作為虛擬目錄,親自測試通過, 有一個想法,調用ASP.DLL解析ASP,可是始終沒有找到資料,有待于研究,還有…

c#對文件的讀寫

最近需要對一個文件進行數量的分割,因為數據量龐大,所以就想到了通過寫程序來處理。將代碼貼出來以備以后使用。 //讀取文件的內容 放置于StringBuilder 中 StreamReader sr new StreamReader(path, Encoding.Default); String line; StringBuilder sb …

php表格tr,jQuery+ajax實現動態添加表格tr td功能示例

本文實例講述了jQueryajax實現動態添加表格tr td功能。分享給大家供大家參考,具體如下:功能:ajax獲取后臺返回數據給table動態添加tr/tdhtml部分:ajax部分:var year $(#year).val();//下拉框數據var province $(#prov…

maya的簡單使用

1、導出obj類型文件window - settings preferences - plug- in Manager objExport.mllfile - export selection就有OBJ選項了窗口-設置/首選項- 插件管理 objExport.mll文件-導出當前選擇2、合并元素在文件下面的下拉框,選擇多邊形。按住shift鍵&…

ai前沿公司_美術是AI的下一個前沿嗎?

ai前沿公司In 1950, Alan Turing developed the Turing Test as a test of a machine’s ability to display human-like intelligent behavior. In his prolific paper, he posed the following questions:1950年,阿蘭圖靈開發的圖靈測試作為一臺機器的顯示類似人類…

查看修改swap空間大小

查看swap 空間大小(總計): # free -m 默認單位為k, -m 單位為M   total used free shared buffers cached  Mem: 377 180 197 0 19 110  -/ buffers/ca…

關于WKWebView高度的問題的解決

關于WKWebView高度的問題的解決 IOS端嵌入網頁的方式有兩種UIWebView和WKWebView。其中WKWebView的性能要高些;WKWebView的使用也相對簡單 WKWebView在加載完成后,在相應的代理里面獲取其內容高度,大多數網上的方法在獲取高度是會出現一定的問題&#xf…

測試nignx php請求并發數,nginx 優化(突破十萬并發)

一般來說nginx 配置文件中對優化比較有作用的為以下幾項:worker_processes 8;nginx 進程數,建議按照cpu 數目來指定,一般為它的倍數。worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;為每個進…

多米諾骨牌v.1MEL語言

// // //Script Name:多米諾骨牌v.1 //Author:瘋狂小豬 //Last Updated: 2011.10.5 //Email:wzybwj163.com // //---------------------------------------------------------------------------- //-----------------------------------------------------------------…

THINKPHP3.2視頻教程

http://edu.51cto.com/lesson/id-24504.html lunix視頻教程 http://bbs.lampbrother.net/read-htm-tid-161465.html TP資料http://pan.baidu.com/s/1dDCLFRr#path%252Fthink 微信開發,任務吧,留著記號了

mardown 標題帶數字_標題中帶有數字的故事更成功嗎?

mardown 標題帶數字統計 (Statistics) I have read a few stories on Medium about writing advice, and there were some of them which, along with other tips, suggested that putting numbers in your story’s title will increase the number of views, as people tend …

897. 遞增順序查找樹-未解決

897. 遞增順序查找樹 https://leetcode-cn.com/contest/weekly-contest-100/problems/increasing-order-search-tree/ package com.test;import java.util.ArrayList; import java.util.Collections; import java.util.List;/*** author stono* date 2018/9/2* 897. 遞增順序查…

Azure PowerShell (16) 并行開關機Azure ARM VM

《Windows Azure Platform 系列文章目錄》 并行開機腳本: https://github.com/leizhang1984/AzureChinaPowerShell/blob/master/ARM/2StartAzureARMVM/StartAzureRMVM.txt 并行關機腳本: https://github.com/leizhang1984/AzureChinaPowerShell/blob/mas…

使用Pandas 1.1.0進行穩健的2個DataFrames驗證

Pandas is one of the most used Python library for both data scientist and data engineers. Today, I want to share some Python tips to help us do qualification checks between 2 Dataframes.Pandas是數據科學家和數據工程師最常用的Python庫之一。 今天,我…

Maya開發

Maya開發(一)-- 緒論 (翻譯自Maya官方文檔)2008-05-09 15:33 緒論 Autodesk Maya 是一個開放的產品,就是說任何Autodesk以外的人都可以改變Maya現有的特征,或者 增加新的特性.你可以用兩個方法來修改MAYA: ME…

織夢在線報名平臺php,DedeCMSv5

DedeCMS v5國內專業的PHP網站內容管理系統-織夢內容管理系統v5.8 Roadmap狀態 ? 已完成 🔨 進行中 ? 未完成項目開發可以到織夢開發問題管理中進行交流反饋。🔨 調整DedeCMS目錄結構,將原有include中外部訪問的內容遷移出去;&am…

pom.xml文件詳解

<project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <!-- 父項目的坐…

軟件工程第一次作業

&#xff08;1&#xff09;回想一下你初入大學時對計算機專業的暢想 當初你是如何做出選擇計算機專業的決定的&#xff1f; 當初選擇計算機專業是因為之前看大佬們參加信息競賽&#xff0c;覺得很厲害、很有意思&#xff0c;而且也希望能自己做一款游戲出來&#xff0c;所以就選…

置信區間的置信區間_什么是置信區間,為什么人們使用它們?

置信區間的置信區間I’m going to try something a little different today, in which I combine two (completely unrelated) topics I love talking about, and hopefully create something that is interesting and educational.今天&#xff0c;我將嘗試一些與眾不同的東西…