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.mzph.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;我将尝试一些与众不同的东西…