echo json_encode响应不适用于邮件功能

时间:2017-08-01 08:12:58

标签: php json curl

我已使用curl生成报告并向用户发送邮件。

这是我的代码

$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="test@gmail.com";
$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 <do_not_reply@mcs.com>";
    $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;
}

1 个答案:

答案 0 :(得分: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 <do_not_reply@mcs.com>";
    $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;
}