简体   繁体   English

如何接收和使用从Mailgun发送的webhook数据?

[英]How do I receive and use webhook data sent from Mailgun?

I am using webhooks from the MailGun API to notify me when an email has been delivered. 我正在使用MailGun API中的webhooks在电子邮件发送时通知我。

I used the RequestBin service to see the data that is being sent: 我使用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

I am now trying to use their information sent via a POST array and insert it into my DB. 我现在正尝试使用通过POST阵列发送的信息并将其插入我的数据库。

The following variables I can grab directly from the POST array that they are sending: 我可以直接从他们发送的POST数组中获取以下变量:

$recipient_email = $_POST['recipient']; //This works
$timestamp = $_POST['timestamp']; //This works

The following variables I cannot get to directly from the POST array. 以下变量我无法直接从POST数组中获取。 They are within a datatype in the "message-headers" variable that I am not familiar with. 它们位于我不熟悉的“message-headers”变量中的数据类型中。 How can I access those? 我怎样才能访问这些?

$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));

Well, the message-headers are an array of arrays. 好吧, message-headers是一个数组数组。 For each sub-array the first item (key 0 ) is the "label", the second item (key 1 ) is the "value". 对于每个子阵列,第一项(键0 )是“标签”,第二项(键1 )是“值”。 So you could do something like this: 所以你可以这样做:

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 ...*/
    }
}

(although building up a map of $header[0] vs variable name and then simply looping without the switch might be somewhat easier to read, but that's just my taste.) (虽然建立一个$header[0] vs变量名称的地图然后简单地循环而没有switch可能会更容易阅读,但这只是我的口味。)

But PLEASE parameterize the SQL query! 但是PLEASE参数化SQL查询! Otherwise you will get hacked in no time at all. 否则你很快就会被黑客攻击。

This what I have done in Django app.. 这就是我在Django应用程序中所做的..

I was just logged all post dat into the log file and monitor it using tail command 我刚刚将所有post dat记录到日志文件中并使用tail命令监视它

1) may be you can do same in PHP like logging the code into the error log 1)您可以在PHP中执行相同操作,例如将代码记录到错误日志中

2) error_log(json_encode($_POST)); 2)error_log(json_encode($ _ POST));

3) check monitor the post data which you received from the mailgun 3)检查监控从邮件枪收到的帖子数据

4) and use the required params :) 4)并使用所需的参数:)

Here is the python code: 这是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', '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM