Send hundreds or thousands of personalized emails with two API calls.
This is a deprecated API Call. For Updated API Documentation Visit: http://api.elasticemail.com/public/help
Using this technique you can define an email template which contains special fields such as {firstname}, {lastname} or many others and send personalized emails in a very efficient way to your clients.
For example:
Dear {firstname} {lastname},
This is our monthly newsletter. We hope you have been enjoying our service. You can access your account at {link}.
If you would like to allow custom headers, please make sure this has been activated on your advanced options screen.
To use the mail merge you need to make two calls to the API. One to upload a CSV (comma separated values) data source file using the upload attachments API and a second call to the send API with the specified datasource.
Create a CSV file
Step 1) Create a CSV file like the following:
"ToEmail","FirstName","LastName"
"email1@email.com","Joe","Plumber"
"email2@email.com", "Jackie","Smith"
The first column of the data must be ToEmail and will be used as the recipients of the merged email. The other columns represent the data fields that you can use in your email. You can have as many columns as you need. If you have the header FirstName you can include that in your email template with {FirstName} and all references to that field will be replaced before sending.
Upload attachments API
Step 2) Call the attachments upload API to upload the CSV file.
To upload your attachment do a HTTP PUT of your attachment file to:
Call to the send API
Step 3) Call mailer send with the datasource value set:
To use the send command POST to https://api.elasticemail.com/mailer/send with the following form values:
username=your account email address,
api_key=your API key,
from=from email address,
from_name=display name for from email address,
subject=email subject,
body_html=html email body [optional],
body_text=text email body [optional],
data_source=your CSV file name or attachment id (eg: test.csv),
charset=text value of encoding for example: iso-8859-1, windows-1251, utf-8, us-ascii, windows-1250 and more…,
encoding for None, 1 for Raw7Bit, 2 for Raw8Bit, 3 for QuotedPrintable, 4 for Base64 (Default), 5 for Uue (note that you can also provide the text version such as "Raw7Bit" for value 1).
If sent correctly you will receive a response like:
f74b9f96-f89a-4cfe-813f-5f86df1cb37f
This is the transaction ID of your send job. You can use this transaction ID to check on the statistics of the given job using the Get Status API.
Your email will be sent to each email address in the CSV file and the data will be merged with the CSV data.
Note: There is a limit of 100000 emails / uploaded CSV file for larger jobs please split into separate calls.
C#
using System; using System.IO; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Net; namespace BasicElasticEmailSend { public static string ElasticEmailMailMerge() { string username = "your username"; string apiKey = "your api key"; string csvLocation = @"D:\test.csv"; string dataSourceName = "test.csv"; string csv = File.ReadAllText(csvLocation); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(csv)); WebRequest request = WebRequest.Create("https://api.elasticemail.com/attachments/upload?username=" + username + "&api_key=" + apiKey + "&file=" + dataSourceName); request.Method = "PUT"; request.ContentLength = stream.Length; Stream outstream = request.GetRequestStream(); stream.CopyTo(outstream, 4096); stream.Close(); WebResponse response = request.GetResponse(); string attachID = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd(); response.Close(); WebClient client = new WebClient(); NameValueCollection values = new NameValueCollection(); values.Add("username", username); values.Add("api_key", apiKey); values.Add("from", "you@yourdomain.com"); values.Add("from_name", "Your Name"); values.Add("subject", "This is a test of Email Templates"); values.Add("body_html", "Dear {FirstName} {LastName}, templates are here!"); values.Add("body_text", "Dear {FirstName} {LastName}, templates are here!"); values.Add("data_source", attachID); byte[] result = client.UploadValues("https://api.elasticemail.com/mailer/send", values); string res = Encoding.UTF8.GetString(result); return res; } }
PHP
class BaseElasticEmail { private $apiKey = "PUT_YOUR_KEY_HERE"; private $userName = "PUT_YOUR_USERNAME_HERE"; function __construct($userName=NULL,$apiKey=NULL) { if($userName != NULL) $this->userName = $userName; if($apiKey != NULL) $this->apiKey = $apiKey; } /** * Sending simple email * @param string $from * @param string $fromName * @param string $to * @param string $subject * @param string $bodyText * @param string $bodyHTML */ function sendMail($from, $fromName, $to, $subject, $bodyText, $bodyHTML) { $res = ""; $data = "username=".urlencode($this->userName); $data .= "&api_key=".urlencode($this->apiKey); $data .= "&from=".urlencode($from); $data .= "&from_name=".urlencode($fromName); $data .= "&to=".urlencode($to); $data .= "&subject=".urlencode($subject); if($bodyHTML) $data .= "&body_html=".urlencode($bodyHTML); if($bodyText) $data .= "&body_text=".urlencode($bodyText); $header = "POST /mailer/send HTTP/1.0\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($data) . "\r\n\r\n"; $fp = @fsockopen('ssl://api.elasticemail.com', 443, $errno, $errstr, 30); if(!$fp) { return "ERROR. Could not open connection"; } else { fputs ($fp, $header.$data); while (!feof($fp)) { $res .= fread ($fp, 1024); } fclose($fp); } return $res; } /** * Uploading attachments * @param string $content Content of the file to upload * @param string $fileName */ function uploadAttachment($content, $fileName) { $res = ""; $header = "PUT /attachments/upload?username=".urlencode($this->userName)."&api_key=".urlencode($this->apiKey)."&file=".urlencode($fileName)." HTTP/1.0\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($content) . "\r\n\r\n"; $fp = @fsockopen("ssl://api.elasticemail.com", 443, $errno, $errstr, 30); if(!$fp) { return "ERROR. Could not open connection"; } else { fputs ($fp, $header.$content); while (!feof($fp)) { $res .= fread ($fp, 1024); } fclose($fp); } $res = explode("\r\n\r\n", $res, 2); return ((isset($res[1])) ? $res[1] : ''); } /** * Sending emails with Elastic MailMerge * @param string $csv Content of the CSV File to send * @param string $from * @param string $fromName * @param string $subject * @param string $bodyText * @param string $bodyHTML */ function mailMerge($csv, $from, $fromName, $subject, $bodyText, $bodyHTML=NULL) { $csvName = 'mailmerge.csv'; $attachID = $this->uploadAttachment($csv, $csvName); $res = ""; $data = "username=".urlencode($this->userName); $data .= "&api_key=".urlencode($this->apiKey); $data .= "&from=".urlencode($from); $data .= "&from_name=".urlencode($fromName); $data .= "&subject=".urlencode($subject); $data .= "&data_source=".urlencode($attachID); if($bodyHTML) $data .= "&body_html=".urlencode($bodyHTML); if($bodyText) $data .= "&body_text=".urlencode($bodyText); $header = "POST /mailer/send HTTP/1.0\r\n"; $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; $header .= "Content-Length: " . strlen($data) . "\r\n\r\n"; $fp = @fsockopen('ssl://api.elasticemail.com', 443, $errno, $errstr, 30); if(!$fp) { return "ERROR. Could not open connection"; } else { fputs ($fp, $header.$data); while (!feof($fp)) { $res .= fread ($fp, 1024); } fclose($fp); } return $res; } } # Demo : require_once 'BaseElasticEmail.php'; $ee = new BaseElasticEmail(); $csv = '"ToMail","Title","FirstName","LastName"'."\n"; $csv .= '"smith@example.com","Mr","Alexander","Smith"'."\n"; $csv .= '"dupon@example.com","Miss","Sarah","Dupon"'."\n"; $text = 'Hello {Title} {LastName}, your first name is {FirstName}.' $res = $ee->mailMerge($csv, "demo@example.com", "Demo", "Demo Mail Merge", $text); var_dump($res);
Visual Basic
Imports System Imports System.IO Imports System.Net Imports System.Text Imports System.Xml Imports System.Web Imports System.Collections.Specialized Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim username As String = "your Username" Dim apiKey As String = "the API key" Dim csvLocation As String = "D:\testdata.csv" Dim dataSourceName As String = "testdata.csv" Dim csv As String = File.ReadAllText(csvLocation) Dim stream As New MemoryStream(Encoding.UTF8.GetBytes(csv)) Dim request As WebRequest = WebRequest.Create(Convert.ToString((Convert.ToString((Convert.ToString("https://api.elasticemail.com/attachments/upload?username=") & username) + "&api_key=") & apiKey) + "&file=") & dataSourceName) request.Method = "PUT" request.ContentLength = stream.Length Dim outstream As Stream = request.GetRequestStream() stream.CopyTo(outstream, 4096) stream.Close() Dim response As WebResponse = request.GetResponse() Dim attachID As String = New StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd() response.Close() Dim client As New WebClient() Dim values As New NameValueCollection() values.Add("username", username) values.Add("api_key", apiKey) values.Add("from", "you@yourdomain.com") values.Add("from_name", "Your Name") values.Add("subject", "This is a test of Email Templates") values.Add("body_html", "Dear {FirstName} {LastName}, templates are here!") values.Add("body_text", "Dear {FirstName} {LastName}, templates are here!") values.Add("data_source", attachID) Dim result As Byte() = client.UploadValues("https://api.elasticemail.com/mailer/send", values) Dim res As String = Encoding.UTF8.GetString(result) End Sub End Class
License
Copyright (c) 2016-2017 Elastic Email, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.