PHP Conference Nagoya 2025

运行时配置

这些函数的行为受 php.ini 中的设置影响。

Mail 配置选项
名字 默认 可修改范围 更新日志
mail.add_x_header "0" INI_PERDIR  
mail.log NULL INI_SYSTEM|INI_PERDIR  
mail.force_extra_parameters NULL INI_SYSTEM|INI_PERDIR  
SMTP "localhost" INI_ALL  
smtp_port "25" INI_ALL  
sendmail_from NULL INI_ALL  
sendmail_path NULL INI_SYSTEM  
有关 INI_* 样式的更多详情与定义,见 配置可被设定范围

这是配置指令的简短说明。

mail.add_x_header bool

添加 X-PHP-Originating-Script,将会包含脚本的 UID 以及文件名。

mail.log string

记录所有调用 mail() 的日志文件路径。日志条目包含脚本的完整路径、行号、To 地址和报头。

mail.force_extra_parameters string

强制添加指定参数作为额外参数传递给 sendmail 二进制文件。这些参数将始终代替 mail() 第五个参数的值。

SMTP string

仅用于 Windows:PHP 在 mail() 函数中用来发送邮件的 SMTP 服务器的主机名称或者 IP 地址。

smtp_port int

仅用于 Windows:使用 mail() 发送邮件时,连接到使用 SMTP 的指定服务器的端口号,默认为 25。

sendmail_from string

通过 STMP 直接用 PHP 发送邮件时的 "From:" 邮件地址的值(仅限 Windows)。该选项同时设置了 "Return-Path:" 头。

sendmail_path string

sendmail 程序的路径,通常为 /usr/sbin/sendmail/usr/lib/sendmailconfigure 脚本会尝试找到该程序并设定为默认值,但是如果失败的话,可以在这里设定。

不使用 sendmail 的系统应将此指令设定为其邮件系统提供的 sendmail 替代程序,如果有的话。例如,» Qmail 用户通常可以设为 /var/qmail/bin/sendmail/var/qmail/bin/qmail-inject

qmail-inject 不需要任何选项就能正确处理邮件。

此指令也可用于 Windows。如果设定,smtpsmtp_portsendmail_from 都被忽略并运行指定的命令。

添加备注

用户贡献的备注 4 notes

up
26
elitescripts2000 at yahoo dot com
10 years ago
On Ubuntu 13.04, not sure of the other Distros.

If you simply uncomment the default:

sendmail_path = "sendmail -t -i"

Your mail() functions will all fail. This is because, you should place the FULL PATH (i.e. /usr/sbin/sendmail -t -i )

The documentation states PHP tries it's best to find the correct sendmail path, but it clearly failed for me.

So, always enter in the FULLPATH to sendmail or you may get unexpected failing results.

As a secondary note: Those that just want to ENFORCE the -f parameter, you can do so in php.ini using:

mail.force_extra_parameters = -fdo_not_reply@domain.tld

You can leave the sendmail path commented out, it will still use the defaults (under UNIX -t -i options which if you look them up are very important to have set)....

But, now there is no way to change this, even with the 5th argument of the mail() function. -f is important, because if NOT set, will be set to which ever user the PHP script is running under, and you may not want that.

Also, -f sets the Return-Path: header which is used as the Bounce address, if errors occur, so you can process them. You you can not set Return-Path: in mail() headers for some reason... you could before. Now you have to use the -f option.
up
3
jscholz at wisc dot edu
2 years ago
The documentation should be made clear that sendmail does NOT default to -t -i when using just /usr/sbin/sendmail. You literally need to specify the options.

I know this might seem like a no-brainer but I wasted hours trying to get mail() to work only to discover that the sendmail program is NOT passed -t and -i by default as stipulated in the documentation.
up
-1
php dot net at ii0 dot net
8 years ago
If anyone gets this cryptic error message in the PHP error logs:
"sh: -t: command not found"
after upgrading from PHP 5.4, this may be the solution for you.

I upgraded PHP from 5.4 to 5.6 and all our mail() functionality suddenly broke, with no useful error logging.

If this is you, and you've been using ini_set() to set the "sendmail_path" then note that even though it's apparently not mentioned in the upgrade documentation -- or anywhere else I could find on php.net (or a dozen forums) -- you'll now need to go set the sendmail_path in your php.ini file; it is now ignored if you use ini_set() to specify a path to the sendmail binary on the fly.

So, just specify "sendmail_path" in php.ini instead. That's all there is to it -- that fixed all the mail() functionality for us.

Hope this little note saves someone else as much time as I spent troubleshooting and researching. Cheers!
up
-3
Mark Simon
5 years ago
It is worth reiterating that, as stated above, sendmail_path also works for Windows, overriding other Windows SMTP settings.

The comment in php.ini, “For Unix only”, does not make that clear.

This makes it relatively easy to substitute a fake sendmail program or even a shell script/batch file to save mail to a text file.

I use this technique when teaching or testing in PHP.
To Top