PHP 邮件:如何发送 html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4897215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:12:22  来源:igfitidea点击:

Php mail: how to send html?

phphtmlemailmessagesendmail

提问by lleoun

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 :(

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

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

如何修复我的代码以发送正文中包含 html 的电子邮件?

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

?>

回答by helle

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.

使用内联 css 命令很重要,建议在界面中使用表格。

...

...

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

在您的邮件正文中,您必须将HTML 代码与 head 和 body放在一起

回答by Linus Kleen

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

您是否查看了传入邮件的标题?它说

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

Simply add another \r\nhere:

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

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

回答by James

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

我会推荐:PHPMailer

回答by Paul Calabro

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

回答by ThiefMaster

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.

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

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

如果你真的需要它:http: //pear.php.net/package/Mail_Mime