【问题标题】:My HTML Emails are not getting formatted as HTML?我的 HTML 电子邮件没有被格式化为 HTML?
【发布时间】:2011-10-21 17:56:29
【问题描述】:

不知道出了什么问题,我确保设置了我的标题,所以也许有问题。我拥有的邮件脚本很大,所以我把这个小测试器放在一起,当我收到电子邮件时,所有的 html 标签都存在,但没有进行格式化。我很好奇这是我设置标题的方式有问题还是我需要更多。我搜索了论坛,看起来大多数人遇到的问题是他们没有在 HTML 内容类型中添加,而是在此处添加,所以任何帮助都会很棒。

谢谢

好的,我已经在线查看了一些教程
http://www.webhostingtalk.com/showthread.php?t=416467
http://css-tricks.com/2866-sending-nice-html-email-with-php
http://www.w3schools.com/php/func_mail_mail.asp

<?php
session_start();
if (isset($_SESSION['new_count'])) //counts how many fake emails i send myself
{
    $count = $_SESSION['new_count'];
}
else
{
    //first time
    $count = 0;
}

$to = 'myemail@gmail.com';
$subject = 'email test';
$message = '<html><head></head><body>';
$message .= '<h1>this is an email test</h1>';
$message .= '<br />does new line work?<br />';
$message .= 'how about <b>bold</b> and <strong>strong</strong>?<br />';
$message .= '</body></html>';
//updated my header to include mime-version
$mailheader = 'MIME-Version: 1.0' . '\r\n';
$mailheader .= 'Content-type: text/html; charset=ISO-8859-1' . '\r\n';
$mailheader .= 'from: abc@def.com <btyazaki@gmail.com>' . '\r\n';

$yay = mail($to,$subject,$message,$mailheader);

if($yay)
{
    echo 'woot';
    $count++;
    $_SESSION['new_count'] = $count;
    echo '<br>Emails Sent: '.$count;
}
else
{
    echo 'no woot';
}
?>

我将标题更新为 W3 和其他一些地方的建议表单。我猜我的标题是问题......这仍然输出常规文本而不是html,不确定问题是什么。至于这个脚本的结构,它不是我的实际邮件脚本,它是一个带计数器的测试脚本,所以我知道在测试会话期间要注意多少电子邮件。

【问题讨论】:

  • 这不是发送 HTML 电子邮件的方式。请参阅 Stack Overflow 或 Google 的搜索,了解它是如何完成的
  • 佩卡,感谢您提供的信息。我确实进行了谷歌搜索,并在提问之前查看了 Stack Overflow 的一些建议主题。我发现的大部分内容是声明标题有问题,但是根据我发布的教程,我的标题看起来应该是这样,很好奇是什么搞砸了电子邮件。
  • 您没有使用 &lt;html&gt;&lt;head&gt;&lt;body&gt; 标记作为初学者。电子邮件客户端很挑剔。
  • 哦,我的错,我更新的脚本确实有 和 标签...
  • 如果你想使用转义字符,例如\r\n,那么你必须在PHP中使用双引号字符串。

标签: php html email header


【解决方案1】:

尝试为您的 \r\n 使用双引号。

$mailheader = 'MIME-Version: 1.0' . "\r\n";
$mailheader .= 'Content-type: text/html; charset=ISO-8859-1' . "\r\n";
$mailheader .= 'from: abc@def.com <btyazaki@gmail.com>' . "\r\n";

【讨论】:

  • +1 这很可能是解决方案,因为其他一切似乎都井井有条。
  • 这是解决方案,我没有意识到我必须双引号返回和换行符。非常感谢。这会是我尝试发送普通文本电子邮件时换行符不起作用的原因吗?
  • 是的,任何转义字符都应该用双引号括起来。
【解决方案2】:

我建议你使用Swift,这样可以很容易地用PHP 发送电子邮件。更好的解决方案是使用 Postmarkapp 之类的东西,除了拥有优秀的库之外,还可以确保您的邮件不会卡在垃圾邮件过滤器等中。

Swift 示例:

require_once 'lib/swift_required.php';

//Create the message
$message = Swift_Message::newInstance()

  //Give the message a subject
  ->setSubject('Your subject')

  //Set the From address with an associative array
  ->setFrom(array('john@doe.com' => 'John Doe'))

  //Set the To addresses with an associative array
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))

  //Give it a body
  ->setBody('Here is the message itself')

  //And optionally an alternative body
  ->addPart('<q>Here is the message itself</q>', 'text/html')

  //Optionally add any attachments
  ->attach(Swift_Attachment::fromPath('my-document.pdf'))
  ;

【讨论】:

  • 我很感激,我可能会在我的个人网站上使用邮件应用程序/库进行集成,但我不想在这个客户网站上有一堆额外的东西,他得到了是否免费使用外部脚本真的很敏感,不知道为什么。
  • @Brodie:很抱歉听到这个消息,here's something to cheer you up :)
【解决方案3】:

据我所知,“发件人”应该在“内容类型”之前。最重要的是,您不需要“MIME”标头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2011-06-02
    • 2013-11-08
    • 1970-01-01
    • 2016-01-18
    • 2012-06-04
    相关资源
    最近更新 更多