php邮件:如何发送html?

发布于 2024-10-16 00:47:23 字数 842 浏览 1 评论 0原文

下面的代码正确发送电子邮件,但针对正文。我需要在消息正文中显示 html,但无法实现。网络中的示例不会发送电子邮件:(

我怎样才能修复我的代码以发送正文中包含 html 的电子邮件?

非常感谢!

<?php

$to = '[email protected]';

$subject = 'I need to show html'; 

$from ='[email protected]'; 

$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>");
 }

?>

The code below is sending an email correctly but for the body. I need to show html in the body of the message and I cannot make it. The examples in the web won't send the email :(

How can I fix my code to send the email with the html in the body?

Thanks a ton!

<?php

$to = '[email protected]';

$subject = 'I need to show html'; 

$from ='[email protected]'; 

$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>");
 }

?>

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

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

发布评论

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

评论(5

假装不在乎 2024-10-23 00:47:23

将此标头用于邮件:

 $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 代码

use this header for the mail:

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

and for the content/body:

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

it's important to use inline css commands and recommanded to use tables for the interface.

...

In your Mail-Body you than have to put HTML code with head and body

伤感在游骋 2024-10-23 00:47:23

您查看过收到邮件的标头吗?它说

Reply-To: [email protected]: text/html

只需在此处添加另一个 \r\n 即可:

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

Have you looked at the headers of the incoming mail? It says

Reply-To: [email protected]: text/html

Simply add another \r\n here:

Reply-To: " . $from . "\r\n";
时光病人 2024-10-23 00:47:23

我建议您不要自己乱搞,而是使用网络上提供的众多免费课程之一来完成此操作。

我推荐: PHPMailer

I recommend rather than messing around with doing this yourself you use one of the many free classes available all over the web to do it.

I would recommend: PHPMailer

梦中楼上月下 2024-10-23 00:47:23

我发现这个效果很好!

来源

<?php
//define the receiver of the email
$to = '[email protected]';
//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: [email protected]\r\nReply-To: [email protected]";
//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";
?>

I found this works well!

Source

<?php
//define the receiver of the email
$to = '[email protected]';
//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: [email protected]\r\nReply-To: [email protected]";
//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";
?>
沙与沫 2024-10-23 00:47:23

简单的回答:不要这样做。 HTML 电子邮件既邪恶又烦人。至少如果没有包含正确的纯文本版本。正确 = 与 HTML 版本中的信息相同,而不仅仅是关于获取另一个电子邮件客户端的愚蠢评论或指向 html 版本(如果在网络上可用)的链接。

如果您确实需要它: http://pear.php.net/package/Mail_Mime

Simple answer: Don't do it. HTML emails are evil and annoying. At least if there's no PROPER plaintext version included. Proper = same information like in the HTML version, not just a stupid comment about getting another email client or a link to the html version if it's available on the web.

If you really need it: http://pear.php.net/package/Mail_Mime

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