【问题标题】:What is the format for e-mail headers that display a name rather than the e-mail?显示名称而不是电子邮件的电子邮件标题的格式是什么?
【发布时间】:2011-04-08 07:53:41
【问题描述】:

我正在尝试创建一个 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

【问题讨论】:

    标签: php header email


    【解决方案1】:

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

    "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 );
    

    【讨论】:

    • 显示名称包含空格字符时需要加引号。
    • @Gumbo:刚刚测试过了。工作没有引号。不确定这是否是标准,或者只是一个非常灵活/宽容的结构......
    • 我猜是后者;见RFC 822
    • 不过,我怀疑引用通常是个好主意。感谢您提供完整的描述性答案。
    • 这个答案是红鲱鱼,因为From: noreply@rilburskryler.net 是根据 RFC 5322 中的 BNF 的有效标头。请参阅第 3.4 节。我怀疑 Gumbo 关于逃跑的回答才是真正的问题。
    【解决方案2】:

    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);
    

    【讨论】:

      【解决方案3】:
          $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);
      

      【讨论】:

        猜你喜欢
        • 2011-02-07
        • 2015-07-05
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-09
        相关资源
        最近更新 更多