我使用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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7a6b7d7a4e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a>";
$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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43272c1c2d2c371c3126332f3a032e20306d202c2e" rel="noreferrer noopener nofollow">[email protected]</a>>";
$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> </p>';
mail($to,$subject,$body_message,$mailheaders);
return true;
}
最佳答案
我认为您的 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 <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9cdc6f6c7c6ddf6dbccd9c5d0e9c4cada87cac6c4" rel="noreferrer noopener nofollow">[email protected]</a>>";
$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> </p>';
mail($to,$subject,$body_message,$mailheaders);
return true;
}
关于php - echo json_encode 响应不适用于邮件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432403/