Php邮件:如何发送HTML?

12
下面的代码可以正确地发送电子邮件,但是出现在正文中。我需要在邮件正文中显示HTML,但我无法做到。网络上的示例不会发送电子邮件 :(
如何修复我的代码以使用HTML在正文中发送电子邮件?
谢谢!
<?php

$to = 'mymail@mail.com';

$subject = 'I need to show html'; 

$from ='example@example.com'; 

$body = '<p style=color:red;>This text should be red</p>';

ini_set("sendmail_from", $from);

$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
  $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) {

  echo("<p>Sent</p>");
 } else {
  echo("<p>Error...</p>");
 }

?>

+1 对于这个问题我也有类似的疑问,但是这个帖子从不同角度回答了它。 - Sam
5个回答

20

使用此标题发送邮件:

 $header  = "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html; charset: utf8\r\n";

并且对于内容/正文部分:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...

使用内联CSS命令非常重要,推荐为界面使用表格。

...

在您的邮件正文中,您需要放置带有头部和主体的HTML代码


谢谢你的回复,但是头部有错误,正文中也没有HTML标记 :( - lleoun
请将您的代码行更改为这样,并关闭您打开的所有标签。并使用<body>标签。 $headers .= "From: " . $from . "\r\nReply-To: " . $from . ""; - helle
抱歉,我不太明白:$headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset: utf8\r\n"; 发送的电子邮件中正文已经很好地显示了。但是在发件人字段中显示的是www-data..我该如何解决?一旦解决了这个问题就完成了。非常感谢。 - lleoun
@helle 你的意思是使用表格比使用div更好来进行布局吗? - Sam
2
@Sam 在邮件的情况下:是的。 - helle

4

你有查看过来信的头部吗?它会显示:

回复地址:example@example.com内容类型:文本/HTML

在这里简单地添加另一个\r\n

Reply-To: " . $from . "\r\n";

2
我建议您不要自己尝试,而是使用网上提供的许多免费课程来完成它。我推荐使用PHPMailer。请注意保留HTML标签。

1
我发现这个很好用!

来源

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

我也找到了,但为什么在我的情况下没有发送呢? - lleoun
这依赖于邮件命令。该命令存在于大多数*NIX系统中。 - Paul Calabro

-3
简单的回答:不要这样做。HTML邮件很烦人而且很恶心,除非有一个合适的纯文本版本。合适的纯文本版本应该包含与HTML版本相同的信息,而不仅仅是一个愚蠢的评论或者一个链接,告诉你去下载另一个电子邮件客户端或者在网上查看HTML版本。
如果你真的需要它:http://pear.php.net/package/Mail_Mime

3
如果被误用,它们可能会很烦人。但是,如果小心使用,它们就非常好。在我看来,最佳实践是同时发送纯文本和 HTML 格式的正文,这样用户可以选择想要查看哪个版本。 - binaryLV

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接