【问题标题】:How to create html mail body in php?如何在 php 中创建 html 邮件正文?
【发布时间】:2015-04-24 15:07:43
【问题描述】:

我正在尝试为我的邮件正文编写代码。我有一个名为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>";

但不幸的是,我收到的邮件没有对 htlm 标签进行编码。我收到了一些包含 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>

有没有办法把这段html代码写在php文件里?

【问题讨论】:

  • Php mail: how to send html? 的可能重复项
  • 您必须声明它是一个 HTML 电子邮件。请参阅上面发布的链接@caCtus 了解如何操作。

标签: php html email


【解决方案1】:

您需要在邮件功能中使用标题

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

【讨论】:

  • 为什么要这样设置标题?为什么不直接使用headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=UTF-8\r\n"; 工作得很好。
  • 非常感谢它有效。事实上,我已经将我的内容类型更改为“text/html”
  • 这是我更正后的完整代码 $headers .= 'From: ADMIN SERVER' 。 "\r\n"; $headers .= '抄送:cc-cc@admin-server.org' 。 "\r\n"; $headers = 'MIME 版本:1.0' 。 "\r\n" 。 '内容类型:text/html;字符集=UTF-8' 。 "\r\n"; $message_body ="" ."" . "" ."
    ";
  • 很酷,您必须在邮件中添加标题类型 :) 感谢您的支持 :)
【解决方案2】:

非常感谢。在这里我发布我更正的源代码

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

【讨论】:

  • 你应该接受上面对你有帮助的答案,而不是发布这个(点击绿色勾号)
  • 有时我没有那个特权。这就是为什么我添加了您提供的更正版本!
  • 每个用户都有接受答案的特权。在获得几个代表点之前,您无法进行投票。
猜你喜欢
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
相关资源
最近更新 更多