2016-05-19 13 views
0

Dies ist die Mandrill-Vorlage (Paste html in ONLINE HTML-Editor) verwendet wird:Wie wird JSON an Mandrill REST API übergeben?

<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.2/handlebars.min.js"></script> <script id="entry-template" type="text/x-handlebars-template"> <table style="border-collapse: collapse;padding:0;margin:0 auto;"> </td></tr> <tr> <td colspan='9' style="padding:15px; border-width:1px; border-style:solid; border-color:#1bae7e"> <div style="text-transform:uppercase;font-weight:bold;font-family:sans-serif;font-size:25px;padding-bottom: 5px;color:#414141;">{{customer_name}}</div> <div style="font-weight:normal;font-family:sans-serif;font-size:20px;color:#414141;">Load Updates for {{run_date}}</div> </td> </tr> {{#data}} <td colspan='9' style="text-align:center;padding:15px; border-color:#1bae7e; font-family:sans-serif;font-weight:bold;font-size:20px"> {{label}} </td> <tr> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">ORDER</th> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">CUSTOMER BOL</th> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">STATUS</th> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">ORIGIN</th> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">DESTINATION</th> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">DELIVERY DATE</th> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">CURRENT LOCATION</th> <th style="vertical-align:top; font-size:12px; padding:10px; font-weight:bold;font-family:sans-serif; background-color:#3a3a3a;color:#a5a3a3;border-width:1px 1px 1px;border-color:#3a3a3a #1bae7e #FFF;border-style:solid;text-align:left">MILES OUT</th> </tr> {{#each loads}} <tr> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{order_id}} </td> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{bol_number}} </td> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{status}} </td> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{pickup_city}}, {{pickup_state}}<br/> <i>{{origin_actual_arrival_date}}</i> </td> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{dest_city}}, {{dest_state}}<br/> <i>{{dest_sched_late_arrival_date}}</i> </td> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{delivered_date}} </td> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{current_location_city}}, {{current_location_state}} <br/> <i>as of {{current_location_date}}</i> </td> <td style="font-size:12px; padding:10px; font-family:sans-serif; background-color:#1bae7e;border-width:0px 1px 1px;border-color:#a5a3a3 #a5a3a3;border-style:solid;color:#FFF;"> {{miles_remaining}} </td> </tr> {{/each}} {{/data}} </table> </script> </head> <body> <script> var BODY = document.getElementById("entry-template"); var template = Handlebars.compile(BODY.innerHTML); var dom = template({ "customer_name":"Super Foods", "run_date":"10/14/2015", "data":[ { "label":"Meat", "loads":[ { "order_id": "456123", "miles_remaining": "337", "shipped_date": "10/30/15 08:00", "pickup_city": "FORT WORTH", "pickup_state": "TX", "pickup_zip": "76106", "dest_city": "AMARILLO", "dest_state": "TX", "delivered_date": "11/02/15 19:00", "current_location_city":"Reno", "current_location_state":"NV", "current_location_date":"10/31/15 09:45", "status":"Picking Up", "bol_number":"587975", "origin_actual_arrival_date":"10/31/15 09:45", "dest_sched_late_arrival_date":"11/2/15 16:00", }, { "order_id": "789456", "miles_remaining": "2210", "shipped_date": "10/29/15 13:00", "pickup_city": "FREMONT", "pickup_state": "OH", "pickup_zip": "43420", "dest_city": "MIRA LOMA", "dest_state": "CA", "delivered_date": "10/31/15 12:00", "current_location_city":"Reno", "current_location_state":"NV", "current_location_date":"10/31/15 09:23", "status":"In Transit", "bol_number":"123456", "origin_actual_arrival_date":"10/31/15 09:45", "dest_sched_late_arrival_date":"11/2/15 16:00", } ] }, { "label":"Other", "loads":[ { "order_id": "659832", "miles_remaining": "0", "shipped_date": "10/30/15 09:30", "pickup_city": "FREMONT", "pickup_state": "OH", "pickup_zip": "43420", "dest_city": "MIRA LOMA", "dest_state": "CA", "delivered_date": "10/31/15 06:00", "current_location_city":"Reno", "current_location_state":"NV", "current_location_date":"10/31/15 10:14", "status":"Delivering", "bol_number":"L0314PS", "origin_actual_arrival_date":"10/31/15 09:45", "dest_sched_late_arrival_date":"11/2/15 16:00", } ] } ] }); document.body.innerHTML = dom; </script> </body></html> 

Hier ist, was meine Post-Anforderung wie folgt aussieht:

{ 
"key": "pfxrmv.................", 
"template_name": "customer_order", 
"template_content" : [], 
"message": { 
    "subject": "This is my subject", 
    "from_email": "[email protected]", 
    "from_name": "Sample Name", 
    "to": [ 
     { 
      "email": "[email protected]", 
      "name": "Sample2 Name", 
      "type": "to" 
     } 
    ], 
    "auto_text": true, 
    "inline_css": true, 
    "merge": true, 
    "merge_language": "handlebars", 
    "important": false, 
    "merge_vars": [ 
     { 
      "vars": [ 
       { 
        "name": "entry-template", 
        "content": [ 
         { 
          "customer_name": "SUPER FOOD" 
         } 
        ] 
       } 
      ] 
     } 
    ]   
}, 
"async": true} 

AUSGABE: einen Beitrag in Doing Postman (REST-Client) oder hier mandrillapp.com, die E-Mail sendet, aber die E-Mail erscheint leer (kein Inhalt in E-Mail). Das übergebene JSON-Objekt wird nicht verwendet. Wie kann ich das JSON-Objekt korrekt übergeben?

Dinge zu beachten:

  1. Might Objekt JSON in der falschen Stelle werden eingefügt. Gerade jetzt ist es in "merge_vars" : [{ "vars": [{ "content" : [{ }]
  2. JSON-Objekt gehen könnte in dem „template_content“
  3. Das JSON-Objekt in diesem Beispiel verwendet werden, kann in dem <Script> -Tag aus dem HTML gefunden wird oben gepostet.

Antwort

0

Hier ist, wie ich es an die Arbeit:

dieses JSON-Objekt zu übergeben:

{ 
"customer_name": "Company Foods", 
"run_date": "05/14/2016", 
"data": [ 
    { 
     "label": "Type 1", 
     "loads": [ 
      { 
       "order_id": "8675432", 
       "miles_remaining": "169" 
      }, 
      { 
       "order_id": "8675421", 
       "miles_remaining": "337" 
      } 
     ] 
    }, 
    { 
     "label": "Type 2", 
     "loads": [ 
      { 
       "order_id": "8675421", 
       "miles_remaining": "0" 
      } 
     ] 
    } 
]} 

Setzen Sie jede Ebene Ihrer JSON-Objekt in einem eigenen Objekt unter "global_merge_vars": []

Hier ist, wie die Anfrage aussehen sollte:

{ 
"key": "pfxrmv.................", 
"template_name": "customer_order", 
"template_content": [], 
"message": { 
    "subject": "This is my subject", 
    "from_email": "[email protected]", 
    "from_name": "Sample Name", 
    "to": [ 
     { 
      "email": "[email protected]", 
      "name": "Sample2 Name", 
      "type": "to" 
     } 
    ], 
    "auto_text": true, 
    "inline_css": true, 
    "merge": true, 
    "merge_language": "handlebars", 
    "important": false, 
    "global_merge_vars": [ 
     { 
      "name": "customer_name", 
      "content": "Company Foods" 
     }, 
     { 
      "name": "data", 
      "content": [ 
      { 
        "label":"Type 1", 
        "loads":[ 
         { 
          "order_id": "8675432", 
          "miles_remaining": "169" 
         }] 

      }] 
     } 
    ] 
}, 
"async": true}