我的HTML电子邮件没有格式化为HTML?

时间:2011-10-21 17:56:29

标签: php html email header

不确定是什么问题,我确保设置我的标题所以可能出现了问题。我有的mailscript很大,所以我把这个小测试者放在一起,当我收到电子邮件时,所有的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不确定问题是什么。至于这个脚本的结构,它不是我的实际邮件程序脚本,它是一个带计数器的测试脚本,所以我知道在测试会话期间要注意多少封电子邮件。

3 个答案:

答案 0 :(得分:5)

尝试为\ 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 :(得分:1)

我建议你使用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'))
  ;

答案 2 :(得分:0)

据我所知,“From”应该在“Content-type”之前。除此之外,您不需要“MIME”标题。