PHP mail() 如何设置发件人邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17890134/
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-25 16:38:16  来源:igfitidea点击:

PHP mail() how to set sender mail

phpemail

提问by user1885099

This is my code:

这是我的代码:

$to = '[email protected]';

$subject = 'test';

$body = 'test';

$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "To: <$to>" . "\r\n";
$header .= 'From: [email protected] \r\n';

mail($to, $subject, $body, $header);

The code works, it sends the email. But the sender is not the one that I defined. The sender seems to be the webmail host. What am I doing wrong?

代码有效,它发送电子邮件。但是发件人不是我定义的那个。发件人似乎是网络邮件主机。我究竟做错了什么?

回答by mti2935

Try setting the envelope sender, as well setting the sender in the headers of the message, like so:

尝试设置信封发件人,并在邮件的标题中设置发件人,如下所示:

$to = "[email protected]";
$from = "[email protected]";
$subject = "subject";
$message = "this is the message body";

$headers = "From: $from"; 
$ok = @mail($to, $subject, $message, $headers, "-f " . $from);