Unsubscribe to Speak To Me Catalog newsletter

To do
#newsletters #SparkPost #unsubscribes #webhooks


need to know how to extract the “rcpt_to” from the JSON post transaction …
from this hyperliink in the bulk email …
<a data-msys-unsubscribe="1"
i assume i get this result in the POST transaction ...
    "msys": {
        "unsubscribe_event": {
            "template_version": "VERSION_NUMBER_OF_TEMPLATE",
            "template_id": "TEMPLATE_ID_VALUE",
            "ip_address": "IP_ADDRESS_OF_THE_UNSUBSCRIBE_SOURCE",
            "target_link_url": "HREF_OF_UNSUBSCRIBE_LINK",
            "rcpt_tags": [
            "customer_id": "YOUR_SPARKPOST_ACCOUNT_ID",
            "transmission_id": "SPARKPOST_TRANSMISSION_ID",
            "user_agent": "USER_AGENT_STRING",
            "type": "link_unsubscribe",
            "rcpt_meta": {
                "META_KEYS": "META_VALUES"
            "message_id": "MESSAGE_ID",
            "rcpt_to": "RECIPIENT_EMAIL_ADDRESS",
            "accept_language": "en-US,en;q=0.8",
            "target_link_name": "NAME_OF_UNSUBSCRIBE_LINK"

hmmm … see this … hopefully available in my version



true in json_decode simply makes it an associative array instead of an object. Which only affects the syntax you then use to access it’s parts.

For the first it would be $j->a and $j->b

For the second it would be $j[‘a’] and $j[‘b’]

Otherwise, all is the same … including print_f() and var_dump() … except that associative arrays are just a micro hair faster to access than objects … but only an amount a benchmark test could notice.

the problem is, that given the sparkpost POST (specified i guess in Unsubscribe to Speak To Me Catalog newsletter (comment 77012))

and this code in my program …
$rcpt_to= "email=" . $r[’msys’][’unsubscribe_event’][’rcpt_to’];
echo $rcpt_to;

The response according to sparkpost is …

Your server responded like this:   (in other words, i don’t know how to retrieve the data from the tree)

HTTP/1.1 200
Date: Wed, 03 May 2017 21:34:24 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8


Okay. By what you “assumed” the post should look like, it would have worked.

What does the post data really look like?

"Array\n(\n [0] => Array\n (\n [msys] => Array\n (\n [unsubscribe_event] => Array\n (\n [type] => link_unsubscribe\n [campaign_id] => Example Campaign Name\n [customer_id] => 1\n [delv_method] => esmtp\n [event_id] => 92356927693813856\n [friendly_from] => sender@example.com\n [ip_address] =>\n [ip_pool] => Example-Ip-Pool\n [mailfrom] => recipient@example.com\n [message_id] => 000443ee14578172be22\n [msg_from] => sender@example.com\n [msg_size] => 1337\n [num_retries] => 2\n [queue_time] => 12\n [rcpt_meta] => Array\n (\n [customKey] => customValue\n )\n\n [rcpt_tags] => Array\n (\n [0] => male\n [1] => US\n )\n\n [rcpt_to] => recipient@example.com\n [raw_rcpt_to] => recipient@example.com\n [rcpt_type] => cc\n [routing_domain] => example.com\n [sending_ip] =>\n [subaccount_id] => 101\n [subject] => Summer deals are here!\n [template_id] => templ-1234\n [template_version] => 1\n [timestamp] => 1454442600\n [transmission_id] => 65832150921904138\n [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36\n )\n\n )\n\n )\n\n)\nemail="
which is from …


from …

echo $raw;

here is the result …
[{"msys":{"unsubscribe_event":{"type":"link_unsubscribe","campaign_id":"Example Campaign Name","customer_id":"1","delv_method":"esmtp","event_id":"92356927693813856","friendly_from":"sender@example.com","ip_address":"","ip_pool":"Example-Ip-Pool","mailfrom":"recipient@example.com","message_id":"000443ee14578172be22","msg_from":"sender@example.com","msg_size":"1337","num_retries":"2","queue_time":"12","rcpt_meta":{"customKey":"customValue"},"rcpt_tags":["male","US"],"rcpt_to":"recipient@example.com","raw_rcpt_to":"recipient@example.com","rcpt_type":"cc","routing_domain":"example.com","sending_ip":"","subaccount_id":"101","subject":"Summer deals are here!","template_id":"templ-1234","template_version":"1","timestamp":"1454442600","transmission_id":"65832150921904138","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"}}}]

… looks to me that the array is nested in an extra [  … ]

Yep. They must be able to send you multiple.

So just,

$r = file_get_contents(’php://input’);
$r = json_decode($r, true);
$r = $r[0];
$email = $r[’msys’][’unsubscribe_event’][’rcpt_to’];

Success …
#thanks → nathan


#kudos → nathan