显示名称而不是电子邮件的电子邮件标题的格式是什么?

时间:2010-09-04 21:32:55

标签: php header email

我正在尝试创建一个php脚本,它将使用mySQL数据库为我处理邮件列表,我已经完成了大部分工作。不幸的是,我似乎无法使标题正常工作,我不确定问题是什么。

$headers='From: noreply@rilburskryler.net \r\n';
$headers.='Reply-To: noreply@rilburskryler.net\r\n';
$headers.='X-Mailer: PHP/' . phpversion().'\r\n';
$headers.= 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1 \r\n';
$headers.= "BCC: $emailList";

我收到的结果是:

“noreply”@ rilburskryler.net rnReply-To:noreply@rilburskryler.netrnX-Mailer:PHP / 5.2.13rnMIME-Version:1.0

3 个答案:

答案 0 :(得分:105)

要显示姓名,而不是显示的电子邮件地址,请使用以下内容:

"John Smith" <johnsemail@hisserver.com>

易。

关于断行换行符,这是因为您将文本括在撇号而不是引号中:

$headers = array(
  'From: "The Sending Name" <noreply@rilburskryler.net>' ,
  'Reply-To: "The Reply To Name" <noreply@rilburskryler.net>' ,
  'X-Mailer: PHP/' . phpversion() ,
  'MIME-Version: 1.0' ,
  'Content-type: text/html; charset=iso-8859-1' ,
  'BCC: ' . $emailList
);
$headers = implode( "\r\n" , $headers );

答案 1 :(得分:10)

single quoted string内,只有转义序列\'\\分别被'\取代。您需要使用double quotes将转义序列\r\n替换为相应的字符:

$headers = "From: noreply@rilburskryler.net \r\n";
$headers.= "Reply-To: noreply@rilburskryler.net\r\n";
$headers.= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers.= "BCC: $emailList";

您还可以使用数组来收集标题字段,然后将它们放在一起:

$headers = array(
    'From: noreply@rilburskryler.net',
    'Reply-To: noreply@rilburskryler.net',
    'X-Mailer: PHP/' . phpversion(),
    'MIME-Version: 1.0',
    'Content-type: text/html; charset=iso-8859-1',
    "BCC: $emailList"
);
$headers = implode("\r\n", $headers);

答案 2 :(得分:-2)

    $to = 'SendersName@domain.com';
    $to .=', ' . $_POST['Femail'];
    $subject = 'Contact Us Form';

// message
$message ="<html>
<head>
<title>Email title</title>
</head>
<body>
<h3>important message follows</h3>
<div>
     you are being brought this email to be safe.
</div>
</body>
</html>";


    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    // Additional headers
    $headers .= 'To: SendersEmailName <SendersEmailName@domain.com>' . "\r\n";
    $headers .= 'From: YourName <YourName@domain.com>' . "\r\n";
    $headers.='X-Mailer: PHP/' . phpversion()."\r\n";
    $headers.= "BCC: $emailList";


    mail($to, $subject, $message, $headers);