当打开一封邮件时,我希望from
字段显示为"Jack Sparrow Via somesite"
,而不是显示具体的电子邮件地址。
请问如何在PHP的mail()
函数中进行设置?
你可以使用基本的标题来实现这一点。
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Jack Sparrow <jsparrow@blackpearl.com>' . PHP_EOL .
'Reply-To: Jack Sparrow <jsparrow@blackpearl.com>' . PHP_EOL .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
你需要设置头部信息:
$to = "Someone@email.com";
$header = "FROM: Jack Sparrow <some@site.com>\r\n";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header) or die();
只需像这样使用
$headers .= 'From: [Name of the person]'.'<'. $this->from .'>'. "\n";
$to = "Someone@email.com";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header, '-f some@site.com -F "Jack Sparrow"') or die();
只需添加 -f 参数来提供发件人的电子邮件,添加 -F 参数以提供发件人的姓名给 mail(),都作为字符串。
注意:在 "Jack Sparrow" 周围使用双引号。
假设您正在Linux机器上使用sendmail:
您可以在shell中键入 "man sendmail" 以获得更详细的信息。
$mail->SetFrom("info@mibckerala.org", "MIBC");
\r\n
换行时,最好使用PHP_EOL
以使其兼容所有系统。 - Lawrence Cherone