7.用邮件发送错误日志

参考网址:

 http://php.net/manual/zh/function.error-log.php
【可以解决“怎么发送错误日志到邮件”问题】


http://bbs.csdn.net/topics/330204372

【可以解决问题

“Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:AppServwwwmail.php on line 15”


【可以解决  “php发送email (邮件)若干问题总结(成功smtp案例见附件)”问题】




error_log

(PHP 4, PHP 5, PHP 7)

error_log发送错误信息到某个地方

说明

bool error_log ( string $message [, int $message_type = 0 [, string $destination [, string $extra_headers ]]] )

把错误信息发送到 web 服务器的错误日志,或者到一个文件里。

参数

message

应该被记录的错误信息。

message_type

设置错误应该发送到何处。可能的信息类型有以下几个:

error_log() 日志类型
0message 发送到 PHP 的系统日志,使用 操作系统的日志机制或者一个文件,取决于 error_log 指令设置了什么。 这是个默认的选项。
1message 发送到参数 destination 设置的邮件地址。 第四个参数 extra_headers 只有在这个类型里才会被用到。
2 不再是一个选项。
3message 被发送到位置为 destination 的文件里。 字符 message 不会默认被当做新的一行。
4message 直接发送到 SAPI 的日志处理程序中。
destination

目标。它的含义描述于以上,由 message_type 参数所决定。

extra_headers

额外的头。当 message_type 设置为 1 的时候使用。 该信息类型使用了 mail() 的同一个内置函数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

注释

Warning

error_log() 并非二进制安全的。null 字符可能截断 message

Tip

message 不能包含 null 字符。 注意,message 可能会发送到文件、邮件、syslog 等。 所以在调用 error_log() 前需要使用适合的转换/转义函数: base64_encode()rawurlencode()addslashes()

范例

Example #1 error_log() 范例

<?php
// 如果无法连接到数据库,发送通知到服务器日志
if (!Ora_Logon($username$password)) {
    
error_log("Oracle database not available!"0);
}

// 如果用尽了 FOO,通过邮件通知管理员
if (!($foo allocate_new_foo())) {
    
error_log("Big trouble, we're all out of FOOs!"1,
               
"operator@example.com");
}

// 调用 error_log() 的另一种方式:
error_log("You messed up!"3"/var/tmp/my-errors.log");
?>


 
 



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

问题一

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

解决方法:

PHP的Mail函数使用的是SMTP服务发出E-Mail的。也就是说你需要在php.ini里配置好适当的SMTP服务器地址和端口信息。如果你需要在本机上发出E-Mail,即

SMTP = localhost
smtp_port = 25
这样的话,你需要在你自己的本机上配置上SMTP服务才可以。你可以尝试打开IIS的SMTP服务。
问题二

mail(): SMTP server response: 550 5.7.1 Unable to

解决方法:
关键一步:我的电脑->管理->服务和应用程序 ->SMTP虚拟服务器上点击右键,在弹出的属性窗口里进行如下设置:
点击访问选项卡,再点击中继,在弹出的窗口出点击添加,然后选单台计算机,添加IP地址为 127.0.0.1。

这时就可以测试一下了..如果不可以继续往下操作...

还有的网友说要修改php.ini文件,做成如下配 置,但我觉得是没有必要的,我用的默认设置就可以,如果您经过以上设置还不可以发送的话,就再修改一下吧,注意修改后要重启web服务器:
php.ini的设置: 可使用默认选项,不用修改
SMTP = localhost      //默认,不用修改
smtp_port = 25        //默认,不用修改
sendmail_from=你的设定值  //如果不指定,就必须在程序中指定

这样就可以使用mail函数了
<?php
$to = "***@163.com";  //改成自己的邮箱进行测试
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "shf@qq.com";  //随意设置
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

 注意:收不到邮件还有另一个原因就 是目标邮件系统服务商的设置,有的当成正常邮件,有的当成垃圾邮件,还有的直接不接收,如果一个邮箱不成功的话,建议换个邮件服务提供商试试。据我测试 gmail 163可以收到,qq有时收不到。

小结:
这种“Unable to relay user@externaldomain.com”的错误通常属于设计意图。也就是说,为了防止Internet上的Unsolicited Commercial E-Mail (UCE),Microsoft的SMTP服务,默认,是不允许一封邮件通过它中继到外面的邮件地址的!

原文地址:https://www.cnblogs.com/youyuanjuyou/p/8088941.html