PHP邮件:如何发送HTML?

时间:2021-10-17 18:12:54

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

?>

5 个解决方案

#1


16  

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

#2


3  

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

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

Reply-To: example@example.comContent-type: text/html

Simply add another \r\n here:

只需添加另一个\ r \ n:

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

#3


2  

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

#4


1  

I found this works well!

我发现这很好用!

Source

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

#5


-3  

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电子邮件是邪恶和恼人的。至少如果没有包含PROPER明文版本。正确=与HTML版本中相同的信息,而不仅仅是关于获取另一个电子邮件客户端的愚蠢评论,或者如果它在网络上可用,则链接到html版本。

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

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

#1


16  

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

#2


3  

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

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

Reply-To: example@example.comContent-type: text/html

Simply add another \r\n here:

只需添加另一个\ r \ n:

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

#3


2  

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

#4


1  

I found this works well!

我发现这很好用!

Source

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

#5


-3  

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电子邮件是邪恶和恼人的。至少如果没有包含PROPER明文版本。正确=与HTML版本中相同的信息,而不仅仅是关于获取另一个电子邮件客户端的愚蠢评论,或者如果它在网络上可用,则链接到html版本。

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

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