dongmangsha7354 2015-12-31 15:02
浏览 103

在有换行符时显示在我的电子邮件中

I'm not sure what I am doing wrong here. In the emails, keeps showing every time there is a line break. What do I need to modify in this code to fix it.

    public function sendSupportEmail($email, $name, $comments)
{
    //Wait until Google Apps are configured to accept from this domain
    //$to = "test@mail.com";
    $to = "test@mail.com.com";
    $subject = "Support: Support Inquiry";


    //Headers
    // To send HTML mail, you can set the Content-type header.
    $autoHeaders  = "MIME-Version: 1.0
";
    $autoHeaders .= "Content-type: text/html; charset=iso-88591
";
    $autoHeaders .= "From: Web Bot";
    $autoHeaders .= "<webbot@mail.com>
";
    $autoHeaders .= "Reply-To: webbot@mail.com
";
    $autoHeaders .= "Return-Path: webbot@mail.com
";
    $autoHeaders .= "X-Mailer: PHP 5.x
";


    //Print the local date
    $date = new DateTime('now', new DateTimeZone('America/Denver'));
    $datePrint =  $date->format('F j, Y, g:i a');

    //Create Text Based Message Below
    $message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
    $message .= "<b>Name:</b><br>{$name}<br><br>";
    $message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
    $message .= "<b>Comments:</b><p>{$comments}</p>";

    //Send them the E-Mail
    return mail($to, $subject, $message, $autoHeaders);
}
  • 写回答

2条回答 默认 最新

  • doucheng7534 2015-12-31 15:40
    关注

    For the headers ($autoHeaders), is the correct way to separate various fields. But the $comments itself needs to use the HTML <br>-tag to denote a line-break, because you're sending a HTML e-mail (see Content-Type).

    You can use the nl2br function for that:

    $message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
    $message .= "<b>Name:</b><br>{$name}<br><br>";
    $message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
    $message .= "<b>Comments:</b><p>{" . nl2br($comments) . "}</p>";
    return mail($to, $subject, $message, $autoHeaders);
    

    In addition you can use htmlentities() on $comments before nl2br to convert other characters to HTML entities (like &euro; for € f.e.):

    $message = "<h3>Support Inquiry sent on {$datePrint}</h3>";
    $message .= "<b>Name:</b><br>{$name}<br><br>";
    $message .= "<b>Email:</b><br><a href='mailto:{$email}'>{$name}</a><br><br>";
    $message .= "<b>Comments:</b><p>{" . nl2br(htmlentities($comments)) . "}</p>";
    return mail($to, $subject, $message, $autoHeaders);
    

    See Ideone sample

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制