PHP - 如何接收JSON字符串并通过电子邮件发送它

时间:2017-10-28 09:43:51

标签: php json email

我有一个表单(来自Cognito),它将JSON字符串发送到我的网站脚本,我想通过电子邮件发送字符串。这是我的代码:

<?php
$to = "stuff@greenery.me.uk";
$subject = 'has JSON arrived yet';
$body = file_get_contents('php://input');
// $body = 'replace the file_get';
$from = 'From: "thos"<thosgreen@gmail.com>';
mail($to, $subject, $body, $from) ;
?>

我知道表单正在发送JSON,因为我已经使用RequestBin拦截了它。我知道我的脚本可以发送电子邮件,因为如果我退出行$body = 'replace the file_get';它会发送一封电子邮件。所以问题似乎是声明$body = file_get_contents('php://input'); - 但这正是我所看到的所有页面都是正确的命令。因为JSON是一个字符串,所以它应该被接受作为邮件的参数,但我尝试使用显式字符串连接,这没有任何区别。

我已经将我的代码与SO和其他地方的其他代码进行了比较,并且看起来正确,但显然不是。所以我很难过。我做错了什么?

1 个答案:

答案 0 :(得分:0)

你检查过表格的enctype吗? php://输入不能使用文档中所述的enctype =“multipart / form-data”。 http://php.net/manual/en/wrappers.php.php#wrappers.php.input