2011-02-04 29 views
10

Aşağıdaki kod doğru bir e-posta gönderiyor, ancak gövde için. Mesajın gövdesinde html göstermem gerekiyor ve bunu yapamam. web örnekler email :(Ben vücutta html ile e-posta göndermek için kodumu nasıl düzeltebilirimPhp mail: html nasıl gönderilir?

göndermez?

Teşekkür bir 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>"); 
} 

?> 
+0

+1 soru için ben benzer soru vardı ama çok değil ama benim durumumda gönderme Bulunan (çeşitli açılardan) Konuyu cevaplar onu – Sam

cevap

16

kullanım mai için bu başlık l:

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

ve içerik/vücut için

:

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

o satır içi css komutlarını kullanmak önemlidir ve arayüz için tabloları kullanmak önerilir. Yapmayın: HTML code with head and body

+0

Teşekkürler helle, ama hatalar üstbilgiler ve no html gövdesinde :( – lleoun

+0

sizin de bunun için satırınızı değiştirin ve açtığınız tüm etiketleri kapatın ve ' 'etiketi kullanın. ' $ headers. = "Kimden: ". $ from." \ r \ nReply-To: ". $ from." "; – helle

+0

Üzgünüm, bunu anlamadım: $ headers =" MIME-Version: 1.0 \ r \ n "; $ headers = "İçerik türü: text/html; charset: utf8 \ r \ n"; , e-postayı metin ile birlikte neyin harika olduğunu okumak için gönderir.Ama alanında www-data gösterilmiştir .. bunu nasıl düzeltebilirim Bir kez bittiğinde bitti. Çok teşekkürler – lleoun

1

I Bu iyi çalışıyor bulundu!

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

, niye ya? – lleoun

+0

Posta kurulumu doğru mu? Bir IIS sunucusunda mısınız? – chrisjlee

+0

Bu, posta komutuna dayanıyordu. Bu komut, çoğu * NIX sistemidir. –

-3

Basit cevabı koymak zorunda daha Mail-Body

...

sen. HTML e-postaları kötü ve can sıkıcıdır. En azından PROPER düz metin sürümü bulunmuyorsa. Uygun = HTML sürümünde olduğu gibi aynı bilgiler, sadece başka bir e-posta istemcisi almanın ya da web'de mevcutsa html sürümüne bağlantı hakkında aptalca bir yorum.

gerçekten ihtiyacınız varsa: http://pear.php.net/package/Mail_Mime

+3

Kötüye kullanılması halinde can sıkıcı olabilirler. Yine de dikkatlice kullanılmışsa, oldukça iyidirler. Benim düşünceme göre, en iyi uygulama hem düz/metin hem de düz/html gövdeleri göndermektir, böylece kullanıcı hangisini görmek istediğini seçebilir. – binaryLV

2

Bunu yapmak için web üzerinden tüm birçok ücretsiz sınıfların birini kullanan kendiniz bunu ile karıştırmasını yerine öneriyoruz.

tavsiye ediyorum: PHPMailer

3

Gelen postalardan başlıklarına baktım? Bu

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

Basitçe burada başka bir \r\n eklemek diyor ki:

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