如何修复wp_mail()在电子邮件中显示HTML标签的

时间:2019-04-02 07:12:13

标签: php wordpress email

我正在使用wp_mail()函数发送预订表单,该表单是在自定义主题的页面模板中自定义构建的。我的问题是,我总是在收到的电子邮件中显示HTML标签。

我尝试通过在线查找解决方案多次修改标头和消息,但它只是不会以应有的格式显示。我尝试了不同的标题并修改了HTML。

我希望电子邮件以文本显示,并且没有HTML标签。

函数send_email(){

if(isset($_POST['submit'])){

  $room_selected = $_POST['room-name'];
  $checkIn = $_POST['check-in'];
  $checkOut = $_POST['check-out'];
  $firstname = $_POST['first-name'];
  $lastname = $_POST['surname'];
  $from = $_POST['email'];
  $phone = $_POST['phone'];
  $room_message = $_POST['your-message'];
  $numPeople = $_POST['people-select'];
  $dinnerSelect = $_POST['dinner-select'];
  $breakfastSelect = $_POST['breakfast-select'];
  $bedSelect = $_POST['bed-select'];
  $grandTotal = $_POST['grand-total'];

  $to = "test@email.com";
  $subject = "My subject";

  $headers = "From" . $from .  "\r\n";
  $headers .= "Reply-To: admin@example.com \r\n";
  $headers .= "MIME-Version: 1.0 \r\n";
  $headers .= "Content-Type: text/html; charset=UTF-8 \r\n";


  $message = '<html>';
  $message .= '<head>';
  $message .= '<title>Booking Request</title>';
  $message .= '</head>';
  $message .= '<body>';
  $message .= '<div class="container">';
  $message .=   '<div class="row">';
  $message .=          '<div class="card" style="width: 18rem;">';
  $message .=            '<img class="card-img-top" src="..." alt="Card image cap">';
  $message .=            '<div class="card-message">';
  $message .=              '<h5 class="card-title">' . $room_selected . '</h5>';
  $message .=              '<p class="card-text">Some quick example text</p>';
  $message .=            '</div>';
  $message .=            '<ul class="list-group list-group-flush">';
  $message .=              '<li class="list-group-item">Name: '. $firstname . ' ' . $lastname . '</li>';
  $message .=              '<li class="list-group-item">Check In Day:' . $checkIn . '</li>';
  $message .=              '<li class="list-group-item">Check Out Day:' . $checkOut . '</li>';
  $message .=              '<li class="list-group-item">Email:' . $from . '</li>';
  $message .=              '<li class="list-group-item">Phone:' . $phone . '</li>';
  $message .=              '<li class="list-group-item">Room:' . $room_selected . '</li>';
  $message .=              '<li class="list-group-item">Number of People:' . $numPeople . '</li>';
  $message .=              '<li class="list-group-item">Dinner Option:' . $dinnerSelect . '</li>';
  $message .=              '<li class="list-group-item">Breakfast Option:' . $breakfastSelect . '</li>';
  $message .=              '<li class="list-group-item">Bed Option:' . $bedSelect . '</li>';
  $message .=              '<li class="list-group-item">Message:' . $room_message . '</li>';
  $message .=            '</ul>';
  $message .=            '<div class="card-message">';
  $message .=              '<p class="card-text">Grand Total:' . $grandTotal . '</p>';
  $message .=            '</div>';
  $message .=          '</div>';
  $message .=        '</div>';
  $message .=       '</div>';
  $message .=    '</body>';
  $message .=  '</html>';




  if( wp_mail($to, $subject, $message, $headers) ) {
    echo 'sent';
  }
  else {
    echo 'there is a problem';
  }

}

   }

 }

    add_action( 'init', 'send_email' );

我希望电子邮件以文本显示,并且没有HTML标签。

1 个答案:

答案 0 :(得分:0)

默认的内容类型电子邮件为text/plain,不允许使用HTML。您可以通过在headers函数中设置wp_mail()参数来更改此设置:

wp_mail(
  'to email',
  'mail subject',
  'your message',
  'Content-type: text/html'
);

参数可以是字符串或数组。

有关更多详细信息,https://developer.wordpress.org/reference/functions/wp_mail/#usage