php 如何使用PHP邮件从地址更改信封?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/179014/
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
How to change envelope from address using PHP mail?
提问by codebunny
I am using PHP with Apache on Linux, with Sendmail. I use the PHP mailfunction. The email is sent, but the envelope has the Apache_user@localhostnamein MAIL FROM(example [email protected]) and some remote mail servers reject this because the domain doesn't exist (obviously). Using mail, can I force it to change the envelope MAIL FROM?
我在 Linux 上使用 PHP 和 Apache,使用 Sendmail。我使用PHPmail函数。电子邮件已发送,但信封中有Apache_user@localhostnamein MAIL FROM(例如 [email protected]),并且一些远程邮件服务器拒绝此操作,因为域不存在(显然)。使用mail,我可以强制它改变信封MAIL FROM吗?
EDIT: If I add a header in the fourth field of the mail() function, that changes the Fromfield in the headers of the body of the message, and DOES NOT change the envelope MAIL FROM.
编辑:如果我在mail() 函数的第四个字段中添加一个标题From,则会更改邮件正文标题中的字段,并且不会更改信封MAIL FROM。
I can force it by spawning sendmail with sendmail -t -odb -oi -frealname@realhostand piping the email contents to it. Is this a better approach?
我可以通过生成 sendmailsendmail -t -odb -oi -frealname@realhost并将电子邮件内容传递给它来强制它。这是更好的方法吗?
Is there a better, simpler, more PHP appropriate way of doing this?
有没有更好、更简单、更适合 PHP 的方法来做到这一点?
EDIT: The bottom line is I should have RTM. Thanks for the answers folks, the fifth parameter works and all is well.
编辑:底线是我应该有 RTM。感谢大家的回答,第五个参数有效,一切正常。
回答by Lucas Oman
mail() has a 4th and 5th parameter (optional). The 5th argument is what should be passed as options directly to sendmail. I use the following:
mail() 有第四个和第五个参数(可选)。第 5 个参数应该作为选项直接传递给 sendmail。我使用以下内容:
mail('[email protected]','subject!','body!','From: [email protected]','-f [email protected]');
回答by Vladimir Kornea
PHP Official documentation for mail()
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )...
additional_parameters (optional)
The additional_parametersparameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the
sendmail_pathconfiguration setting. For example, this can be used to set the envelope sender addresswhen using sendmail with the-fsendmail option.This parameter is escaped by
escapeshellcmd()internally to prevent command execution.escapeshellcmd()prevents command execution, but allows to add additional parameters. For security reasons, it is recommended for the user to sanitize this parameter to avoid adding unwanted parameters to the shell command.Since
escapeshellcmd()is applied automatically, some characters that are allowed as email addresses by internet RFCs cannot be used. mail()can not allow such characters, so in programs where the use of such characters is required, alternative means of sending emails (such as using a framework or a library) is recommended.The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a '
X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is/etc/mail/trusted-users.
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )...
附加参数(可选)
所述additional_parameters发送邮件时由所限定要使用的参数可被用于传递附加的标志作为命令行选项来配置该程序,
sendmail_path配置设置。例如,这可用于在将 sendmail 与sendmail 选项一起使用时设置信封发件人地址-f。此参数由
escapeshellcmd()内部转义以防止命令执行。escapeshellcmd()阻止命令执行,但允许添加其他参数。出于安全原因,建议用户清理此参数以避免在 shell 命令中添加不需要的参数。由于
escapeshellcmd()是自动应用,因此无法使用 Internet RFC 允许作为电子邮件地址的某些字符。mail()不能允许此类字符,因此在需要使用此类字符的程序中,建议使用其他发送电子邮件的方式(例如使用框架或库)。
X-Warning当-f使用此方法设置信封发件人 ( )时,应将网络服务器运行的用户作为受信任用户添加到 sendmail 配置中,以防止将“ ”标头添加到邮件中。对于 sendmail 用户,此文件是/etc/mail/trusted-users.
回答by Joe Scylla
You can try this (im not sure tho):
你可以试试这个(我不确定):
ini_set("sendmail_from", [email protected]);
mail(...);
ini_restore("sendmail_from");
回答by Darryl Hein
回答by mr-euro
What you actually need to do is changethe hostnameof the machine Apache is running on, plus the userApache is running as.
您实际需要做的是更改运行 Apache 的机器的主机名,以及运行 Apache的用户。
In your current case it is:
在您目前的情况下,它是:
- Apache user:nobody
- Server hostname: conniptin.internal
- Apache用户:nobody
- 服务器主机名:conniptin.internal
Changing those two values is pretty simple and will solve the root of your problem.
更改这两个值非常简单,可以解决问题的根源。
Although if you need to do it from PHP then perhaps use the system/execfunctions. I do not think it will work in practice though, as you need to restart Apache and probably also the entire host for the new names to be used.
尽管如果您需要从 PHP 执行此操作,那么也许可以使用这些system/exec函数。不过,我认为它在实践中不会起作用,因为您需要重新启动 Apache,可能还需要重新启动整个主机才能使用新名称。

