![]() |
This API is designed to be as flexible as possible.
Using just one API, your software will be configured for sending various types of messages. The MessageType parameter specifies which type of message is sent (eg Email).
SendMode
parameter in your message object to skip message delivery. The message will be accepted and processed with a simulated delivery result displayed. Head to https://www.tnz.co.nz/Customer/SignUp/ to create a new account.
A test account is possible, however creating a commercial account and using the Mode=Test function of the API is simpler to use when you’re ready to push your application live.
After completing the sign up form, a sales consultant will manually activate your account. This may take one business day. Once the account has been activated, you will be able to Create a new API user token.
Using the API will require a Sender and APIKey pair to authenticate with the API.
You can generate this using the Web Dashboard under: Users | Outbound Users | Create
User Email Address = Sender
User Token = APIKey
The API is designed to be as flexible as possible. This means you can use the same API URL to send a variety of message types.
https://www.tnz.co.nz/api/json/send/
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "Email",
"APIVersion": "1.05",
"MessageID": "ID123456",
"MessageData" : %%
https://www.tnz.co.nz/api/json/send/
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "SMS",
"APIVersion": "1.05",
"MessageID": "ID123456",
"MessageData" : %%
The base parameters are: Sender, APIKey, APIVersion, MessageType, MessageData
Parameter | Example Value | Description |
---|---|---|
Sender | application@domain.com | Sender value set up in Create a new API user token |
APIKey | ta8wr7ymd | APIKey value set up in Create a new API user token |
APIVersion | 1.05 | Version of the API used; can differ based on MessageType |
MessageID (Optional) |
ID123456 | A message tracking identifier (maximum 40 characters, alphanumeric). If you do not supply this field, the API will return one for you in the response body (UUID v4 of 36 characters) |
MessageType | Available message types referenced in Adding variables based on the MessageType, eg Email, Fax, SMS, Voice, TextToSpeech | |
MessageData | %Variable% | String of values based on the MessageType referenced in Adding variables to MessageData based on MessageType |
The possible POST response values are:
200 OK, Job %JOBNUMBER%
400 BAD REQUEST, %ERROR REASON%
500 Internal Server Error
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
{
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "Email",
"APIVersion": "1.05",
"MessageData" :
{
"EmailSubject": "Test Email Subject",
"MessagePlain": "Hello, This is a test message. Thank you.",
"Destinations" :
[
"recipient.four@example.com"
]
}
}
{
"Result": "Success",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD"
}
{
"Result": "Failed",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
{
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "Email",
"APIVersion": "1.05",
"MessageID": "ID123456",
"MessageData" :
{
"Reference": "Test1",
"SendTime": "",
"TimeZone": "New Zealand",
"SubAccount": "SubAccount01",
"Department": "Department01",
"ChargeCode": "BillingGroup01",
"EmailSubject": "Test Email Subject",
"SMTPFrom": "noreply@example.com",
"From": "noreply",
"FromEmail": "noreply@example.com",
"ReplyTo": "reply@example.com",
"MessagePlain": "Hello,\r\nThis is a test message.\r\nThank you.",
"MessageHTML": "<html>Hello,<br /><br />This is a test message.<br /><br />Thank you.</html>",
"Destinations" :
[
{
"Recipient": "recipient.one@example.com",
"Attention": "Recipient One",
"Company": "Example Corp",
"Custom1": "",
"Custom2": ""
},
"recipient.two@example.com",
"recipient.three@example.com",
"recipient.four@example.com"
],
"Files":
[
{
"Name": "Sample.pdf",
"Data": "%%BASE-64 CONTENT%%"
},
{
"Name": "Sample2.pdf",
"Data": "%%BASE-64 CONTENT%%"
}
]
}
}
{
"Result": "Success",
"MessageID": "ID123456"
}
{
"Result": "Failed",
"MessageID": "ID123456",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
curl --request POST \
--url https://www.tnz.co.nz/api/json/send \
--header "Content-Type: application/json; encoding='utf-8'" \
--data '{
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "Email",
"APIVersion": "1.05",
"MessageData" :
{
"EmailSubject": "Test Email Subject",
"MessagePlain": "Hello, This is a test message. Thank you.",
"Destinations" :
[
"recipient.four@example.com"
]
}
}'
{
"Result": "Success",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD"
}
{
"Result": "Failed",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
curl --request POST \
--url https://www.tnz.co.nz/api/json/send \
--header "Content-Type: application/json; encoding='utf-8'" \
--data '{
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "Email",
"APIVersion": "1.05",
"MessageID": "ID123456",
"MessageData" :
{
"Reference": "Test1",
"SendTime": "",
"TimeZone": "New Zealand",
"SubAccount": "SubAccount01",
"Department": "Department01",
"ChargeCode": "BillingGroup01",
"EmailSubject": "Test Email Subject",
"SMTPFrom": "noreply@example.com",
"From": "noreply",
"FromEmail": "noreply@example.com",
"ReplyTo": "reply@example.com",
"MessagePlain": "Hello,\r\nThis is a test message.\r\nThank you.",
"MessageHTML": "<html>Hello,<br /><br />This is a test message.<br /><br />Thank you.</html>",
"Destinations" :
[
{
"Recipient": "recipient.one@example.com",
"Attention": "Recipient One",
"Company": "Example Corp",
"Custom1": "",
"Custom2": ""
},
"recipient.two@example.com",
"recipient.three@example.com",
"recipient.four@example.com"
],
"Files":
[
{
"Name": "Sample.pdf",
"Data": "%%BASE-64 CONTENT%%"
},
{
"Name": "Sample2.pdf",
"Data": "%%BASE-64 CONTENT%%"
}
]
}
}'
{
"Result": "Success",
"MessageID": "ID123456"
}
{
"Result": "Failed",
"MessageID": "ID123456",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
<?php
$data = array(
"Sender" => "application@domain.com", // your user id
"APIKey" => "ta8wr7ymd", // your api key
"MessageType" => "Email",
"APIVersion" => "1.05",
"MessageData" => array(
"EmailSubject" => "Test Email Subject",
"MessagePlain" => "Hello, This is a test message. Thank you.",
"Destinations" => array(
"recipient.four@example.com"
),
)
);
$headers = array(
"Content-Type: application/json",
"encoding='utf-8'",
"Content-length: ".strlen(json_encode($data)),
"Connection: close"
);
try
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.tnz.co.nz/api/json/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$err = "";
if(curl_errno($ch))
{
$err = curl_error($ch);
}
else
{
curl_close($ch);
}
switch( $info['http_code'] )
{
case "200": // success;
header('Content-Type: application/json');
echo $response;
break;
case "400": // error
header('Content-Type: application/json');
echo $response;
break;
case "500": // critical error
echo $response;
echo $err;
break;
default:
echo $response;
echo $err;
break;
}
}
catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage();
}
?>
{
"Result": "Success",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD"
}
{
"Result": "Failed",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
<?php
$sender = "application@domain.com"; // your user id
$apikey = "ta8wr7ymd"; // your api key
$type = "Email";
$version = "1.05";
$message_id = "ID123456"; //optional
$file1 = "Sample.pdf";
$file2 = "Sample2.pdf";
if( file_exsits($file1) )
{
$fp = fopen($file1,"r");
$file_content1 = fread($fp,filesize($file1));
fclose($fp);
}
if( file_exists($file2) )
{
$fp = fopen($file2,"r");
$file_content2 = fread($fp,filesize($file2));
fclose($fp);
}
$data = array(
"Sender" => $sender,
"APIKey" => $apikey,
"MessageType" => $type,
"APIVersion" => $version,
"MessageID" => $message_id,
"MessageData" => array(
"Reference" => "Test1",
"SendTime" => "",
"TimeZone" => "New Zealand",
"SubAccount" => "SubAccount01",
"Department" => "Department01",
"ChargeCode" => "BillingGroup01",
"EmailSubject" => "Test Email Subject",
"SMTPFrom" => "noreply@example.com",
"From" => "noreply",
"FromEmail" => "noreply@example.com",
"ReplyTo" => "reply@example.com",
"MessagePlain" => "Hello,\r\nThis is a test message.\r\nThank you.",
"MessageHTML" => "Hello,
This is a test message.
Thank you.",
"Destinations" => array(
array(
"Recipient" => "recipient.one@example.com",
"Attention" => "Recipient One",
"Company" => "Example Corp",
"Custom1" => "",
"Custom2" => ""
),
"recipient.two@example.com",
"recipient.three@example.com",
"recipient.four@example.com"
),
"Files" => array(
array(
"Name" => $file1,
"Data" => base64_encode($file_content1)
),
array(
"Name" => $file2,
"Data" => base64_encode($file_content2)
)
),
)
);
$headers = array(
"Content-Type: application/json",
"encoding='utf-8'",
"Content-length: ".strlen(json_encode($data)),
"Connection: close"
);
try
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.tnz.co.nz/api/json/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$err = "";
if(curl_errno($ch))
{
$err = curl_error($ch);
}
else
{
curl_close($ch);
}
switch( $info['http_code'] )
{
case "200": // success;
header('Content-Type: application/json');
echo $response;
break;
case "400": // error
header('Content-Type: application/json');
echo $response;
break;
case "500": // critical error
echo $response;
echo $err;
break;
default:
echo $response;
echo $err;
break;
}
}
catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage();
}
?>
{
"Result": "Success",
"MessageID": "ID123456"
}
{
"Result": "Failed",
"MessageID": "ID123456",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
using System;
using System.Text;
using System.IO;
using System.Net;
namespace EmailSimple
{
class Program
{
static void Main(string[] args)
{
string apiURL = "https://www.tnz.co.nz/api/json/send";
//
// Build JSON Data
//
string strJSONData = @"{
""Sender"": ""application@domain.com"",
""APIKey"": ""ta8wr7ymd"",
""MessageType"": ""Email"",
""APIVersion"": ""1.05"",
""MessageData"": {
""EmailSubject"": ""Test Email Subject"",
""MessagePlain"": ""Hello, This is a test message. Thank you."",
""Destinations"":
[
""recipient@example.com""
]
}
}";
//
// Send HTTP Request to TNZ Rest API
//
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiURL);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/json; encoding='utf-8'";
byte[] byteArray = Encoding.UTF8.GetBytes(strJSONData);
request.ContentLength = byteArray.Length;
Stream stream = request.GetRequestStream(); //open connection
stream.Write(byteArray, 0, byteArray.Length); // Send the data.
stream.Close();
string strResponse = "";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
strResponse = sr.ReadToEnd();
}
Console.WriteLine(strResponse);
}
}
}
{
"Result": "Success",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD"
}
{
"Result": "Failed",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
using System;
using System.Text;
using System.IO;
using System.Net;
namespace EmailAdvanced
{
class Program
{
private static string GetFileContents(string strFileLocation)
{
//
// Read files in bytes, convert to Base64String and return in string
//
using (FileStream reader = new FileStream(strFileLocation, FileMode.Open))
{
byte[] buffer = new byte[reader.Length];
reader.Read(buffer, 0, (int)reader.Length);
string strContent = Convert.ToBase64String(buffer);
ASCIIEncoding encoding = new ASCIIEncoding();
return Encoding.ASCII.GetString(encoding.GetBytes(strContent));
}
}
static void Main(string[] args)
{
string apiURL = "https://www.tnz.co.nz/api/json/send";
string file1 = "C:\\File1.pdf";
string file2 = "C:\\File2.pdf";
string file1_data = GetFileContents(file1);
string file2_data = GetFileContents(file2);
//
// Build JSON Data
//
string strJSONData = $@"{{
""Sender"": ""application@domain.com"",
""APIKey"": ""ta8wr7ymd"",
""MessageType"": ""Email"",
""APIVersion"": ""1.05"",
""MessageID"": ""ID123456"",
""MessageData"": {{
""Reference"": ""Test1"",
""SendTime"": """",
""TimeZone"": ""New Zealand"",
""SubAccount"": ""SubAccount01"",
""Department"": ""Department01"",
""ChargeCode"": ""BillingGroup01"",
""EmailSubject"": ""Test Email Subject"",
""SMTPFrom"": ""noreply@example.com"",
""From"": ""noreply"",
""FromEmail"": ""noreply@example.com"",
""ReplyTo"": ""reply@example.com"",
""MessagePlain"": ""Hello,\r\nThis is a test message.\r\nThank you."",
""MessageHTML"": ""<html>Hello,<br /><br />This is a test message.<br /><br />Thank you.</html>"",
""Destinations"":
[
{{
""Recipient"": ""recipient.one @example.com"",
""Attention"": ""Recipient One"",
""Company"": ""Example Corp""
}},
""recipient.two@example.com"",
""recipient.three@example.com"",
""recipient.four@example.com""
],
""Files"":
[
{{
""Name"": ""File1.pdf"",
""Data"": ""{file1_data}""
}},
{{
""Name"": ""File2.pdf"",
""Data"": ""{file2_data}""
}}
]
}}
}}";
//
// Send HTTP Request to TNZ Rest API
//
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiURL);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/json; encoding='utf-8'";
byte[] byteArray = Encoding.UTF8.GetBytes(strJSONData);
request.ContentLength = byteArray.Length;
Stream stream = request.GetRequestStream(); //open connection
stream.Write(byteArray, 0, byteArray.Length); // Send the data.
stream.Close();
string strResponse = "";
//
// More code to handle 400 Bad Request errors
//
try
{
using (var response = request.GetResponse() as HttpWebResponse)
{
if (request.HaveResponse && response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
strResponse = reader.ReadToEnd();
}
}
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (var errorResponse = (HttpWebResponse)wex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
strResponse = reader.ReadToEnd();
}
}
}
}
Console.WriteLine(strResponse);
}
}
}
{
"Result": "Success",
"MessageID": "ID123456"
}
{
"Result": "Failed",
"MessageID": "ID123456",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
import requests
import json
url = "https://www.tnz.co.nz/api/json/send/"
headers = {
"Content-Type": "application/json",
"encoding": "utf-8"
}
data = {
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "Email",
"APIVersion": "1.05",
"MessageData" :
{
"EmailSubject" : "Test Email Subject",
"MessagePlain" : "Hello, This is a test message. Thank you.",
"Destinations" :
[
"recipient.four@example.com"
]
}
}
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r)
print(r.text)
{
"Result": "Success",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD"
}
{
"Result": "Failed",
"MessageID": "AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/json/send/
HTTP Header: Content-Type="application/json; encoding='utf-8'"
import requests
import base64
import json
url = "https://www.tnz.co.nz/api/json/send/"
headers = {
"Content-Type": "application/json",
"encoding": "utf-8"
}
file_data = base64.b64encode(open("d:/File1.pdf","rb").read()).decode("utf-8")
data = {
"Sender": "application@domain.com",
"APIKey": "ta8wr7ymd",
"MessageType": "Email",
"APIVersion": "1.05",
"MessageID": "ID123456",
"MessageData" :
{
"Reference": "Test1",
"SendTime": "",
"TimeZone": "New Zealand",
"SubAccount": "SubAccount01",
"Department": "Department01",
"ChargeCode": "BillingGroup01",
"EmailSubject": "Test Email Subject",
"SMTPFrom": "noreply@example.com",
"From": "noreply",
"FromEmail": "noreply@example.com",
"ReplyTo": "reply@example.com",
"MessagePlain": "Hello,\r\nThis is a test message.\r\nThank you.",
"MessageHTML": "Hello,
This is a test message.
Thank you.",
"Destinations" :
[
{
"Recipient": "recipient.one@example.com",
"Attention": "Recipient One",
"Company": "Example Corp",
"Custom1": "",
"Custom2": ""
},
"recipient.two@example.com",
"recipient.three@example.com",
"recipient.four@example.com"
],
"Files" :
[
{
"Name": "File1.pdf",
"Data": file_data
}
]
}
}
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r)
print(r.text)
{
"Result": "Success",
"MessageID": "ID123456"
}
{
"Result": "Failed",
"MessageID": "ID123456",
"Message": "Missing Sender"
}
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Sender>application@domain.com</Sender>
<APIKey>ta8wr7ymd</APIKey>
<MessageType>Email</MessageType>
<APIVersion>1.05</APIVersion>
<MessageData>
<EmailSubject>Test Email Subject</EmailSubject>
<MessagePlain>Hello, This is a test message. Thank you.</MessagePlain>
<Destinations>recipient.one@example.com</Destinations>
</MessageData>
</root>
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Sender>application@domain.com</Sender>
<APIKey>ta8wr7ymd</APIKey>
<MessageType>Email</MessageType>
<APIVersion>1.05</APIVersion>
<MessageID>ID123456</MessageID>
<MessageData>
<Reference>Test1</Reference>
<SendTime></SendTime>
<TimeZone>New Zealand</TimeZone>
<SubAccount>SubAccount01</SubAccount>
<Department>Department01</Department>
<ChargeCode>BillingGroup01</ChargeCode>
<EmailSubject>Test Email Subject</EmailSubject>
<SMTPFrom>noreply@example.com</SMTPFrom>
<From>noreply</From>
<FromEmail>noreply</FromEmail>
<ReplyTo>reply@example.com</ReplyTo>
<MessagePlain>Hello,\r\nThis is a test message.\r\nThank you.</MessagePlain>
<MessageHTML><![CDATA[<html>Hello,<br /><br />This is a test message.<br /><br />Thank you.</html>]]></MessageHTML>
<Destinations>
<Recipient>recipient.one@example.com</Recipient>
<Attention>Recipient One</Attention>
<Company>Example Corp</Company>
<Custom1></Custom1>
<Custom2></Custom2>
</Destinations>
<Destinations>recipient.two@example.com</Destinations>
<Destinations>recipient.three@example.com</Destinations>
<Destinations>recipient.four@example.com</Destinations>
<Files>
<Name>Sample.pdf</Name>
<Data>%%BASE-64 CONTENT%%</Data>
</Files>
<Files>
<Name>Sample2.pdf</Name>
<Data>%%BASE-64 CONTENT%%</Data>
</Files>
</MessageData>
</root>
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>ID123456</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>ID123456</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
curl --request POST \
--url https://www.tnz.co.nz/api/xml/send \
--header "Content-Type: text/xml; encoding='utf-8'" \
--data '
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Sender>application@domain.com</Sender>
<APIKey>ta8wr7ymd</APIKey>
<MessageType>Email</MessageType>
<APIVersion>1.05</APIVersion>
<MessageData>
<EmailSubject>Test Email Subject</EmailSubject>
<MessagePlain>Hello, This is a test message. Thank you.</MessagePlain>
<Destinations>recipient@example.com</Destinations>
</MessageData>
</root>
'
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
curl --request POST \
--url https://www.tnz.co.nz/api/xml/send \
--header "Content-Type: text/xml; encoding='utf-8'" \
--data '
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Sender>application@domain.com</Sender>
<APIKey>ta8wr7ymd</APIKey>
<MessageType>Email</MessageType>
<APIVersion>1.05</APIVersion>
<MessageID>ID123456</MessageID>
<MessageData>
<Reference>Test1</Reference>
<SendTime></SendTime>
<TimeZone>New Zealand</TimeZone>
<SubAccount>SubAccount01</SubAccount>
<Department>Department01</Department>
<ChargeCode>BillingGroup01</ChargeCode>
<EmailSubject>Test Email Subject</EmailSubject>
<SMTPFrom>noreply@domain.com</SMTPFrom>
<From>noreply</From>
<FromEmail>noreply@domain.com</FromEmail>
<ReplyTo>reply@domain.com</ReplyTo>
<MessagePlain>Hello,\r\nThis is a test message.\r\nThank you.</MessagePlain>
<MessageHTML><![CDATA[<html>Hello,<br /><br />This is a test message.<br /><br />Thank you.</html>]]></MessageHTML>
<Destinations>
<Recipient>recipient.one@example.com</Recipient>
<Attention>Recipient One</Attention>
<Company>Example Corp</Company>
<Custom1></Custom1>
<Custom2></Custom2>
</Destinations>
<Destinations>recipient.two@example.com</Destinations>
<Destinations>recipient.three@example.com</Destinations>
<Destinations>recipient.four@example.com</Destinations>
<Files>
<Name>Sample.pdf</Name>
<Data>%%BASE-64 CONTENT%%</Data>
</Files>
<Files>
<Name>Sample2.pdf</Name>
<Data>%%BASE-64 CONTENT%%</Data>
</Files>
</MessageData>
</root>
'
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>ID123456</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>ID123456</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
<?php
$data = new SimpleXMLExtended("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
$data->addChild("Sender","application@domain.com");
$data->addChild("APIKey","ta8wr7ymd");
$data->addChild("MessageType","1.05");
$data->addChild("APIVersion","Email");
$data->addChild("MessageData");
$data->MessageData->addChild("EmailSubject","Test Email Subject");
$data->MessageData->addChild("MessaePlain","Hello, This is a test message. Thank you.");
$data->MessageData->addChild("Destinations","recipient@example.com");
$headers = array(
"Content-Type: text/xml",
"encoding='utf-8'",
"Content-length: ".strlen($data->asXML()),
"Connection: close"
);
try
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.tnz.co.nz/api/xml/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data->asXML());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$err = "";
if(curl_errno($ch))
{
$err = curl_error($ch);
}
else
{
curl_close($ch);
}
switch( $info['http_code'] )
{
case "200": // success;
header("Content-Type: text/xml; encoding='utf-8'");
echo $response;
break;
case "400": // error
header("Content-Type: text/xml; encoding='utf-8'");
echo $response;
break;
case "500": // critical error
echo $response."\n";
echo $err;
break;
default:
echo $response."\n";
echo $err;
break;
}
}
catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage();
}
?>
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
<?php
class SimpleXMLExtended extends SimpleXMLElement {
//
// this class extends SimplXMLElement to insert CDATA if necessary.
// for example, XML is not validated if HTML tag is inside XML Element,
// so we want to wrap inside CDATA, <![CDATA[<html>...</html>]]>
//
public function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
$sender = "application@domain.com";
$apikey = "ta8wr7ymd";
$type = "Email";
$version = "1.05";
$message_id = "ID123456"; //optional
$file_name1 = "Sample.pdf";
$file_name2 = "Sample2.pdf";
if( file_exists($file_name1) )
{
$fp = fopen($file_name1,"r");
$file_content1 = fread($fp,filesize($file_name1));
fclose($fp);
}
if( file_exists($file_name2) )
{
$fp = fopen($file_name2,"r");
$file_content2 = fread($fp,filesize($file_name2));
fclose($fp);
}
$data = new SimpleXMLExtended("<?xml version=\"1.0\" encoding=\"utf-8\"?><root></root>");
$data->addChild("Sender",$sender);
$data->addChild("APIKey",$apikey);
$data->addChild("MessageType",$type);
$data->addChild("APIVersion",$version);
$data->addChild("MessageID",$message_id);
$data->addChild("MessageData");
$data->MessageData->addChild("Reference","Test1");
$data->MessageData->addChild("SendTime");
$data->MessageData->addChild("TimeZone","New Zealand");
$data->MessageData->addChild("SubAccount","SubAccount01");
$data->MessageData->addChild("Department","Department01");
$data->MessageData->addChild("ChargeCode","BillingGroup01");
$data->MessageData->addChild("EmailSubject","Test Email Subject");
$data->MessageData->addChild("SMTPFrom","noreply@example.com");
$data->MessageData->addChild("From","noreply");
$data->MessageData->addChild("FromEmail","noreply@example.com");
$data->MessageData->addChild("ReplyTo","reply@example.com");
$data->MessageData->addChild("MessaePlain","Hello,\r\nThis is a test message.\r\nThank you.");
$data->MessageData->addChild("MessageHTML");
$data->MessageData->MessageHTML->addCData("<html>Hello,<br /><br />This is a test message.<br /><br />Thank you.</html>"); // wrap CDATA for HTML Tags
$destination1 = $data->MessageData->addChild("Destinations");
$destination1->addChild("Recipient","recipient.one@example.com");
$destination1->addChild("Attention","Recipient One");
$destination1->addChild("Company","Example Corp");
$destination1->addChild("Custom1","");
$destination1->addChild("Custom2","");
$destination2 = $data->MessageData->addChild("Destinations","recipient.two@example.com");
$destination3 = $data->MessageData->addChild("Destinations","recipient.three@example.com");
$destination4 = $data->MessageData->addChild("Destinations","recipient.four@example.com");
if( file_exists($file_name1) )
{
$fileElement1 = $data->MessageData->addChild("Files");
$fileElement1->addChild("Name",basename($file_name1));
$fileElement1->addChild("Data",base64_encode($file_content1));
}
if( file_exists($file_name2) )
{
$fileElement2 = $data->MessageData->addChild("Files");
$fileElement2->addChild("Name",basename($file_name2));
$fileElement2->addChild("Data",base64_encode($file_content2));
}
$headers = array(
"Content-Type: text/xml",
"encoding='utf-8'",
"Content-length: ".strlen($data->asXML()),
"Connection: close"
);
try
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.tnz.co.nz/api/xml/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data->asXML());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$err = "";
if(curl_errno($ch))
{
$err = curl_error($ch);
}
else
{
curl_close($ch);
}
switch( $info['http_code'] )
{
case "200": // success;
header("Content-Type: text/xml; encoding='utf-8'");
echo $response;
break;
case "400": // error
header("Content-Type: text/xml; encoding='utf-8'");
echo $response;
break;
case "500": // critical error
echo $response."\n";
echo $err;
break;
default:
echo $response."\n";
echo $err;
break;
}
}
catch(Exception $e)
{
echo 'Caught exception: ', $e->getMessage();
}
?>
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>ID123456</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>ID123456</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Xml;
namespace EmailSimple
{
class Program
{
private static XmlNode addChildNode(XmlDocument xmlDoc, string name, string val)
{
XmlNode workingNode = xmlDoc.CreateElement(name);
workingNode.InnerText = val;
return workingNode;
}
static void Main(string[] args)
{
string apiURL = "https://www.tnz.co.nz/api/xml/send";
XmlDocument xmlDoc = new XmlDocument();
XmlNode docNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); // <?xml version="1.0" encoding="UTF-8"?>
xmlDoc.AppendChild(docNode);
XmlNode rootNode = xmlDoc.CreateElement("root");
xmlDoc.AppendChild(rootNode);
rootNode.AppendChild(addChildNode(xmlDoc, "Sender", "application@domain.com"));
rootNode.AppendChild(addChildNode(xmlDoc, "APIKey", "ta8wr7ymd"));
rootNode.AppendChild(addChildNode(xmlDoc, "MessageType", "Email"));
rootNode.AppendChild(addChildNode(xmlDoc, "APIVersion", "1.05"));
XmlNode workingNode = xmlDoc.CreateElement("Sender");
//
// MessageData
//
XmlNode messageDataNode = xmlDoc.CreateElement("MessageData");
messageDataNode.AppendChild(addChildNode(xmlDoc, "EmailSubject", "Test Email Subject"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "MessagePlain", "Hello,\r\nThis is a test message.\r\nThank you."));
//
// Add Destination
//
messageDataNode.AppendChild(addChildNode(xmlDoc, "Destinations", "recipient@example.com"));
//
// Set MessageData into root node
//
rootNode.AppendChild(messageDataNode);
//
// Send HTTP Request to REST API
//
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiURL);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "text/xml; encoding='utf-8'";
byte[] byteArray = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
request.ContentLength = byteArray.Length;
Stream stream = request.GetRequestStream(); //open connection
stream.Write(byteArray, 0, byteArray.Length); // Send the data.
stream.Close();
string strResponse = "";
//
// More code to handle 400 Bad Request errors
//
try
{
using (var response = request.GetResponse() as HttpWebResponse)
{
if (request.HaveResponse && response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
strResponse = reader.ReadToEnd();
}
}
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (var errorResponse = (HttpWebResponse)wex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
strResponse = reader.ReadToEnd();
}
}
}
}
Console.WriteLine(strResponse);
}
}
}
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Xml;
namespace EmailAdvanced
{
class Program
{
private static string GetFileContents(string strFileLocation)
{
//
// Read files in bytes, convert to Base64String and return in string
//
using (FileStream reader = new FileStream(strFileLocation, FileMode.Open))
{
byte[] buffer = new byte[reader.Length];
reader.Read(buffer, 0, (int)reader.Length);
string strContent = Convert.ToBase64String(buffer);
ASCIIEncoding encoding = new ASCIIEncoding();
return Encoding.ASCII.GetString(encoding.GetBytes(strContent));
}
}
private static XmlNode addChildNode(XmlDocument xmlDoc, string name, string val)
{
XmlNode workingNode = xmlDoc.CreateElement(name);
workingNode.InnerText = val;
return workingNode;
}
private static XmlNode addChildNodeCDATA(XmlDocument xmlDoc, string name, string val)
{
XmlNode workingNode = xmlDoc.CreateElement(name);
workingNode.InnerXml = @"<![CDATA[" + val + "]]>";
return workingNode;
}
static void Main(string[] args)
{
string apiURL = "https://www.tnz.co.nz/api/xml/send";
string file1 = "C:\\File1.pdf";
string file2 = "C:\\File2.pdf";
string file1_data = GetFileContents(file1);
string file2_data = GetFileContents(file2);
XmlDocument xmlDoc = new XmlDocument();
XmlNode docNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); // <?xml version="1.0" encoding="UTF-8"?>
xmlDoc.AppendChild(docNode);
XmlNode rootNode = xmlDoc.CreateElement("root");
xmlDoc.AppendChild(rootNode);
rootNode.AppendChild(addChildNode(xmlDoc, "Sender", "application@domain.com"));
rootNode.AppendChild(addChildNode(xmlDoc, "APIKey", "ta8wr7ymd"));
rootNode.AppendChild(addChildNode(xmlDoc, "MessageType", "Email"));
rootNode.AppendChild(addChildNode(xmlDoc, "APIVersion", "1.05"));
rootNode.AppendChild(addChildNode(xmlDoc, "MessageID", "ID123456"));
XmlNode workingNode = xmlDoc.CreateElement("Sender");
//
// MessageData
//
XmlNode messageDataNode = xmlDoc.CreateElement("MessageData");
messageDataNode.AppendChild(addChildNode(xmlDoc, "Reference", "Test1"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "SendTime", ""));
messageDataNode.AppendChild(addChildNode(xmlDoc, "TimeZone", "New Zealand"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "SubAccount", "SubAccount01"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "Department", "Department01"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "ChargeCode", "BillingGroup01"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "EmailSubject", "Test Email Subject"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "SMTPFrom", "noreply@example.com"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "From", "noreply"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "FromEmail", "noreply@example.com"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "ReplyTo", "reply@example.com"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "MessagePlain", "Hello,\r\nThis is a test message.\r\nThank you."));
messageDataNode.AppendChild(addChildNodeCDATA(xmlDoc, "MessageHTML", "<html>Hello,<br /><br />This is a test message.<br /><br />Thank you.</html>")); // wrap with <![CDATA[]]>
//
// Add Destinations 1 - Add with details
//
workingNode = xmlDoc.CreateElement("Destinations");
workingNode.AppendChild(addChildNode(xmlDoc, "Recipient", "recipient.one@example.com"));
workingNode.AppendChild(addChildNode(xmlDoc, "Attention", "Recipient One"));
workingNode.AppendChild(addChildNode(xmlDoc, "Company", "Example Corp"));
workingNode.AppendChild(addChildNode(xmlDoc, "Custom1", "Custom1"));
workingNode.AppendChild(addChildNode(xmlDoc, "Custom2", "Custom2"));
messageDataNode.AppendChild(workingNode);
//
// Add Destinations 2 - Add numbers only
//
messageDataNode.AppendChild(addChildNode(xmlDoc, "Destinations", "recipient.two@example.com"));
messageDataNode.AppendChild(addChildNode(xmlDoc, "Destinations", "recipient.three@example.com"));
//
// Attach File 1
//
workingNode = xmlDoc.CreateElement("Files");
workingNode.AppendChild(addChildNode(xmlDoc, "Name", "File1.pdf"));
workingNode.AppendChild(addChildNode(xmlDoc, "Data", file1_data));
messageDataNode.AppendChild(workingNode);
//
// Attach File 2
//
workingNode = xmlDoc.CreateElement("Files");
workingNode.AppendChild(addChildNode(xmlDoc, "Name", "File2.pdf"));
workingNode.AppendChild(addChildNode(xmlDoc, "Data", file2_data));
messageDataNode.AppendChild(workingNode);
//
// Set MessageData into root node
//
rootNode.AppendChild(messageDataNode);
//
// Send HTTP Request to REST API
//
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiURL);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "text/xml; encoding='utf-8'";
byte[] byteArray = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);
request.ContentLength = byteArray.Length;
Stream stream = request.GetRequestStream(); //open connection
stream.Write(byteArray, 0, byteArray.Length); // Send the data.
stream.Close();
string strResponse = "";
//
// More code to handle 400 Bad Request errors
//
try
{
using (var response = request.GetResponse() as HttpWebResponse)
{
if (request.HaveResponse && response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
strResponse = reader.ReadToEnd();
}
}
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (var errorResponse = (HttpWebResponse)wex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
strResponse = reader.ReadToEnd();
}
}
}
}
Console.WriteLine(strResponse);
}
}
}
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>ID123456</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>ID123456</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
import requests
import xml.etree.ElementTree as ET
url = "https://www.tnz.co.nz/api/xml/send/"
headers = {
"Content-Type": "text/xml",
"encoding": "utf-8"
}
root = ET.Element('root')
ET.SubElement(root, "Sender").text = "application@domain.com"
ET.SubElement(root, "APIKey").text = "ta8wr7ymd"
ET.SubElement(root, "MessageType").text = "Email"
ET.SubElement(root, "APIVersion").text = "1.05"
message_data = ET.SubElement(root,"MessageData")
ET.SubElement(message_data, "EmailSubject").text = "Test Email Subject"
ET.SubElement(message_data, "MessagePlain").text = "Hello, This is a test message. Thank you."
ET.SubElement(message_data, "Destinations").text = "recipient.four@example.com"
r = requests.post(url, data=ET.tostring(root,'utf-8'), headers=headers)
print(r)
print(r.text)
<?xml version="1.0"?>
<root>
<Result>Success</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
</root>
<?xml version="1.0"?>
<root>
<Result>Failed</Result>
<MessageID>AAAAAAAA-BBBB-BBBB-CCCC-DDDDDDDDDDDD</MessageID>
<Message>Missing or empty sender</Message>
</root>
API URL: https://www.tnz.co.nz/api/xml/send/
HTTP Header: Content-Type="text/xml; encoding='utf-8'"
import requests
import base64
import xml.etree.ElementTree as ET
url = "https://www.tnz.co.nz/api/xml/send/"
headers = {
"Content-Type": "text/xml",
"encoding": "utf-8"
}
file_data_1 = base64.b64encode(open("d:/File1.pdf","rb").read()).decode("utf-8")
file_data_2 = base64.b64encode(open("d:/File2.pdf","rb").read()).decode("utf-8")
root = ET.Element('root')
ET.SubElement(root, "Sender").text = "application@domain.com"
ET.SubElement(root, "APIKey").text = "ta8wr7ymd"
ET.SubElement(root, "MessageType").text = "Email"
ET.SubElement(root, "APIVersion").text = "1.05"
ET.SubElement(root, "MessageID").text = "ID123456"
message_data = ET.SubElement(root,"MessageData")
ET.SubElement(message_data, "Reference").text = "Test1"
ET.SubElement(message_data, "SendTime").text = ""
ET.SubElement(message_data, "TimeZone").text = "New Zealand"
ET.SubElement(message_data, "SubAccount").text = "SubAccount01"
ET.SubElement(message_data, "Department").text = "Department01"
ET.SubElement(message_data, "ChargeCode").text = "BillingGroup01"
ET.SubElement(message_data, "EmailSubject").text = "Test Email Subject"
ET.SubElement(message_data, "SMTPFrom").text = "noreply@example.com"
ET.SubElement(message_data, "From").text = "noreply"
ET.SubElement(message_data, "FromEmail").text = "noreply@example.com"
ET.SubElement(message_data, "ReplyTo").text = "reply@example.com"
ET.SubElement(message_data, "MessagePlain").text = "Hello,\r\nThis is a test message.\r\nThank you."
ET.SubElement(message_data, "MessageHTML").text = "Hello,
This is a test message.
Thank you.