REST Email Marketing API DocumentationExample 6 — Send a Document
This example shows sending a Document in Symphonie using dynamic substitutions
to mail-merge the contents. We'll assume the content has already been created with multiple
DynamicTags that we'll populate through this API call. |
private static void RestExample6(System.Collections.Generic.List<string> topics, string brandName, string email)
{
try
{
string htmlTable = "<table><tbody><tr><td>Topic name</td><td>Status</td></tr></tbody>";
foreach (string row in topics)
htmlTable += "<tr><td>" + row + "</td><td>Subscribed</td></tr>";
htmlTable += "</table>";
string url = "http://www.example.com/api/rest/Documents/Queue";
System.Text.StringBuilder data = new StringBuilder();
data.Append("{'accountName':'acme', 'login':'ApiUser', 'password':'dfh45ywhhsb', 'documentName':'Welcome1','emailAddress':");
data.AppendFormat("'{0}'", email);
data.Append(",'substitutions':{ 'HtmlTable':");
data.AppendFormat("'{0}'", htmlTable);
data.AppendFormat(", 'brandName':'{0}'", brandName);
data.Append("}}");
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
requestWriter.Write(data);
string result;
System.Net.WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream())
using (StreamReader responseReader = new StreamReader(webStream))
result = responseReader.ReadToEnd();
if (result.StartsWith("Success"))
System.Console.WriteLine("Document queued to be sent. You should receive it shortly.");
else
System.Console.WriteLine("Error: {0}", result);
}
catch ( System.Exception e)
{
System.Console.WriteLine(e.Message);
}
}
# encoding: utf-8
require 'rest-client'
require 'json'
topics = ["Apple news", "Samsung news"]
brandName = "TechTips.com"
email = "joe@example.com"
htmlTable = "<table><tbody><tr><td>Topic name</td><td>Status</td></tr></tbody>"
for row in topics
htmlTable += "<tr><td>" + row + "</td><td>Subscribed</td></tr>"
end
htmlTable += "</table>"
url = "http://www.example.com/api/rest/Documents/Queue"
data = {"accountName":"acme", "login":"ApiUser", "password":"xvc67ysvd76svdh", "documentName":"Welcome1",
"emailAddress":email,"substitutions":{"HtmlTable":htmlTable, "brandName":brandName}}
resp = RestClient.post url, data.to_json, {"Content-Type": "application/json"}
if resp.code != 200
puts "Document send failed!"
puts resp.code
return
end
if resp.body != "Success"
puts "An error occurred"
puts resp.body
return
end
import requests
import json
def sendDocument(topics, brandName, email):
htmlTable = "<table><tbody><tr><td>Topic name</td><td>Status</td></tr></tbody>"
for row in topics:
htmlTable += "<tr><td>" + row + "</td><td>Subscribed</td></tr>"
htmlTable += "</table>"
url = "http://www.example.com/api/rest/Documents/Queue"
data = {"accountName":"acme", "login":"ApiUser", "password":"dfb4w5y4thwrb", "documentName":"Welcome1",
"emailAddress":email,"substitutions":{"HtmlTable":htmlTable, "brandName":brandName}}
resp = requests.post(url, json=data, headers={"Content-Type": "application/json"})
if resp.status_code != 200:
print "Document send failed!"
print resp.status_code
return
if resp.text != "Success":
print "An error occurred"
print resp.text
return
sendDocument(["Apple news", "Samsung news"], "TechTips.com", "joe@example.com")