【问题标题】:PHP mail not displaying body, head tagsPHP邮件不显示正文,头部标签
【发布时间】:2020-10-21 12:59:37
【问题描述】:

我知道有很多类似的问题是关于不使用mail() 显示的 html 标签。我已经阅读了一堆,看看它是否提到了我的具体问题,但我找不到任何东西。

我的问题不在于 HTML 标记无法正确呈现,而是 <html><head><title><body> 标记完全丢失。所有其他标签都会正确显示。我已经通过更改标题进行了研究和测试,但我总是得到相同的结果。如果我删除标题,电子邮件将显示所有标签,但以常规文本显示。当我添加标题时,4 个主要标签消失了。

我不确定这是 PHP 的事情还是电子邮件客户端的事情。在 Gmail 上,所有这 4 个标签都消失了,但在 Yahoo 上,我确实得到了 title 标签。

我检查以确保 mail.add_x_header 已设置并且确实是。

我正在开发一个 Joola 网站,Joomla 也提供了一个发送电子邮件的类。我首先尝试使用它,我得到了相同的结果。这就是我尝试使用 PHP 的邮件功能的原因。

下面是我正在使用的代码。我是从 tutorial 站点获得的,我尝试将标题更改为不同的设置,但我总是得到相同的结果。

$to = "test_1@yahoo.com";
$subject = "My HTML email test.";
$headers = "From: info@test.org\r\n";
$headers .= "Reply-To: info@test.org\r\n";
$headers .= "Return-Path: info@test.org\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = "<html><body>";
$message .= "<h1> This is a test </h1>";
$message .= "</body></html>";

if ( mail($to,$subject,$message,$headers) ) {
   echo "The email has been sent!";
`enter code here`} else {
   echo "The email has failed!";
}

【问题讨论】:

  • 这仍然有点令人困惑。我有点想可能是电子邮件客户端,但我之前也创建了电子邮件模板,我会包含所有的 HTML 标记。这就是为什么我不完全确定。我会在 head 标签中添加样式声明,样式就会出现。这就是我在这种情况下想要做的。现在唯一的区别是电子邮件是使用 PHP 生成的。所以我认为我的理解是 HTML 标签只有在您使用像 MainChimp 这样的电子邮件提供商时才能正确显示。

标签: php email html-email


【解决方案1】:

这可能符合预期。电子邮件客户端尝试显示多封电子邮件,但他们没有使用大量 iframe 或类似的东西,而是解析代码并将其剥离到相关部分。

所以他们会削减&lt;html&gt;; &lt;title&gt; 没有意义,因为该页面有自己的页面标题(电子邮件客户端页面),除非他们想更改每封打开的电子邮件的标题,例如; &lt;body&gt; 通常会转换为 &lt;div&gt;&lt;head&gt; 也会被解释。

这是随机电子邮件中 ​​Gmail 网络邮件的 &lt;head&gt; 部分:

<div id=":csl" class="a3s aXjCH "><u></u>

  
  <div style="margin: 0px; padding: 0px; min-width: 100%; background-color: rgb(255, 255, 255);"><!-- This is my <body> -->

在雅虎上也是如此:

<div data-test-id="message-body-container"><div data-test-id="message-view-body" class="I_52qC D_FY W_6D6F"><div class="X_6MGW"></div><div class="msg-body P_wpofO mq_AS" data-test-id="message-view-body-content"><div class="jb_0 X_6MGW N_6Fd5"><div><div id="yiv8851120227">

    <title></title>
    
    <style type="text/css">
... <!--lots of styles here -->
</style>
  
  <div> <!--Used to be <body> -->

Android 上的 Gmail 应用还允许做更多事情:

<html><head>
  <meta id="meta-viewport" name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=2" data-zoom-on="maximum-scale=2" data-zoom-off="user-scalable=no">
  
  <link rel="stylesheet" type="text/css" href="https://mobile-webview.gmail.com/android_asset/conv.css">
  
  <style>  .spacer{display: none !important }
    img[blocked-src] { border: 1px solid #CCCCCC; }
  </style>
  <style>  .spacer{display: none !important }
    
  </style>
</head>
<body style="margin: 0 16px;"><!--This was inserted-->
<script type="text/javascript">
    var JS_TOKEN = 'TSONS4YTXAF2U436RLHPASX5HN3JIU3O';
    window.ConversationView.createTracingInstant(JS_TOKEN, "BODY_PARSE_STARTED");
</script>
<div id="conversation-header" class="spacer" style="height: 116px;"></div>
<div id="conversation-promotion" class="spacer" style="height: 0px;"></div>
<div id="m#msg-f:1671067648779872745" class="mail-message expanded">
    <div class="mail-message-header spacer" style="height: 80px;"></div>
    <div class="mail-message-content collapsible zoom-normal mail-show-images " style="display: block; margin: 16px 0; user-select: auto; -webkit-user-select: auto;"><div class="clear"><u></u>
...
  <div style="margin:0px;padding:0px;min-width:100%;background-color:#ffffff"> <!--Used to be <body>-->
...

【讨论】:

  • 感谢您的回复。我有点想可能是这样。我不完全确定的原因是因为我以前使用过电子邮件提供商。我已经创建了模板,我在 head 标签中添加了样式表,并且所有内容都被渲染了。这实际上是我在这种情况下想要做的,而不是我打算使用 PHP 的电子邮件提供商。我想我得到了答案。再次感谢您。
猜你喜欢
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
相关资源
最近更新 更多