[英]Php mail: how to send html?
以下代码可正确发送电子邮件,但适用于正文。 我需要在邮件正文中显示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>");
}
?>
您是否查看了传入邮件的标题? 它说
Reply-To: example@example.comContent-type: text/html
只需在此处添加另一个\\r\\n
:
Reply-To: " . $from . "\r\n";
我建议您使用网络上可用的许多免费类之一来做,而不要自己弄乱。
我建议: PHPMailer
我发现这很好!
<?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";
?>
简单的答案:不要这样做。 HTML电子邮件既邪恶又令人讨厌。 至少如果没有PROPER明文版本。 正确=与HTML版本中的信息相同,而不仅仅是关于获取另一个电子邮件客户端的愚蠢评论,或者如果在Web上可用,则指向HTML版本的链接。
如果您确实需要它: http : //pear.php.net/package/Mail_Mime
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.