2012-10-17 5 views
2

성공적인 구매 후에 페이팔 IPN을 사용하여 이메일을 보내는 스크립트가 있습니다. HTML 이메일을 보내도록 스크립트를 수정하려면 어떻게해야합니까?PHP로 HTML 이메일 보내기?

<?php 

// read the post from PayPal system and add 'cmd' 
$req = 'cmd=_notify-validate'; 

foreach ($_POST as $key => $value) { 
$value = urlencode(stripslashes($value)); 
$req .= "&$key=$value"; 
} 

// post back to PayPal system to validate 

$header = "POST /cgi-bin/webscr HTTP/1.1\r\n"; 

// If testing on Sandbox use: 
// $header .= "Host: www.sandbox.paypal.com:443\r\n"; 
$header .= "Host: ipnpb.paypal.com:443\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n"; 

// If testing on Sandbox use: 
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); 
$fp = fsockopen ('ssl://ipnpb.paypal.com', 443, $errno, $errstr, 30); 

// assign posted variables to local variables 
$item_name = $_POST['item_name']; 
$item_number = $_POST['item_number']; 
$payment_status = $_POST['payment_status']; 
$payment_amount = $_POST['mc_gross']; 
$payment_currency = $_POST['mc_currency']; 
$txn_id = $_POST['txn_id']; 
$receiver_email = $_POST['receiver_email']; 
$payer_email = $_POST['payer_email']; 

//set email variables 
$From_email = "From: [email protected]"; 
$Subject_line = "Thanks"; 

$email_msg = "Thanks for purchasing my item. Your order will be delivered in 3-4 days. We  appreciate your business."; 
$email_msg .= "\n\nThe details of your order are as follows:"; 
$email_msg .= "\n\n" . "Transaction ID: " . $txn_id ; 
$email_msg .= "\n" . "Payment Date: " . $payment_date; 

if (!$fp) { 
// HTTP ERROR 
} else { 
fputs ($fp, $header . $req); 
while (!feof($fp)) { 
$res = fgets ($fp, 1024); 
if (strcmp ($res, "VERIFIED") == 0) { 
// check the payment_status is Completed 
// check that txn_id has not been previously processed 
// check that receiver_email is your Primary PayPal email 
// check that payment_amount/payment_currency are correct 
// process payment 

$mail_From = $From_email; 
$mail_To = $payer_email; 
$mail_Subject = $Subject_line; 
$mail_Body = $email_msg; 

mail($mail_To, $mail_Subject, $mail_Body, $mail_From); 


} 
else if (strcmp ($res, "INVALID") == 0) { 
// log for manual investigation 

$mail_From = $From_email; 
$mail_To = $receiver_email; 
$mail_Subject = "INVALID IPN POST"; 
$mail_Body = "INVALID IPN POST. The raw POST string is below.\n\n" . $req; 

mail($mail_To, $mail_Subject, $mail_Body, $mail_From); 

} 
} 
fclose ($fp); 
} 
?> 
+0

HTML 이메일은 무엇을 의미합니까? 이 코드는 괜찮아 보입니다. – Flaxbeard

+0

이 작업을 수행하는 방법에 대한 예는 http://stackoverflow.com/questions/1361762/how-to-send-html-mails-using-pear-mail을 참조하십시오. – bartlaarhoven

+0

PEAR :: mail을 사용하는 것이 좋습니다. 그것은 매우 쉽고 간단합니다 ... http://pear.php.net/package/Mail/redirected – rws907

답변

7

mail_from뿐만 아니라 콘텐츠 유형이 포함 된 전체 헤더를 보내야합니다. Heres는 예를 들어

$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
$headers .= $mail_From . "\r\n"; 
mail($mail_To,$mail_Subject,$mail_Body,$headers); 
코드에서

http://www.w3schools.com/php/func_mail_mail.asp

:

$ mail_From = $ FROM_EMAIL; $ mail_To = $ payer_email; $ mail_Subject = $ Subject_line; $ mail_Body = $ email_msg;

mail ($ mail_To, $ mail_Subject, $ mail_Body, $ mail_From);

$ mail_From은 네 번째 매개 변수 (머리글)입니다. HTML 이메일에 대한 전체 헤더를 포함 더한 새로운 캐릭터 만들기 : 당신이 $ 헤더를 에코 경우 $ _From

$mail_From = $From_email; 
$mail_To = $payer_email; 
$mail_Subject = $Subject_line; 
$mail_Body = $email_msg; 
//start $headers 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; //adds content type to headers 
$headers .= $mail_From . "\r\n"; //adds the sender details 
mail($mail_To,$mail_Subject,$mail_Body,$headers); //sends the email 

그것이 PHP를 통해이

MIME-Version:1.0 
Content-type:text/html;charset=iso-8859-1 
From:[email protected] 
+0

헤더는 어디에 두어야합니까? // 이메일 변수를 설정 하시겠습니까? 그들은 다른 $ 헤더를 방해하지 않습니까? – Anthony

+0

머리글은 이름 값 쌍의 일종이므로, – geekman

+0

헤더는 그림과 같이 문자열의 이름 값 쌍입니다.. =.이 문자열에 추가됩니다. – geekman

2

메일 본문에 HTML을 넣으면 대부분의 메일 클라이언트가 알아낼 수 있어야합니다.

보다 정확한 대답은 적절한 Content-Type 헤더를 메일에 포함합니다 (text/html).

완전한 대답은 내가하는 일이 될 것입니다 :

<?php 
$html = <<<HTML 
<!DOCTYPE HTML> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" value="text/html; charset=iso-8859-1" /> 
     <title>Title here</title> 
    </head> 
    <body> 
     <p>My mail content here.</p> 
     <hr /> 
     <p style="font-size: 8pt; font-style: italic;">Some signature here</p> 
    </body> 
</html> 
HTML; 
$text = preg_replace("(\n{2,})","\n",strip_tags(str_replace(Array("\t","<hr />"),Array("","===================="),$html))); 
$seperator = uniqid(); 
mail("[email protected]","Title here", 
     "--".$seperator."\r\n" 
     ."Content-Type: text/plain\r\n\r\n" 
     .$text."\r\n" 
     ."--".$seperator."\r\n" 
     ."Content-Type: text/html\r\n\r\n" 
     .$html."\r\n" 
     ."--".$seperator."--", 
    "From: My name <[email protected]>\r\n" 
    ."MIME-Version: 1.0\r\n" 
    ."Content-Type: multipart/alternative; boundary=".$seperator); 
?> 

을뿐만 아니라이 클라이언트에서 어떤 추측을 떠나 모든 올바른 헤더를 보낼 않습니다, 그것은 또한 사용자의 돈 '에 대해 자동으로 일반 텍스트 버전을 생성 HTML 이메일을 원하지 않습니다.

0

보내기 HTML 이메일과 같은 것하면 동일한 사용 css tricks

볼 수

mail($to, $subject, $message, $headers); 

$to = '[email protected]'; 

$subject = 'Website Change Reqest'; 

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; 
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; 
$headers .= "CC: [email protected]\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

$message = '<html><body>'; 
$message .= '<h1>Hello, World!</h1>'; 
$message .= '</body></html>'; 

소스 및 donwload : 텍스트 이메일로 메일 기능