How to send emails via API

How to send emails via API

Learn how you can send emails with VMA Emailer API.

To use the send command please use POST to https://api.elasticemail.com/v2/email/send with the parameters listed below.

PARAMETERS:
* apikey=your api key,
* subject=email subject,
* from=from email address,
* to=List of email recipients (each email is treated separately, like a BCC). Separated by comma or semicolon. We suggest using the "msgTo" parameter.
* bodyHtml=html email body [optional],
* bodyText=text email body [optional],
* isTransactional - True, if email is transactional (non-bulk, non-marketing, non-commercial). Otherwise, false

Full list of additional parameters is available in our API Documentation.

C#

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

namespace ElasticEmailClient
{
    class Program
    {
        static void Main(string[] args)
        {
            NameValueCollection values = new NameValueCollection();
            values.Add("apikey", "00000000-0000-0000-0000-000000000000");
            values.Add("from", "youremail@yourdomain.com");
            values.Add("fromName", "Your Company Name");
            values.Add("to", "recipient1@gmail.com;recipient2@gmail.com");
            values.Add("subject", "Your Subject");
            values.Add("bodyText", "Text Body");
            values.Add("bodyHtml", "<h1>Html Body</h1>");
values.Add("isTransactional", true);

            string address = "https://api.elasticemail.com/v2/email/send";

            string response = Send(address, values);

            Console.WriteLine(response);
        }

        static string Send(string address, NameValueCollection values)
        {
            using (WebClient client = new WebClient())
            {
                try
                {
                    byte[] apiResponse = client.UploadValues(address, values);
                    return Encoding.UTF8.GetString(apiResponse);

                }
                catch (Exception ex)
                {
                    return "Exception caught: " + ex.Message + "\n" + ex.StackTrace;
                }
            }
        }

    }
}

Java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class ElasticEmailClient {
 
 public static String Send(String userName, String apiKey, String from, String fromName, String subject, String body, String to, String isTransactional) {
 
 try {
 
 String encoding = "UTF-8";
 
 String data = "apikey=" + URLEncoder.encode(apiKey, encoding);
 data += "&from=" + URLEncoder.encode(from, encoding);
 data += "&fromName=" + URLEncoder.encode(fromName, encoding);
 data += "&subject=" + URLEncoder.encode(subject, encoding);
 data += "&bodyHtml=" + URLEncoder.encode(body, encoding);
 data += "&to=" + URLEncoder.encode(to, encoding);
 data += "&isTransactional=" + URLEncoder.encode(isTransactional, encoding);
 
 URL url = new URL("https://api.elasticemail.com/v2/email/send");
 URLConnection conn = url.openConnection();
 conn.setDoOutput(true);
 OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
 wr.write(data);
 wr.flush();
 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
 String result = rd.readLine();
 wr.close();
 rd.close();

 return result;
 }
 
 catch(Exception e) {
 
 e.printStackTrace();
 }
 }

}

PHP

<?php
$url = 'https://api.elasticemail.com/v2/email/send';

try{
        $post = array('from' => 'youremail@yourdomain.com',
'fromName' => 'Your Company Name',
'apikey' => '00000000-0000-0000-0000-000000000000',
'subject' => 'Your Subject',
'to' => 'recipient1@gmail.com;recipient2@gmail.com',
'bodyHtml' => '<h1>Html Body</h1>',
'bodyText' => 'Text Body',
'isTransactional' => false);

$ch = curl_init();
curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false
        ));

        $result=curl_exec ($ch);
        curl_close ($ch);

        echo $result;
}
catch(Exception $ex){
echo $ex->getMessage();
}
?>

Python

import requests
import json
from enum import Enum
class ApiClient:
apiUri = 'https://api.elasticemail.com/v2'
apiKey = '00000000-0000-0000-0000-0000000000000'

def Request(method, url, data):
data['apikey'] = ApiClient.apiKey
if method == 'POST':
result = requests.post(ApiClient.apiUri + url, params = data)
elif method == 'PUT':
result = requests.put(ApiClient.apiUri + url, params = data)
elif method == 'GET':
attach = ''
for key in data:
attach = attach + key + '=' + data[key] + '&'
url = url + '?' + attach[:-1]
result = requests.get(ApiClient.apiUri + url)

jsonMy = result.json()

if jsonMy['success'] is False:
return jsonMy['error']

return jsonMy['data']

def Send(subject, EEfrom, fromName, to, bodyHtml, bodyText, isTransactional):
return ApiClient.Request('POST', '/email/send', {
'subject': subject,
'from': EEfrom,
'fromName': fromName,
'to': to,
'bodyHtml': bodyHtml,
'bodyText': bodyText,
'isTransactional': isTransactional})

print(Send("Your Subject", "youremail@yourdomain.com", "Your Company Name", "recipient1@gmail.com;recipient2@gmail.com", "<h1>Html Body</h1>", "Text Body", True))

License

All code samples are licensed under MIT license.

    • Related Articles

    • How to send email with attachments via API

      Learn how to send email with attachments via VMA Emailer API. Sending with attachments is not that much different from Mail Merge. You need to provide your attachments as the Multipart/Form-Data POST field in your Send request, so the specific ...
    • I Can't Send Emails

      You may have exceeded the mail quota imposed by your account. This limits the number of emails you may send in a specific period. You TRIAL period may have ended Check with support if you are unsure.
    • How to send to a list or segment

      There are few ways to choose a specific contact list or segment you want to send your campaign to. All of them are very simple. In this section, we assume that you have already created a list or segment of contacts. If not, don't worry, we've got it ...
    • PHP API Integration Library

      Learn more about how to download and implement our PHP API Integration Library to your own project. Overview - what this Library is? We want to provide our clients a way to use our system however they like. That is why we have introduced the ...
    • How to configure SMTP & API

      Your VMA Emailer SMTP & API Settings. HTTP API Settings HTTP API Documentation Our complete version 2 API Documentation is here. API Key Your API Key is a GUID and it is the key to your account when trying to gain access or make API calls while ...