我正在使用MailGun API中的webhooks在电子邮件发送时通知我。
我使用RequestBin服务查看正在发送的数据:
domain: domain.com
token: 6o02nxnu-7grjkvkzxdyn2lsm0w7fagidgxzon8-cucz8u15w0
signature: a4d27e390495691fb4b8fb76b3b8a71c90cf8cd467140f5c3d36e023ec343e5c
Message-Id: <20140721155006.120305.72293@domain.com>
timestamp: 1405957808
X-Mailgun-Sid: WyJjZjMyNCIsICJjaHJpc3NlY2tsZXJAZ21haWwuY29tIiwgIjAxNzBiNSJd
message-headers: [["Received", "by luna.mailgun.net with HTTP; Mon, 21 Jul 2014 15:50:06 +0000"], ["Content-Type", ["multipart/alternative", {"boundary": "21586684bc984a0cbc485d4a862fc34c"}]], ["Mime-Version", "1.0"], ["Subject", "Please Check Your Account"], ["From", "Sender Name <testemail@domain.com>"], ["To", "Chris <anotheremail@gmail.com>"], ["X-Mailgun-Sid", "WyJjZjMyNCIsICJjaHJpc3NlY2tsZXJAZ21haWwuY29tIiwgIjAxNzBiNSJd"], ["Date", "Mon, 21 Jul 2014 15:50:08 +0000"], ["Sender", "testemail@domain.com"]]
recipient: email@domain.com
event: delivered
我现在正尝试使用通过POST阵列发送的信息并将其插入我的数据库。
我可以直接从他们发送的POST数组中获取以下变量:
$recipient_email = $_POST['recipient']; //This works
$timestamp = $_POST['timestamp']; //This works
以下变量我无法直接从POST数组中获取。它们位于&#34; message-headers&#34;中的数据类型中。我不熟悉的变量。我怎样才能访问这些?
$sender_name = ?;
$sender_email = ?;
$recipient_name = ?;
$subject = ?;
$sql = 'INSERT into mail SET
from_name = "'.$sender_name.'",
from_email = "'.$sender_email.'",
to_name = "'.$recipient_name.'",
to_email = "'.$recipient_email.'",
subject = "'.$subject.'",
date = "'.$timestamp.'"';
$result = $conn->query($sql) or die(mysqli_error($conn));
答案 0 :(得分:1)
嗯,message-headers
是一个数组数组。对于每个子数组,第一项(键0
)是“标签”,第二项(键1
)是“值”。所以你可以这样做:
foreach ($POST['message-headers']} as $i => $header) {
switch( $header[0] ) {
case 'Subject'
$subject = $header[1];
break;
case 'Sender'
$sender_email = $header[1];
break;
/* Any other cases that I'm too lazy for now ...*/
}
}
(虽然建立一个$header[0]
vs变量名称的地图,然后简单地在没有switch
的情况下循环可能会更容易阅读,但这只是我的口味。)
但是PLEASE参数化SQL查询!否则你很快就会被黑客攻击。
答案 1 :(得分:0)
这是我在Django应用程序中所做的..
我刚刚将所有post dat记录到日志文件中并使用tail命令
进行监视1)你可以在PHP中做同样的事情,比如将代码记录到错误日志中
2)error_log(json_encode($ _ POST));
3)检查监控从mailgun收到的帖子数据
4)并使用所需的参数:)
这是python代码:
sender = request.POST.get('sender')
recipient = request.POST.get('recipient')
subject = request.POST.get('subject', '')
body_plain = request.POST.get('body-plain', '')
body_without_quotes = request.POST.get('stripped-text', '')
body_html = request.POST.get('body-html', '')