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

发布于 2024-12-11 18:56:59 字数 2164 浏览 0 评论 0原文

不确定出了什么问题,我确保设置了标题,所以也许出了问题。我的邮件脚本很大,所以我把这个小测试器放在一起,当我收到电子邮件时,所有 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 = '[email protected]';
$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: [email protected] <[email protected]>' . '\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 不确定问题是什么。至于该脚本的结构,它不是我实际的邮件程序脚本,它是一个带有计数器的测试脚本,因此我知道在测试会话期间要查找多少电子邮件。

Not sure what is wrong, I made sure to set my headers so perhaps something is wrong. The mailscript that I have is huge so I put together this little tester, when i receive the emails all the html tags are present but no formatting took place. I'm curious if it's a problem with the way I set up the header or if I needed more to it. I searched the forums and it looked like the problem most people had was they weren't adding in the HTML content-type but that is added in here, so any help would be awesome.

thanks

Okay so I've checked out some tutorials online
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 = '[email protected]';
$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: [email protected] <[email protected]>' . '\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';
}
?>

I updated the headers to the suggested forms on W3 and a few other places. I'm guessing that my headers are the problem... this still outputs regular text not html not sure what the problem is. As for the structure of this script it's not my actual mailer script it's a test script w/ a counter so I know how many emails to look out for during a test session.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-12-18 18:56:59

尝试对 \r\n 使用双引号。

$mailheader = 'MIME-Version: 1.0' . "\r\n";
$mailheader .= 'Content-type: text/html; charset=ISO-8859-1' . "\r\n";
$mailheader .= 'from: [email protected] <[email protected]>' . "\r\n";

Try using double quotes for your \r\n.

$mailheader = 'MIME-Version: 1.0' . "\r\n";
$mailheader .= 'Content-type: text/html; charset=ISO-8859-1' . "\r\n";
$mailheader .= 'from: [email protected] <[email protected]>' . "\r\n";
毅然前行 2024-12-18 18:56:59

我建议您使用 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('[email protected]' => 'John Doe'))

  //Set the To addresses with an associative array
  ->setTo(array('[email protected]', '[email protected]' => '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'))
  ;

I suggest that you use Swift, which makes it very easy to send email in PHP. An even better solution would be to use something like Postmarkapp, which an addition of having excellent libraries, also makes sure your messages doesn't get stuck in spam filters etc.

Example with 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('[email protected]' => 'John Doe'))

  //Set the To addresses with an associative array
  ->setTo(array('[email protected]', '[email protected]' => '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'))
  ;

据我所知,“发件人”应该位于“内容类型”之前。除此之外,您不需要“MIME”标头。

As far as I can see the "From" should be before the "Content-type". On top of this you should not need the "MIME" header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文