Integrating email API within your website or app gives you the opportunity to create your own independent email infrastructure. With email API, you can send emails in high volumes, directly from your software. Also, you maximize deliverability and automate various email tasks. But how do you send emails via API?
Table of contents
- Sending your first email via email API
- Generate your API key
- Choose your email template
- Let's send a test email
Sending your first email via email API
To start sending your emails, you will need to follow a couple small steps.
1. Generate your API key
The first step to sending via email API is to authenticate your application and to do this, you will need an API key. You can generate it on your Elastic Email account in Settings>Manage API Keys>Create. Once you create your API key, keep it safe, as it will be used for every API call you make in order to identify you and confirm your account’s credentials. Your API key should be sent inside the header with the parameter name ‘x-elasticemail-apikey’ and your API key as a value.
2. Choose your email template
The next essential step for sending emails via API is an email template. You can create it in our Email Designer or HTML editor and save it on your account. Then, you provide its name in your API call. Another option is to input the content details directly into the call.
3. Let's send a test email
Our RESTful API allows you to send emails via the POST method. The required parameter to make this API call is Recipients. You can also input additional parameters into your API call. To get more details, check our API documentation.
We also prepared code samples to send out a simple test email in a few programming languages:
# install library: npm install @elasticemail/elasticemail-client
let ElasticEmail = require('@elasticemail/elasticemail-client');
let defaultClient = ElasticEmail.ApiClient.instance;
let apikey = defaultClient.authentications['apikey'];
apikey.apiKey = "895A382DF3DCC13A97E72EXAMPLEKEY"
let api = new ElasticEmail.EmailsApi()
let email = ElasticEmail.EmailMessageData.constructFromObject({
Recipients: [
new ElasticEmail.EmailRecipient("MeowWow ")
],
Content: {
Body: [
ElasticEmail.BodyPart.constructFromObject({
ContentType: "HTML",
Content: "My test email content ;)"
})
],
Subject: "JS EE lib test",
From: "MyEmail "
}
});
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully.');
}
};
api.emailsPost(email, callback);
// Add Nuget package ElasticEmail to the project
// https://www.nuget.org/packages/ElasticEmail/
using System.Diagnostics;
using ElasticEmail.Api;
using ElasticEmail.Client;
using ElasticEmail.Model;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var config = new Configuration();
config.ApiKey.Add("X-ElasticEmail-ApiKey", "895A382DF3DCC13A97E72EXAMPLEKEY");
var apiInstance = new EmailsApi(config);
try
{
var messageData = new EmailMessageData(
new System.Collections.Generic.List()
{
new EmailRecipient("MeowWow ")
},
new EmailContent(
new System.Collections.Generic.List()
{
new BodyPart()
{
ContentType = BodyContentType.HTML,
Content = "My test email content ;)"
}
},
subject: "EE C# lib test",
from: "MyEmail "
)
);
var apiResponse = apiInstance.EmailsPost(messageData);
}
catch (ApiException e)
{
Debug.Print("Exception when calling EmailsPost: " + e.Message);
Debug.Print("Status Code: " + e.ErrorCode);
Debug.Print(e.StackTrace);
}
}
}
}
# install EE library: pip3 install ElasticEmail
import ElasticEmail
from ElasticEmail.api import emails_api
from ElasticEmail.model.email_message_data import EmailMessageData
from ElasticEmail.model.email_recipient import EmailRecipient
from ElasticEmail.model.email_content import EmailContent
from ElasticEmail.model.body_part import BodyPart
from pprint import pprint
configuration = ElasticEmail.Configuration()
configuration.api_key['apikey'] = '895A382DF3DCC13A97E72EXAMPLEKEY'
with ElasticEmail.ApiClient(configuration) as api_client:
api_instance = emails_api.EmailsApi(api_client)
email_message_data = EmailMessageData(
recipients=[
EmailRecipient(
email="MeowWow "
),
],
content={
"Body": [
{
"ContentType":"HTML",
"Content":"My test email content ;)"
}
],
"Subject": "Python EE lib test",
"From": "MyEmail "
}
)
try:
api_response = api_instance.emails_post(email_message_data)
pprint(api_response)
except ElasticEmail.ApiException as e:
print("Exception when calling EmailsApi->emails_post: %s\n" % e)
# install library: gem install ElasticEmail (may require installation of ruby-dev in the system)
require 'ElasticEmail'
ElasticEmail.configure do |config|
config.api_key['apikey'] = '895A382DF3DCC13A97E72EXAMPLEKEY'
end
api_instance = ElasticEmail::EmailsApi.new
email = ElasticEmail::EmailMessageData.new(
Hash[
"recipients" => [ ElasticEmail::EmailRecipient.new(Hash["email" => "MeowWow "]) ],
"content" => ElasticEmail::EmailContent.new(
"body" => [ ElasticEmail::BodyPart.new(
"content" => "My test email content ;)",
"content_type" => "HTML"
) ],
"subject" => "Ruby EE lib test",
"from" => "MyEmail "
)
]
)
begin
api_instance.emails_post(email)
rescue ElasticEmail::ApiError => e
puts "Exception when calling EE API: #{e}"
end
# 1) install php and php-curl module
# 2) install composer (https://getcomposer.org/)
# 3) create composer.json file:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/elasticemail/elasticemail-php.git"
}
],
"require": {
"elasticemail/elasticemail-php": "*@dev"
}
}
# 4) run composer install
# 5) write and run your script
require_once('vendor/autoload.php');
$config = ElasticEmail\Configuration::getDefaultConfiguration()
->setApiKey('X-ElasticEmail-ApiKey', '895A382DF3DCC13A97E72EXAMPLEKEY');
$apiInstance = new ElasticEmail\Api\EmailsApi(
new GuzzleHttp\Client(),
$config
);
$email = new \ElasticEmail\Model\EmailMessageData(array(
"recipients" => array(
new \ElasticEmail\Model\EmailRecipient(array("email" => "mail@contact.com"))
),
"content" => new \ElasticEmail\Model\EmailContent(array(
"body" => array(
new \ElasticEmail\Model\BodyPart(array(
"content_type" => "HTML",
"content" => "My content"
))
),
"from" => "email@domain.com",
"subject" => "My Subject"
))
));
try {
$apiInstance->emailsPost($email);
} catch (Exception $e) {
echo 'Exception when calling EE API: ', $e->getMessage(), PHP_EOL;
}
/* Initialization */
import { Configuration, EmailsApi, EmailMessageData } from '@elasticemail/elasticemail-client-ts-axios';
/* Generate and use your API key */
const config = new Configuration({
apiKey: "YOUR_API_KEY"
});
const emailsApi = new EmailsApi(config);
const emailMessageData = {
Recipients: [
{
Email: "MeowWow ",
Fields: {
name: "Name"
}
}
],
Content: {
Body: [
{
ContentType: "HTML",
Charset: "utf-8",
Content: "My test email content ;)"
},
{
ContentType: "PlainText",
Charset: "utf-8",
Content: "Hi {name}!"
}
],
From: "MyEmail ",
Subject: "Typescript Axios EE lib test"
}
}; // interface EmailMessageData from '@elasticemail/elasticemail-client-ts-axios'
const sendBulkEmails = (emailMessageData: EmailMessageData): void => {
emailsApi.emailsPost(emailMessageData).then((response) => {
console.log('API called successfully.');
console.log(response.data);
}).catch((error) => {
console.error(error);
});
};
sendBulkEmails(emailMessageData)
# download our ElasticEmail-WebApiClient and the library
# to install this module, run the following commands:
# perl Makefile.PL
# make
# make test
# make install
use Data::Dumper;
use ElasticEmail::EmailsApi;
use ElasticEmail::Object::EmailMessageData;
use ElasticEmail::Object::EmailRecipient;
use ElasticEmail::Object::EmailContent;
use ElasticEmail::Object::BodyPart;
my $api_instance = ElasticEmail::EmailsApi->new(
# Configure API key authorization: apikey
api_key => {'X-ElasticEmail-ApiKey' => 'YOUR_API_KEY'},
# uncomment below to setup prefix (e.g. Bearer) for API key, if needed
#api_key_prefix => {'X-ElasticEmail-ApiKey' => 'Bearer'},
);
my $email_message_data = ElasticEmail::Object::EmailMessageData->new(); # EmailMessageData | Email data
my $recipient = ElasticEmail::Object::EmailRecipient->new();
$recipient->email("MyEmail ");
my $content = ElasticEmail::Object::EmailContent->new();
my $body = ElasticEmail::Object::BodyPart->new();
$body->content('My test email content ;)');
my $body_content_type = 'HTML';
$body->content_type($body_content_type);
$content->body([$body]);
$content->reply_to('MyEmail ');
$content->from('MyEmail ');
$content->subject('Perl EE lib test');
$content->template_name('template name');
$email_message_data->recipients([$recipient]);
$email_message_data->content($content);
eval {
my $result = $api_instance->emails_post(email_message_data => $email_message_data);
print Dumper($result);
};
if ($@) {
warn "Exception when calling EmailsApi->emails_post: $@\n";
}
# download and install the lib from ElasticEmail repository and make sure the script has executable rights: $ chmod u+x ElasticEmail
#!/bin/bash
host=https://api.elasticemail.com
apiKey=YOUR_API_KEY
apiKeyHeaderName=X-ElasticEmail-ApiKey
apiKeyHeader=$apiKeyHeaderName:$apiKey
apiMethod=emailsPost
json='{
Recipients: [
{
Email: "MeowWow ",
Fields: {
name: "MeowWow"
}
}
],
Content: {
Body: [
{
ContentType: "HTML",
Charset: "utf-8",
Content: "My test email content ;)"
},
{
ContentType: "PlainText",
Charset: "utf-8",
Content: "Hi {name}!"
}
],
From: "MyEmail ",
Subject: "Bash EE lib test"
}
}'
echo $json | ./ElasticEmail --host $host --content-type json $apiMethod - $apiKeyHeader
To learn more about API calls in a particular programming language, visit our GitHub repository.