为了账号安全,请及时绑定邮箱和手机立即绑定

PHP邮件:如何发送HTML?

PHP邮件:如何发送HTML?

沧海一幻觉 2019-09-06 15:50:46
下面的代码是正确发送电子邮件,但身体。我需要在消息正文中显示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>"); }?>
查看完整描述

3 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

将此标题用于邮件:


 $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代码放在头部和正文中


查看完整回答
反对 回复 2019-09-06
?
手掌心

TA贡献1942条经验 获得超3个赞

你看过收到的邮件的标题了吗?它说


回复:example@example.comContent-type:text / html

\r\n在这里添加另一个:


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


查看完整回答
反对 回复 2019-09-06
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

我发现这很好用!

<?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";

?>


查看完整回答
反对 回复 2019-09-06
  • 3 回答
  • 0 关注
  • 1293 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信