2017-03-05 3 views
3

PHP 메일() 함수를 통해 HTML 콘텐츠를 보내려고합니다. 내 코드가 무엇이 잘못되었는지 알지 못합니다. 나는 많은 시간을 보냈고, 시간을 보냈다. 그러나 아무것도 효과가 없었습니다. 누구든지 제 코드에 어떤 문제가 있는지 말해 줄 수 있습니까? PHP 메일() 함수가 HTML을 보내지 않습니다.

<?php 
$to = "[email protected]"; 
$today = date("Y/m/d"); 
$today = $today." 00:00"; 
$count_cash_inc = mysql_query("SELECT sum(income) as inc, sum(expense) as exp FROM journal_entry WHERE `date` >= '$today'"); 
$inc = mysql_fetch_array($count_cash_inc); 
$income_c = $inc['inc']; 
$epense_c = $inc['exp']; 
$counter_cash = $income_c - $epense_c; 
$subject = "Daily Report From Sri Sankalpa"; 
$headers = "From: [email protected]\r\n"; 
$headers .= "Reply-To: [email protected]\r\n"; 
$headers .= "CC: [email protected]\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=UTF-8\r\n"; 
$message = '<center><h1>Sri Sankalpa Daily Journal Report</h1>'; 
$message .= '<br><table><tr><td>Date</td><td>Total Income</td><td>Total Expense</td><td>Balance</td></tr>'; 
$message .= '<tr><td>'.date("d-m-Y").'</td><td>'.$income_c.'</td><td>'.$epense_c.'</td><td>'.$counter_cash.'</td></tr></table><br>'; 
$message .= '<p>To Know More... Please Click <strong><a target="_blank" href="http://srisankalpa.com/demo/journal_entry.php">HERE</a></strong></p></center>'; 
mail($to, $subject, $message, $headers); 
?> 

난 \ R \ R \ n 변화하는 캐릭터 = UTF-8 캐릭터 세트는 ISO-8859-1 = 제거 시도. 그러나 아무것도 작동하지 않습니다. 도움을 청했습니다. 아래 라인이없는 일반 텍스트로 메일을 완벽하게 보내십시오.

$headers .= "Content-Type: text/html; charset=UTF-8\r\n"; 
+1

간단한 'mail()'보다 고급이며 쉽게 오류를 추적 할 수있는 [PHPmailer] (http://phpmailer.worxware.com/)를 사용하십시오. –

+0

메일이 전송되었지만 Gmail에서 스팸을 제거했습니다 (스팸). 이를 막기 위해 phpmailer를 사용하십시오 –

+0

이 헤더를 추가하십시오'$ headers. = 'Content-type : text/html; charset = iso-8859-1 '. "\ r \ n"; "및 –

답변

1

php mail() 함수 사용을 중단하십시오! PHPMailer을 사용하고 SMTP를 통해 이메일을 보냅니다.

$mail->IsHTML(true);  // <=== call IsHTML() after $mail->Body has been set. 

예 :

<?php 
require("class.phpmailer.php"); 
$mail = new PHPMailer();$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->Host = "smtp1.example.com;smtp2.example.com"; 
$mail->SMTPAuth = true; 
$mail->Username = 'smtpusername'; 
$mail->Password = 'smtppassword'; 

$mail->From="[email protected]"; 
$mail->FromName="My site's mailer"; 
$mail->Sender="[email protected]"; 
$mail->AddReplyTo("[email protected]", "Replies for my site"); 

$mail->AddAddress("[email protected]"); 
$mail->Subject = "Test 1"; 

$mail->IsHTML(true); 
$mail->Body = "<h1>Test 1 of PHPMailer html</h1><img src="http://wallpaper-gallery.net/images/ball/ball-17.jpg" style="width:220px; height:59px; margin-bottom:15px"><p>This is a test</p>"; 
$mail->AltBody="This is text only alternative body."; 

if(!$mail->Send()) 
{ 
    echo "Error sending: " . $mail->ErrorInfo;; 
} 
else 
{ 
    echo "Letter is sent"; 
} 
?> 
+0

관련 OP와 관련이 없음 .. –

+1

더 나은 접근 방식을위한 OP 입력 –

+0

먼저 문제를 해결 한 다음 –

4

내 온라인 서버에 스크립트를 실행하고 나를 위해 당신의 SQL 쿼리와 변수를 포함하지 불구하고, 잘 작동하는 것 같다.

이메일이 전송되지 않는 이유는 여러 가지가있을 수 있습니다. 당신이 당신의 로컬 호스트에 설정된 메일 서버가없는 것처럼 당신이 당신의 로컬 호스트에서이 스크립트를 실행하는 경우

로컬 호스트의 메일 서버가

을 설정하지, mail 가장 가능성이 작동하지 않습니다. this answer on setting up a mail server on your localhost (XAMPP)을 읽으십시오.

확인 서버 로그

는 메일을 보낼 동안 올 수있는 오류에 대한 서버 로그에서 보라. 로그 파일은 일반적으로 logs 폴더 또는 그와 유사한 서버 내 루트 디렉토리에서 찾을 수 있습니다. This answer will help you identify the mail logs location.

메일이 호스트에 의해 차단되었습니다.

호스트와 확인하십시오. 호스팅 상대방에 따라 발신 이메일을 차단할 수도 있고 차단하지 않을 수도 있습니다. 많은 사람들이 PHP의 mail을 나쁜 의도로 사용하기 때문에 일부 호스트는 단순히이를 비활성화 할 수 있습니다. 그들은 대신 SMTP 서버를 사용하는 것을 선호합니다.

귀하의 메일은 스팸으로 표시되었을 수도 있습니다.

스팸 폴더를 확인하십시오. Gmail은 mail을 통해 전송되는 이메일의 팬이 아니며 이메일을 스팸 폴더에 넣을 가능성이 큽니다.

중복 행은 헤더 문자열에서 중복 줄 바꿈을 일으킬 수 \n\r를 사용하여, 헤더 문자열 어떤 경우에는

에서 휴식. 대신 \r 또는 \n을 사용해보세요.

문자 인코딩

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";보다는 $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";

더 나은 대안으로 전환하십시오 PHP의 당신이 PHPMailer 같은 것을 사용하여 더 나을 것 로컬 호스트에 메일을 보내기 위해 mail();

. 이렇게하면 SMTP 서버를 통해 메일을 보낼 수 있습니다. 유용한 자습서 here이 있습니다.

+0

공유 호스트에서 실행 중입니다. – SHAKTHI

+0

호스트가 '메일'의 발신 메일을 허용하지 않을 가능성이 있습니다. –

+0

오, 호스팅 지원 팀에 확인하겠습니다. 그러나 앞에서 언급 한 것처럼이 줄이 없으면 $ headers. = "Content-Type : text/html; charset = UTF-8 \ r \ n"; mail() 함수는 일반 텍스트처럼 잘 작동합니다. – SHAKTHI