const { EmailApi, EmailMessage } = require('sendpost_js');
const email = new EmailApi();
const apiKey = 'your_api_key'; // {String} Sub-Account API Key
(async function() {
const message = new EmailMessage();
message.from = { email: 'richard@piedpiper.com' };
message.to = [{
email: 'gavin@hooli.com',
cc: [{ email: 'dinesh@bachmanity.com' }],
bcc: [{ email: 'jian@bachmanity.com' }],
}]
message.subject = 'Hello'
message.htmlBody = '<strong>it works!</strong>';
message.ippool = 'PiedPiper'
message.template = 'Welcome Mail'
const opts = {
emailMessage: message
};
try {
const data = await email.sendEmailWithTemplate(apiKey, opts)
console.log('API called successfully. Returned data: ', data);
} catch (error) {
console.error(error);
}
})()
require_once(__DIR__ . '/vendor/autoload.php');
$client = new GuzzleHttp\Client();
$apiInstance = new sendpost\api\EmailApi($client);
$x_sub_account_api_key = 'your_api_key'; // string | Sub-Account API Key
$email_message = new \sendpost\model\EmailMessage();
$email_message->setSubject('Hello World');
$email_message->setHtmlBody('<strong>it works!</strong>');
$email_message->setIppool('PiedPiper');
$from = new \sendpost\model\From();
$from->setEmail('richard@piedpiper.com');
$cc = new \sendpost\model\CopyTo();
$cc->setEmail('dinesh@bachmanity.com');
$to->setCc(array($cc));
$bcc = new \sendpost\model\CopyTo();
$bcc->setEmail('jian@bachmanity.com');
$to->setBcc(array($bcc));
$email_message->setTemplate('Welcome Mail');
$to = new \sendpost\model\To();
$to->setEmail('gavin@hooli.com');
$email_message->setTo(array($to));
$email_message->setFrom($from);
try {
$result = $apiInstance->sendEmailWithTemplate($x_sub_account_api_key, $email_message);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EmailApi->sendEmail: ', $e->getMessage(), PHP_EOL;
}
import sendpost_python_sdk
from pprint import pprint
from sendpost_python_sdk.apis.tags import email_api
# Enter a context with an instance of the API client
with sendpost_python_sdk.ApiClient() as api_client:
# Create an instance of the API class
api_instance = email_api.EmailApi(api_client)
x_sub_account_api_key = "your_api_key" # str | Sub-Account API Key
email = {
"from": {
"email": "richard@piedpiper.com",
},
"to": [
{
"email": "gavin@hooli.com",
"cc": [{ "email": "dinesh@bachmanity.com" }],
"bcc": [{ "email": "jian@bachmanity.com" }],
}
],
"template": "Welcome Mail",
"subject": "Hello World",
"htmlBody": "<strong>it works!</strong>",
"ippool": "PiedPiper",
}
try:
api_response = api_instance.send_email_with_template(header_params={ 'X-SubAccount-ApiKey': x_sub_account_api_key}, body=email)
pprint(api_response)
except sendpost_python_sdk.ApiException as e:
print("Exception when calling EmailApi->send_email: %s\n" % e)
cfg := sendpost.NewConfiguration()
client := sendpost.NewAPIClient(cfg)
emailMessage := sendpost.EmailMessage{}
emailMessage.SetSubject("Hello World")
emailMessage.SetHtmlBody("<strong>it works!</strong>")
emailMessage.SetIppool("PiedPiper")
emailMessage.From = &sendpost.From{}
emailMessage.From.SetEmail("richard@piedpiper.com")
emailMessage.SetTemplate("Welcome Mail")
tos := make([]sendpost.To, 0)
to := &sendpost.To{}
to.SetEmail("gavin@hooli.com")
ccs := make([]sendpost.CopyTo, 0)
cc := &sendpost.CopyTo{}
cc.SetEmail("dinesh@bachmanity.com")
ccs = append(ccs, *cc)
to.SetCc(ccs)
bccs := make([]sendpost.CopyTo, 0)
bcc := &sendpost.CopyTo{}
bcc.SetEmail("jian@bachmanity.com")
bccs = append(bccs, *bcc)
to.SetBcc(bccs)
tos = append(tos, *to)
emailMessage.To = tos
emailRequest := sendpost.ApiSendEmailWithTemplateRequest{}
emailRequest = emailRequest.XSubAccountApiKey("your_api_key")
emailRequest = emailRequest.EmailMessage(emailMessage)
res, _, err := client.EmailApi.SendEmailWithTemplateExecute(emailRequest)
require 'sendpost_ruby_sdk'
api_instance = Sendpost::EmailApi.new
x_sub_account_api_key = 'your_api_key' # String | Sub-Account API Key
email_message = Sendpost::EmailMessage.new
email_message.from = {
email: 'richard@piedpiper.com'
}
email_message.to = [{
email: 'gavin@hooli.com',
cc: [{ email: 'dinesh@bachmanity.com' }],
bcc: [{ email: 'jian@bachmanity.com' }]
}]
email_message.subject = 'Hello World'
email_message.template = 'Welcome Mail'
email_message.html_body = '<strong>it works!</strong>'
email_message.ippool = 'PiedPiper'
opts = {
email_message: email_message # EmailMessage | Email message
}
begin
result = api_instance.send_email_with_template(x_sub_account_api_key, opts)
p result
rescue Sendpost::ApiError => e
puts "Exception when calling EmailApi->send_email: #{e}"
end
curl -X POST "https://api.sendpost.io/api/v1/subaccount/email/template" \
-H "accept: application/json" \
-H "X-SubAccount-ApiKey: your_api_key" \
-d '{
"from": { "email": "richard@piedpiper.com" },
"to": [{ "email": "gavin@hooli.com" }],
"templateId": "12345",
"dynamicData": {
"name": "Gavin",
"product": "Compression Algorithm"
},
"ippool": "PiedPiper"
}'
HttpResponse<String> response = Unirest.post("https://api.sendpost.io/api/v1/subaccount/email/template")
.header("X-SubAccount-ApiKey", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"from\": {\n \"email\": \"notifications@yourcompany.com\",\n \"name\": \"Your Company\"\n },\n \"replyTo\": {\n \"email\": \"support@yourcompany.com\",\n \"name\": \"Support Team\"\n },\n \"to\": [\n {\n \"email\": \"customer@example.com\",\n \"name\": \"John Doe\",\n \"customFields\": {\n \"firstName\": \"John\",\n \"orderId\": \"ORD-12345\",\n \"orderTotal\": \"$99.99\"\n }\n }\n ],\n \"subject\": \"Your order {{orderId}} has been shipped!\",\n \"htmlBody\": \"<h1>Hi {{firstName}}</h1><p>Your order {{orderId}} worth {{orderTotal}} is on its way!</p>\",\n \"textBody\": \"Hi {{firstName}}, Your order {{orderId}} worth {{orderTotal}} is on its way!\",\n \"ippool\": \"transactional\",\n \"trackOpens\": true,\n \"trackClicks\": true,\n \"groups\": [\n \"order-shipped\",\n \"transactional\"\n ]\n}")
.asString();[
{
"to": "customer@example.com",
"submittedAt": 1704067200000000000,
"messageId": "550e8400-e29b-41d4-a716-446655440000",
"errorCode": 0,
"message": "Email submitted successfully"
}
]Send Email With Template
Send emails using pre-configured templates stored in SendPost. Templates separate email content from code, enabling:
Benefits:
- Update email content without code deployments
- Marketing teams can manage templates independently
- Consistent branding across all applications
- A/B test different template versions
How It Works:
- Create templates in the SendPost dashboard with Handlebars variables
- Reference the template by name or ID in this API call
- Pass dynamic data through recipient
customFields - SendPost merges data with template and sends
Common Use Cases:
| Template Type | Dynamic Data |
|---|---|
| Welcome Email | firstName, accountType, loginUrl |
| Order Receipt | orderNumber, items, total, shippingAddress |
| Password Reset | resetLink, expiryTime, userName |
| Weekly Digest | articleList, unreadCount, userName |
Example Request:
{
"from": { "email": "orders@yourstore.com" },
"to": [{
"email": "customer@example.com",
"customFields": {
"firstName": "Sarah",
"orderNumber": "ORD-2024-001",
"totalAmount": "$149.99"
}
}],
"template": "order-confirmation-v2"
}
Note: Template variables not provided in customFields will render as empty strings.
const { EmailApi, EmailMessage } = require('sendpost_js');
const email = new EmailApi();
const apiKey = 'your_api_key'; // {String} Sub-Account API Key
(async function() {
const message = new EmailMessage();
message.from = { email: 'richard@piedpiper.com' };
message.to = [{
email: 'gavin@hooli.com',
cc: [{ email: 'dinesh@bachmanity.com' }],
bcc: [{ email: 'jian@bachmanity.com' }],
}]
message.subject = 'Hello'
message.htmlBody = '<strong>it works!</strong>';
message.ippool = 'PiedPiper'
message.template = 'Welcome Mail'
const opts = {
emailMessage: message
};
try {
const data = await email.sendEmailWithTemplate(apiKey, opts)
console.log('API called successfully. Returned data: ', data);
} catch (error) {
console.error(error);
}
})()
require_once(__DIR__ . '/vendor/autoload.php');
$client = new GuzzleHttp\Client();
$apiInstance = new sendpost\api\EmailApi($client);
$x_sub_account_api_key = 'your_api_key'; // string | Sub-Account API Key
$email_message = new \sendpost\model\EmailMessage();
$email_message->setSubject('Hello World');
$email_message->setHtmlBody('<strong>it works!</strong>');
$email_message->setIppool('PiedPiper');
$from = new \sendpost\model\From();
$from->setEmail('richard@piedpiper.com');
$cc = new \sendpost\model\CopyTo();
$cc->setEmail('dinesh@bachmanity.com');
$to->setCc(array($cc));
$bcc = new \sendpost\model\CopyTo();
$bcc->setEmail('jian@bachmanity.com');
$to->setBcc(array($bcc));
$email_message->setTemplate('Welcome Mail');
$to = new \sendpost\model\To();
$to->setEmail('gavin@hooli.com');
$email_message->setTo(array($to));
$email_message->setFrom($from);
try {
$result = $apiInstance->sendEmailWithTemplate($x_sub_account_api_key, $email_message);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EmailApi->sendEmail: ', $e->getMessage(), PHP_EOL;
}
import sendpost_python_sdk
from pprint import pprint
from sendpost_python_sdk.apis.tags import email_api
# Enter a context with an instance of the API client
with sendpost_python_sdk.ApiClient() as api_client:
# Create an instance of the API class
api_instance = email_api.EmailApi(api_client)
x_sub_account_api_key = "your_api_key" # str | Sub-Account API Key
email = {
"from": {
"email": "richard@piedpiper.com",
},
"to": [
{
"email": "gavin@hooli.com",
"cc": [{ "email": "dinesh@bachmanity.com" }],
"bcc": [{ "email": "jian@bachmanity.com" }],
}
],
"template": "Welcome Mail",
"subject": "Hello World",
"htmlBody": "<strong>it works!</strong>",
"ippool": "PiedPiper",
}
try:
api_response = api_instance.send_email_with_template(header_params={ 'X-SubAccount-ApiKey': x_sub_account_api_key}, body=email)
pprint(api_response)
except sendpost_python_sdk.ApiException as e:
print("Exception when calling EmailApi->send_email: %s\n" % e)
cfg := sendpost.NewConfiguration()
client := sendpost.NewAPIClient(cfg)
emailMessage := sendpost.EmailMessage{}
emailMessage.SetSubject("Hello World")
emailMessage.SetHtmlBody("<strong>it works!</strong>")
emailMessage.SetIppool("PiedPiper")
emailMessage.From = &sendpost.From{}
emailMessage.From.SetEmail("richard@piedpiper.com")
emailMessage.SetTemplate("Welcome Mail")
tos := make([]sendpost.To, 0)
to := &sendpost.To{}
to.SetEmail("gavin@hooli.com")
ccs := make([]sendpost.CopyTo, 0)
cc := &sendpost.CopyTo{}
cc.SetEmail("dinesh@bachmanity.com")
ccs = append(ccs, *cc)
to.SetCc(ccs)
bccs := make([]sendpost.CopyTo, 0)
bcc := &sendpost.CopyTo{}
bcc.SetEmail("jian@bachmanity.com")
bccs = append(bccs, *bcc)
to.SetBcc(bccs)
tos = append(tos, *to)
emailMessage.To = tos
emailRequest := sendpost.ApiSendEmailWithTemplateRequest{}
emailRequest = emailRequest.XSubAccountApiKey("your_api_key")
emailRequest = emailRequest.EmailMessage(emailMessage)
res, _, err := client.EmailApi.SendEmailWithTemplateExecute(emailRequest)
require 'sendpost_ruby_sdk'
api_instance = Sendpost::EmailApi.new
x_sub_account_api_key = 'your_api_key' # String | Sub-Account API Key
email_message = Sendpost::EmailMessage.new
email_message.from = {
email: 'richard@piedpiper.com'
}
email_message.to = [{
email: 'gavin@hooli.com',
cc: [{ email: 'dinesh@bachmanity.com' }],
bcc: [{ email: 'jian@bachmanity.com' }]
}]
email_message.subject = 'Hello World'
email_message.template = 'Welcome Mail'
email_message.html_body = '<strong>it works!</strong>'
email_message.ippool = 'PiedPiper'
opts = {
email_message: email_message # EmailMessage | Email message
}
begin
result = api_instance.send_email_with_template(x_sub_account_api_key, opts)
p result
rescue Sendpost::ApiError => e
puts "Exception when calling EmailApi->send_email: #{e}"
end
curl -X POST "https://api.sendpost.io/api/v1/subaccount/email/template" \
-H "accept: application/json" \
-H "X-SubAccount-ApiKey: your_api_key" \
-d '{
"from": { "email": "richard@piedpiper.com" },
"to": [{ "email": "gavin@hooli.com" }],
"templateId": "12345",
"dynamicData": {
"name": "Gavin",
"product": "Compression Algorithm"
},
"ippool": "PiedPiper"
}'
HttpResponse<String> response = Unirest.post("https://api.sendpost.io/api/v1/subaccount/email/template")
.header("X-SubAccount-ApiKey", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"from\": {\n \"email\": \"notifications@yourcompany.com\",\n \"name\": \"Your Company\"\n },\n \"replyTo\": {\n \"email\": \"support@yourcompany.com\",\n \"name\": \"Support Team\"\n },\n \"to\": [\n {\n \"email\": \"customer@example.com\",\n \"name\": \"John Doe\",\n \"customFields\": {\n \"firstName\": \"John\",\n \"orderId\": \"ORD-12345\",\n \"orderTotal\": \"$99.99\"\n }\n }\n ],\n \"subject\": \"Your order {{orderId}} has been shipped!\",\n \"htmlBody\": \"<h1>Hi {{firstName}}</h1><p>Your order {{orderId}} worth {{orderTotal}} is on its way!</p>\",\n \"textBody\": \"Hi {{firstName}}, Your order {{orderId}} worth {{orderTotal}} is on its way!\",\n \"ippool\": \"transactional\",\n \"trackOpens\": true,\n \"trackClicks\": true,\n \"groups\": [\n \"order-shipped\",\n \"transactional\"\n ]\n}")
.asString();[
{
"to": "customer@example.com",
"submittedAt": 1704067200000000000,
"messageId": "550e8400-e29b-41d4-a716-446655440000",
"errorCode": 0,
"message": "Email submitted successfully"
}
]Authorizations
This api key can be used only for sub account level operations
Body
Email message details with template information
Email message object containing all the details required to send an email.
At minimum, you need to provide from, to, and either subject with htmlBody/textBody or a template.
The sender's email address and optional display name
Show child attributes
Show child attributes
{ "email": "sender@example.com", "name": "John Doe" }
List of recipients. Each recipient can have their own CC, BCC, and custom fields for personalization. Maximum 1000 recipients per API call.
1 - 1000 elementsShow child attributes
Show child attributes
The reply-to email address. If not specified, replies will go to the from address
Show child attributes
Show child attributes
{ "email": "sender@example.com", "name": "John Doe" }
Email subject line. Supports Handlebars templating for personalization. Example: "Hello, {{firstName}}! Your order is ready"
998"Welcome to SendPost, {{firstName}}!"
Preview text (preheader) shown in email clients before opening the email. This text appears after the subject line in most email clients' inbox view.
200"Your weekly digest is here with 5 new updates..."
HTML content of the email. Supports Handlebars templating for personalization. Use {{customFieldName}} to insert recipient-specific values.
"<html><body><h1>Hello {{firstName}}</h1><p>Welcome to our platform!</p></body></html>"
Plain text content of the email. Used as fallback when HTML cannot be rendered. Also improves deliverability as some spam filters prefer multipart emails.
"Hello {{firstName}},\n\nWelcome to our platform!\n\nBest regards,\nThe Team"
AMP HTML content for supported email clients (Gmail, Yahoo). Enables interactive email experiences like carousels, forms, and real-time content. See https://amp.dev/about/email/ for more details.
"<!doctype html><html ⚡4email><head>...</head><body>...</body></html>"
Name of a pre-defined template to use for this email. When specified, the template's subject, htmlBody, and textBody will be used unless explicitly overridden in this request.
"welcome-email-v2"
Name of the IP pool to use for sending this email. If not specified, the default IP pool for the sub-account will be used.
"transactional"
Custom email headers to include in the message. Common uses: adding List-Unsubscribe headers, custom tracking IDs, or priority flags. Note: Some headers like From, To, Subject are set automatically and cannot be overridden.
Show child attributes
Show child attributes
{ "X-Custom-Header": "custom-value", "X-Campaign-ID": "summer-sale-2024", "List-Unsubscribe": "<mailto:unsubscribe@example.com>" }
Whether to track email opens using a tracking pixel. When enabled, a 1x1 transparent image is inserted into the HTML body. Default: true (if not specified)
true
Whether to track link clicks by rewriting URLs through SendPost's tracking domain. When enabled, all links in htmlBody are replaced with tracking URLs. Default: true (if not specified)
true
Tags/groups to categorize this email for analytics and reporting. Use groups to segment your email statistics (e.g., by campaign, email type, or customer segment).
[ "transactional", "order-confirmation", "premium-customers" ]
File attachments to include with the email. Maximum total attachment size: 25MB. Supported formats: PDF, images, documents, etc.
Show child attributes
Show child attributes
Custom webhook URL to receive events for this specific email. Overrides the default webhook configured at the account level. Useful for per-email or per-customer webhook routing.
"https://your-app.com/webhooks/email-events"
Template ID for the email template
Key-Value pair of template variables
Show child attributes
Show child attributes
Response
A list of email message response objects.
The recipient email address this response corresponds to
"customer@example.com"
UNIX epoch timestamp in nanoseconds when the email was accepted for processing. Use this for precise timing and correlation with webhook events.
1704067200000000000
Unique identifier (UUID) for this email message. Use this ID to track the email through webhooks and the message lookup API.
"550e8400-e29b-41d4-a716-446655440000"
Error code if the email submission failed. Common codes:
- 0: Success (no error)
- 1: Invalid recipient email
- 2: Recipient in suppression list
- 3: Domain not verified
- 4: Rate limit exceeded
- 5: Invalid sender email
0
Human-readable message describing the result. On success: "Email submitted successfully" On error: Description of what went wrong
"Email submitted successfully"
Was this page helpful?