PHP sendmail 可以在 Ubuntu 命令行中使用,但不能在 php 文件中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6590934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:48:46  来源:igfitidea点击:

PHP sendmail works in Ubuntu command line, but not from a php file

phpapacheubuntusendmail

提问by Tim

I installed sendmail with PHP and apache on Ubuntu. When I try the following command-line

我在 Ubuntu 上用 PHP 和 apache 安装了 sendmail。当我尝试以下命令行时

php -r "mail('[email protected]', 'test', 'test')"'

it successfully sends the email.

它成功发送电子邮件。

However, running the file "test_send_mail.php" with:

但是,使用以下命令运行文件“test_send_mail.php”:

<?php
mail('[email protected]', 'test', 'test')
?>

doesn't send an email.

不发送电子邮件。

The unsuccessful attempt with the .php file generates the log entry:

使用 .php 文件失败的尝试会生成日志条目:

`Jul  5 21:24:47 www sendmail[25603]: p661OlL7025603: from=www-data, size=106, class=0,

nrcpts=0, msgid=<[email protected]>, relay=www-data@localhost

nrcpts=0,msgid=<[email protected]>,relay=www-data@localhost

The successful attempt with the command line generates the log entry:

使用命令行成功尝试生成日志条目:

    Jul  5 21:22:40 www sm-mta[25533]: p661MevV025533: from=<[email protected]>, 
size=352, class=0, nrcpts=1, msgid=<[email protected]>, 
proto=ESMTP, daemon=MTA-v4, relay=localhost [127.0.0.1]

Does anyone have any idea what might be happening? Thank you for your help!

有谁知道可能会发生什么?感谢您的帮助!

回答by RobertPitt

PHP Has separate inifiles depending on the environment:

PHPini根据环境有单独的文件:

  • cli/php.ini
  • cgi/php.ini
  • php.ini
  • cli/php.ini
  • cgi/php.ini
  • 配置文件

Make sure you have made all the appropriate changes in all the files, cgi is usually used for Nginx and Lighttpd, but replicate the settings in all 3 to be sure.

确保您已对所有文件进行了所有适当的更改,cgi 通常用于 Nginx 和 Lighttpd,但要确保复制所有 3 个文件中的设置。

Also you can run the phpinfo();function to see what settings are actually being used.

您也可以运行该phpinfo();功能以查看实际使用了哪些设置。

also this may be the direct fix: PHP mail issue with www-data

这也可能是直接的解决方法:PHP mail issue with www-data

回答by staticsan

Your unsuccessful attempt is sending email as the web server's user. You probably don't want to do that. The key is to pass more parameters to mail()so as to override these sorts of defaults as it hands the email to your injector.

您的失败尝试是以 Web 服务器的用户身份发送电子邮件。你可能不想这样做。关键是传递更多参数以mail()覆盖这些类型的默认值,因为它将电子邮件传递给您的注入器。

Look in the comments on the man page for mail()and there will be plenty of help to do that.

查看手册页上的评论,mail()会有很多帮助来做到这一点。