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

标签 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="<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>&nbsp;</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>&nbsp;</p>';
    mail($to,$subject,$body_message,$mailheaders);
    return true;
}

关于php - echo json_encode 响应不适用于邮件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45432403/

相关文章:

PHPcs xml 配置禁用一些规则

JSON 将整数字段解码为字符串

php - 为什么即使我将 CURLOPT_PROXY 与 Curl 一起使用,我的原始 IP 仍然可见?

php - 在 Linux 中模拟向上箭头键

php - 为什么我的插入不能使用准备好的语句工作?

php - 使用 Php 从 MYSQL 数据库中提取注释导致意外的 JSON 结果

json - 使用 lombok 自定义序列化和反序列化字段名称

php - Curl shell脚本不给php文件变量

php - Graph Facebook API 在尝试发布到用户的墙上时出现 cURL 错误

php - Yii2 的嵌套集为 foreach() 提供的参数无效