用于发送邮件的 PHP 脚本在通过浏览器执行时不起作用,但在从命令行执行时工作正常

标签 php linux email xampp privileges

我写了一些 PHP 脚本:

<?php
$phpPath = shell_exec("which php");

print "$phpPath\n";
 
$uid =  posix_getuid();
$userinfo = posix_getpwuid($uid);
print_r($userinfo );
print "\n";

    $to = "my_user_name@my_company_mail.com";
    $subject = "Test mail";
    $message = "Hello! This is a simple email message.";
    $from = "do-not-reply@akamai.com";
    $headers = "From:" . $from;

    $res = mail($to,$subject,$message,$headers);

    print_r(error_get_last());
    if($res){
    echo "Mail Sent.\n";
    }else{
    echo "Mail was'nt Sent\n";

    }
?>

让我发疯的是,当我从命令行执行这个脚本时,它工作正常:

/usr/bin/php

Array
(
    [name] => daemon
    [passwd] => x
    [uid] => 1
    [gid] => 1
    [gecos] => daemon
    [dir] => /usr/sbin
    [shell] => /bin/sh
)

Mail Sent.

但是当我从远程浏览器执行它时,我得到的是:

/usr/bin/php

Array
(
    [name] => daemon
    [passwd] => x
    [uid] => 1
    [gid] => 1
    [gecos] => daemon
    [dir] => /usr/sbin
    [shell] => /bin/sh
)

Mail was'nt Sent

有什么想法吗?

最佳答案

Command Line PHP 和 PHP 不使用相同的 php.ini,请检查它们之间是否存在差异。

More information on php.ini here

Related question on SO here

关于用于发送邮件的 PHP 脚本在通过浏览器执行时不起作用,但在从命令行执行时工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19488819/

相关文章:

php - 搜索关键字并应用过滤器

PHP - 行计数多次显示相同数字的问题,我该如何解决这个问题?

php - 联系表单 PHP 重定向不起作用

email - Mutt 发送签名 Hook

linux - 启动时重新启动 autossh 反向隧道的问题

email - 使用 gomail.v2 创建一个新的 *Dialer

php - 如何在 php 中运行或加载 .po/.mo 文件进行本地化

php - 谷歌地图、PHP 和 MySQL : Create multiple markers on map using various options together

C 标准 I/O 与 UNIX I/O 基础

linux - 在 Linux 中创建系统调用