2011-02-04 3 views
10

아래 코드는 본문에 대해 이메일을 올바르게 전송하고 있습니다. 메시지 본문에 html을 표시해야하며 작성할 수 없습니다. 웹의 예는 이메일 :(내가 본문에 HTML로 이메일을 보내 내 코드를 해결할 수있는 방법PHP 메일 : 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>"); 
} 

?> 
+0

한 질문에 대해 나는 유사 질문을했지만, 너무하지만 내 경우에는 전송을 찾을 수 (다양한 각도에서)이 스레드 답변을 – Sam

답변

16

사용 마이이 헤더 L :

$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 명령을 사용하는 것이 중요하고, 인터페이스 테이블을 사용하도록 권유하는. 하지 마 : HTML code with head and body

+0

안녕하세요, 헤더에 오류가 있지만 본문에 html이 없습니다 : ( – lleoun

+0

)이 줄을 변경하고 열어 놓은 모든 태그를 닫고 ''태그를 사용하십시오 '$ headers. = "보낸 사람 : "$ from."답장 : ". $ from." "; ' – helle

+0

죄송합니다. 이해가 안갑니다 : $ headers ="MIME-Version : 1.0 \ r \ n "; $ headers . = "Content-type : text/html; charset : utf8 \ r \ n"; 은 텍스트가있는 이메일을 읽음으로 보냅니다. 그러나 보낸 사람 필드에는 www-data가 표시됩니다. 어떻게 해결할 수 있습니까? 일단 끝나면 끝날 것입니다. 고마워요 – lleoun

1

I을 이 잘 작동 발견!

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

, 왜? – lleoun

+0

메일이 올바르게 설정 되었습니까? IIS 서버에 있습니까? – chrisjlee

+0

이것은 메일 명령에 의존합니다. 이 명령은 대부분의 * NIX 시스템에 존재합니다. –

-3

간단한 답을 넣어야 할 것보다 당신의 메일 본문에

...

당신은. HTML 전자 메일은 악하고 짜증나게합니다. 적어도 적절한 일반 텍스트 버전이없는 경우. Proper = HTML 버전과 같은 동일한 정보, 다른 이메일 클라이언트 또는 웹에서 사용할 수있는 경우 html 버전에 대한 링크를 얻는 것에 대한 어리석은 주석이 아닙니다.

은 당신이 정말로 그것을 필요한 경우 : http://pear.php.net/package/Mail_Mime

+3

오용되는 경우 성가심을 줄 수 있습니다. 비록 조심스럽게 사용된다면, 꽤 좋습니다. 필자가 생각하기에 가장 좋은 방법은 일반/텍스트 및 일반/HTML 본문을 모두 보내어 사용자가보고 싶은 것을 선택할 수 있도록하는 것입니다. – binaryLV

2

나는 당신이 그것을 할 수있는 웹을 통해 모든 가능한 많은 무료 클래스 중 하나를 사용 자신이 일을 만지작보다는 좋습니다.

나는 추천 : PHPMailer

3

을 사용하면 수신 메일의 헤더 봤어? 그것은

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

단순히 여기에 또 다른 \r\n 추가 말한다 :

Reply-To: " . $from . "\r\n"; 
관련 문제