如何通过PHP设置电子邮件发件人的名称


How do I set the name of an email sender via PHP

所以我希望打开电子邮件时的from字段类似

CCD_ 2,而不是明确的电子邮件地址。

我想知道如何在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";

在标题部分。

如果你使用GitHub中的PHP Mailer,那么你可以通过:

$mail->SetFrom("info@mibckerala.org", "MIBC");
$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中键入"mansendmail"来查找更详细的信息。

我不确定你的确切意思,但正如你在PHP.net邮件功能中看到的那样。

要将名称添加到from部分,您需要在标题中发送该名称。From:Name<email@example.com>