Php mail: как отправить html?
Приведенный ниже код отправляет электронное письмо правильно, но для тела. Мне нужно показать 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>");
}
?>
Ответы
Ответ 1
используйте этот заголовок для почты:
$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 и рекомендуется использовать таблицы для интерфейса.
...
В вашем Mail-Body вам больше, чем нужно поставить HTML-код с головой и телом
Ответ 2
Вы посмотрели заголовки входящей почты? В нем говорится:
Reply-To: [email protected]: text/html
Просто добавьте еще \r\n
здесь:
Reply-To: " . $from . "\r\n";
Ответ 3
Я рекомендую, а не вмешиваться в это, используя один из многих бесплатных классов, доступных во всем Интернете, чтобы это сделать.
Я бы порекомендовал: PHPMailer
Ответ 4
Я нашел, что это хорошо работает!
Источник
<?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";
?>
Ответ 5
Простой ответ: не делайте этого. HTML-письма являются злыми и раздражающими. По крайней мере, если нет открытой версии открытого текста PROPER. Правильная = такая же информация, как в HTML-версии, а не просто глупый комментарий о получении другого почтового клиента или ссылки на html-версию, если она доступна в Интернете.
Если вам это действительно нужно: http://pear.php.net/package/Mail_Mime