java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

mailgun 接口api 读取邮件发送状态

今天发现其实任何一个邮件群发服务器都会提供可以识别的邮件状态,提供给月小升这样懂点技术的人来给java-er.com的博客来使用。
登录mailgun 来进行接口对接

到mailgun得接口 找到webhooks

建立一个网页
http://java-er.com/mailgun/pf.php

代码如下

		$post = $GLOBALS['HTTP_RAW_POST_DATA'];
		$data = $post."\n####################\n Email:$email \n";
		$fp = fopen("/test/mailgun.txt", "a+") or die("Unable to open file!");
		$len = fwrite($fp, $data);
		fclose($fp);

访问接口返回数据

{"signature": {"timestamp": "1536379081", "token": "e034e70288b68d4c150918878b0f3b6c5fdd4e65f63e50eadc", "signature": "bdd026e690d57
661a8d3ddde4c2352d033c0420ddd120bebec8ae7cf81493def"}, "event-data": {"severity": "permanent", "tags": ["my_tag_1", "my_tag_2"], "ti
mestamp": 1521233195.375624, "storage": {"url": "https://se.api.mailgun.net/v3/domains/hi.java-er.com/messages/message_key", "key"
: "message_key"}, "log-level": "error", "event": "failed", "campaigns": [], "reason": "suppress-bounce", "user-variables": {"my_var_
1": "Mailgun Variable #1", "my-var-2": "awesome"}, "flags": {"is-routed": false, "is-authenticated": true, "is-system-test": false, 
"is-test-mode": false}, "recipient-domain": "example.com", "envelope": {"sender": "bob@hi.java-er.com", "transport": "smtp", "targ
ets": "alice@example.com"}, "message": {"headers": {"to": "Alice ", "message-id": "20130503192659.13651.20287@hi.
java-er.com", "from": "Bob ", "subject": "Test permanent_fail webhook"}, "attachments": [], "size": 111}, "r
ecipient": "alice@example.com", "id": "G9Bn5sl1TC6nu79C8C0bwg", "delivery-status": {"code": 605, "message": "", "attempt-no": 1, "de
scription": "Not delivering to previously bounced address", "session-seconds": 0}}}
public function pf(){
		$post = $GLOBALS['HTTP_RAW_POST_DATA'];

		$arr = json_decode($post,true);
		
		$arrlist = print_r($arr,true);

		//$data = $post."\n####################\n Email:$email \n";

		var_dump($arr);

		$data = $arrlist."\n####################\n Email:$email \n";
		$fp = fopen("/test/mailgun.txt", "a+") or die("Unable to open file!");
		$len = fwrite($fp, $data);
		fclose($fp);

	}
	Array
(
    [signature] => Array
        (
            [timestamp] => 1536379459
            [token] => 1e4b08068cab21b9cb4cea373a8ad3072ae7f8c848cc66b40f
            [signature] => 4ed053cd5b9ba40e5615bae33b07774c0e9b520ab2311d9e9315f70f65a1930c
        )

    [event-data] => Array
        (
            [severity] => permanent
            [tags] => Array
                (
                    [0] => my_tag_1
                    [1] => my_tag_2
                )

            [timestamp] => 1521233195.3756
            [storage] => Array
                (
                    [url] => https://se.api.mailgun.net/v3/domains/hi.java-er.com/messages/message_key
                    [key] => message_key
                )

            [log-level] => error
            [event] => failed
            [campaigns] => Array
                (
                )

            [reason] => suppress-bounce
            [user-variables] => Array
                (
                    [my_var_1] => Mailgun Variable #1
                    [my-var-2] => awesome
                )

            [flags] => Array
                (
                    [is-routed] => 
                    [is-authenticated] => 1
                    [is-system-test] => 
                    [is-test-mode] => 
                )

            [recipient-domain] => example.com
            [envelope] => Array
                (
                    [sender] => bob@hi.java-er.com
                    [transport] => smtp
                    [targets] => alice@example.com
                )

            [message] => Array
                (
                    [headers] => Array
                        (
                            [to] => Alice 
                            [message-id] => 20130503192659.13651.20287@hi.java-er.com
                            [from] => Bob 
                            [subject] => Test permanent_fail webhook
                        )

                    [attachments] => Array
                        (
                        )

                    [size] => 111
                )

            [recipient] => alice@example.com
            [id] => G9Bn5sl1TC6nu79C8C0bwg
            [delivery-status] => Array
                (
                    [code] => 605
                    [message] => 
                    [attempt-no] => 1
                    [description] => Not delivering to previously bounced address
                    [session-seconds] => 0
                )

        )

)

mailgun 如果你带上 print_r($arr);

$arr = json_decode($post,true);
		print_r($arr);

可以直接在接口测试的地方打印返回的网页信息,这一点比aws得邮件服务要智能。

拿到了数据就是入库处理的事情了。

$post = $GLOBALS['HTTP_RAW_POST_DATA'];

		$arr = json_decode($post,true);
		
		$arrlist = print_r($arr,true);

		print_r($arr);

		//$data = $post."\n####################\n Email:$email \n";

		if(is_array($arr)){
			$x = $arr['event-data'];
			$d['email'] = $x['recipient'];
			$d['action'] = $x['reason'];
			$d['status'] = $x['delivery-status']['code'];
			$d['log'] = $x['delivery-status']['description'];
			$d['platform'] = "1"; //1 mailgun 2 aws
			$d['addtime'] = time();

			$w['email'] = $x['recipient'];
			$d2['remark'] = "mailgun bounce code";
			$d2['status'] = 2;
			$d2['not_mailgun'] = 2;

			M("has_email")->where($w)->save($d2);
			M("has_email_errlog")->data($d)->add();
		}




		$data = $arrlist."\n####################\n Email:$email \n";
		$fp = fopen("/test/mailgun.txt", "a+") or die("Unable to open file!");
		$len = fwrite($fp, $data);
		fclose($fp);


This entry was posted in PHP and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/mailgun-email-send/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

Leave a Reply