如何在 php 中创建 html 邮件正文?

How to create html mail body in php?

我正在尝试为我的邮件正文编写代码。我有一个名为 mailfunction.php 的 php 文件。

在该文件中,我创建了一个变量,其中包含我的 html 代码,如下所示

 $message_body ="<html><body>"
."<table class='tg' style='border-style: dotted;'>"
          ."<tr><td class='tg-3zav'>Civilité</td><td class='tg-3zav'>" . strip_tags($titre) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Prénom</td><td class='tg-3zav'>" . strip_tags($prenom) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Nom</td><td class='tg-3zav'>" . strip_tags($nom) . "</td></tr>"
          . "</table></body></html>";

但不幸的是,我收到的邮件没有对 html 标签进行编码。我收到了一些包含 html 标签的文本,这些标签未按以下方式编码

<table class="tg" style="border-style: dotted;">
  <tr>
    <th class="tg-3zav">Civilité</th>
    <th class="tg-3zav">M</th>
  </tr>
  <tr>
    <td class="tg-3zav">Nom</td>
    <td class="tg-3zav">Frank</td>
  </tr>
  <tr>
    <td class="tg-3zav">Prénom</td>
    <td class="tg-3zav">Betrix</td>
  </tr>
</table>

有没有办法在 php 文件中编写此 html 代码?

您需要在邮件功能

中使用headers
<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message ="<html><body>"
."<table class='tg' style='border-style: dotted;'>"
          ."<tr><td class='tg-3zav'>Civilité</td><td class='tg-3zav'>" . strip_tags($titre) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Prénom</td><td class='tg-3zav'>" . strip_tags($prenom) . "</td></tr>"
          ."<tr><td class='tg-3zav'>Nom</td><td class='tg-3zav'>" . strip_tags($nom) . "</td></tr>"
          . "</table></body></html>";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

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

headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";

非常感谢。在这里,我 post 我的更正源代码

$headers .= 'From: ADMIN SERVER' . "\r\n";
$headers .= 'Cc: cc-cc@admin-server.org' . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$message_body ="<html><body>"
                ."<style type='text/css'>"
                ."tg {border-collapse:collapse;border-spacing:0;}"
                ."</style>"
                ."<table class='tg' style='border-style: dotted;'>"
                ."<tr><td class='tg-3zav'>Civilité :</td><td class='tg-3zav'>" . strip_tags($titre) . "</td></tr>"
                ."<tr><td class='tg-3zav'>Prénom : </td><td class='tg-3zav'>" . strip_tags($prenom) . "</td></tr>"
                ."<tr><td class='tg-3zav'>Nom : </td><td class='tg-3zav'>" . strip_tags($nom) . "</td></tr>"
                ."</table></body></html>";