【问题标题】:Debian can't send mail from PHPDebian 无法从 PHP 发送邮件
【发布时间】:2014-02-01 15:29:28
【问题描述】:

邮件程序错误:无法执行:/usr/sbin/sendmail

我用的是debian服务器,文件权限是777(all allowed),所以不能执行,这是为什么?

//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('admin@test.com', 'test');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($_POST['email'], $_POST['name']);
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("from test");
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.gif');

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

【问题讨论】:

  • Dis 不是权限问题,因为您没有收到权限被拒绝错误
  • 任何 777 模式都是严重的安全问题。您应该恢复到系统的早期备份,或者从头开始重新安装。
  • @tripleee sendmail 默认为 777,放心吧!没有人从头开始重装系统,就因为你chmod一个文件?你是认真的吗?
  • 如果系统二进制文件是世界可写的,你不知道你是否已经烤好了。符号链接显示为 lrwxrwxrwx,但实际目标必须 系统上的每个用户都可写。
  • @tripleee 无论您说什么,都与我的回答无关。事实是——它是可写的。我的建议是让它默认,不要让它成为我喜欢的错误方式。它是777 的原因是未知的(至少对我来说,755 绰绰有余)。我可以理解,如果它只是符号链接但它出现(通过查看包),例如sendmail-bin_8.14.4-2.1ubuntu4_amd64.deb 二进制文件也被 chmod 为 777。无论如何,感谢您指出!

标签: php debian phpmailer


【解决方案1】:

在 Ubuntu 中,sendmail 默认不安装。您必须手动安装它:

sudo apt-get install sendmail-bin

编辑 1:

如果您使用的是 PHPMailer,您可以使用以下方法设置 Sendmail 路径:

$mail->Sendmail     = '/usr/sbin/sendmail';

很容易测试问题是在 PHP 代码中还是在您的邮件服务器配置中,甚至可能是防火墙。尝试从命令行运行,看看是否收到您的电子邮件:

/usr/sbin/sendmail -v my@address.com < email.test

此外,您实际上可以收到邮件,但它可以放在 SPAM 文件夹中,因此请检查那里的邮件。

编辑 2:

还有一件事是你应该安装sendmailconfig然后运行它来配置它:

sudo sendmailconfig

阅读有关在 Ubuntu 上配置 sendmail 的更多信息:sendmail: how to configure sendmail on ubuntu?

【讨论】:

  • 我已经安装了 sudo apt-get install sendmail-bin,但仍然出现同样的错误,属性是 777 允许所有,我正在使用 PHPMailer,我也用邮件尝试过() 函数都给出相同的错误
  • 你在使用 PHPMailer 吗?
  • -1 你必须 chmod 777 任何东西。这些不是生产系统上任何东西的“适当”权限。
  • @tripleee 供您参考,我的朋友,我知道,问题是 sendmail 默认为 777! (应该)。不要相信我的话,继续运行ls -lsa /usr/sbin/sendmail,作为回报你会看到lrwxrwxrwx.
  • @tripleee,我已经更新了我的答案。我个人认为它不应该是 777,因为它是开销,但它就是这样。如果您没有安装它,只需从pkgs.org 下载一个包并自己解决。
【解决方案2】:

直接的问题似乎是您的系统上没有安装/usr/sbin/sendmail。有多个 MTA 提供此功能,因此无需特别安装 Sendmail 套件;事实上,我会反对它,支持 Postfix 或一些非常简单的 MTA,例如 smtpdProvides: sendmail 应该做的任何包。

另一个需要指出的问题是chmod 777 权限。你绝对不应该在生产系统上做任何可写的东西。 PHP 脚本的正确权限是755775,如果您可以信任该组。 httpd 进程当然不需要能够写入脚本——实际上,绝对不允许向脚本文件写入任何内容。

【讨论】:

  • 二进制可执行文件的最佳权限应为 750(所有者为 7,组为 5,其他人为 0)
  • 对于私有脚本,750 有意义;但是系统脚本显然需要每个人都可以阅读和执行。与往常一样,最重要的是您需要了解权限的含义。
  • 您也想了解权限和所有权。如果系统需要执行,那么您可能想要更改脚本的所有权,而不是权限。查看需要哪个用户运行它(是 root / www-data / postfix 等)并更改所有权以适应 750。777 是非常糟糕的做法,这意味着即使是访客用户也可以执行它。
猜你喜欢
  • 2013-08-14
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 2013-01-24
  • 2012-08-31
  • 1970-01-01
相关资源
最近更新 更多