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

时间:2013-07-26 20:07:39

标签: php email

这是我的代码:

$to = 'to@mail.com';

$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: from@mail.com \r\n';

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

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

1 个答案:

答案 0 :(得分:26)

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

$to = "to@to.com";
$from = "from@from.com";
$subject = "subject";
$message = "this is the message body";

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