2017-08-01 67 views
2

我已經使用curl生成的報告和發送郵件給用戶。回聲json_encode響應不起作用的郵件功能

這裏是我的代碼

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, count($post)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); 
$ret = curl_exec($ch); 
$ret = json_decode($ret, true); 
curl_close($ch); 
print_r($ret); 

這裏我用我的郵件功能和返回JSON編碼迴應,但迴應沒有得到。 如果我評論send_report_to_user功能,那麼我能夠得到響應

$to="[email protected]"; 
$name="Test"; 
$report_links='MYREPORT LINKS'; 
send_report_to_user($to,$name,$report_links); 

echo json_encode(array('status'=>'success','message'=>"Report has been generated successfully. Please check your mail to view reports.")); 
die; 

function send_report_to_user($to,$name,$report_links) 
{ 
    $mailheaders .= "MIME-Version: 1.0\r\n"; 
    $mailheaders .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
    $mailheaders .= "From: Mcs <[email protected]>"; 
    $subject = "Your Report is Ready"; 
    $body_message='<p>Your Report is now ready to be viewed here:</p> 
        <p><a href="'.$report_links.'" target="_blank">Click Here to view report</a></p> 
        <p>&nbsp;</p>'; 
    mail($to,$subject,$body_message,$mailheaders); 
    return true; 
} 
+0

你得到任何錯誤或異常試試? –

+0

@RAUSHANKUMAR沒有代碼工作正常,只有響應沒有得到。也收到郵件 –

+0

檢查你在這裏收到的郵件($ to,$ subject,$ body_message,$ mailheaders);' –

回答

2

我認爲你有問題,你send_report_to_user功能。您正在使用變量$mailheaders但尚未定義它。在使用它之前嘗試定義變量,它可能適用於你。

請與下面的代碼

function send_report_to_user($to,$name,$report_links) 
{ 
    $mailheaders = ''; 
    $mailheaders .= "MIME-Version: 1.0\r\n"; 
    $mailheaders .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
    $mailheaders .= "From: Mcs <[email protected]>"; 
    $subject = "Your Report is Ready"; 
    $body_message='<p>Your Report is now ready to be viewed here:</p> 
        <p><a href="'.$report_links.'" target="_blank">Click Here to view report</a></p> 
        <p>&nbsp;</p>'; 
    mail($to,$subject,$body_message,$mailheaders); 
    return true; 
} 
+0

感謝現在工作的人。你節省了我的一天。 –