You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Sending Eloqua Email from Salesforce Using REST API


By: Radha

Eloqua integrates with Salesforce using REST Client API to send emails. We are using Salesforce Apex Code to send Eloqua emails. First, we have to install Advanced REST Client API tool as a Plug-in for browser extension, and use this tool to test sending the Eloqua emails. The Eloqua API authentication uses base-64 encoded value and it should be passed in every API call as part of the authorization header.

Basic Email Processing in Eloqua API Method:
There are two ways to send email using the Eloqua API:

Email Test Deployment:
To send an Email using the first method, we have to choose an existing Email in Eloqua from a Contact.

The sample JSON Request process as follows,
End Point URL : https://secure.eloqua.com/API/REST/1.0/assets/email/deployment
Method : POST
Header : Authorization: Basic XXX
The ‘XXX’ value is an authorization token. This token will be generated by using Base64encoder method. Convert UserName\CompanyName and Password to a base64encoder value i.e., Authorization token.

  1. Ex: username1\companyname
  2. password123

Body : It is a JSON format.

{
"contactId" :"00000123", // Eloqua Contact ID
"sendFromUserId":"123", // Eloqua User ID of the Sender
"email":{
"id":"123", // Eloqua assest Email Id
"name":"SampleEmail", // Eloqua asset Email name
},
"name":"Sample Test Deployment", // Email deployment name
"type":"EmailTestDeployment" // Email deployment type
}

Content–Type : appliation/json

Email Inline Deployment:
In this method, we have to create our own custom content to send email to one or many contacts, but that email content won’t be stored in Eloqua asset. The email is sent to contact(s) through Eloqua.

The sample JSON Request process as follows,
End Point URL : https://secure.eloqua.com/API/REST/1.0/assets/email/deployment
Method : POST
Header : Authorization: Basic XXX
Body : It is a JSON format,

{
“contacts”:[	// Adding a list of Eloqua contacts.
 {
“emailAddress”:”test@live.com”,
“id”:”0000123” // Eloqua contact id
 }
 ],
“sendFromUserId”:”123”, // Eloqua user id
“email”:{
“htmlContent”:{ // Html content body
“type”:”RasHtmlContent”,				 “html”:”<html><head></head><body>Test</body</html>”
 },
 “id”:”null”,
 “isPlainTextEditable”:”fase”, // The text content is editable or not
 “name”:”Sample Email”, // Email name
“plainText”:”null”,
“subject”:”Test Sample” // Emil subject content
},
“name”:”Sample Deploymnet”, // Email deployment name
“type”:”EmailInlineDeployment”
}

Content–Type : appliation/json

Finally, click the Send button and you will see the response of the call.

The below is the screenshot as REST API Call,

eapi2909-1.png

Implementing API Call into SFDC Apex Code:
We want to send email through Eloqua from Salesforce, because there are some limitations with sending email process in Salesforce.

The sample Apex Code,
First, create an Apex class and declare a variable to store Eloqua credentials. Then, get an authorization token by using based64encode method.

String userName = 'abc'+'\'+'company';
String passWord = 'abc1';
Blob headerValue = Blob.valueOf(userName+':'+passWord);
String authorize = 'Basic '+EncodingUtil.base64Encode(headerValue);

Then, query a list of contacts from Contact Object which contains the field values of Eloqua Contact Id and Email, and these values are added into a wrapper class instance list.

List<WrapperClass.ContactList> conts = new List<WrapperClass.ContactList>();
	for(Contact con : contacts) // Queried the list of contacts stored in 'contacts' variable.
	{
	WrapperClass.ContactList cons = new WrapperClass.ContactList();
	cons.emailAddress = con.Email; // Contact field Email Id 
	cons.id = con.Eloqua_Person_ID__c; // Custom field in contact object.
	conts.add(cons);
	}

To query an Email Template content from a Custom Object,

String contentBody;

Add an Email Template’s values into wrapper class variables for converting a JSON format into the content body and then pass it to setBody() method.

WrapperClass wrapp = new WrapperClass();
wrapp.name = 'Sending Email'; // Email Name
wrapp.sendFromUserId = '123' // Eloqua User ID
wrapp.type = 'EmailInlineDeployment'; // Email Type
wrapp.contacts = conts; // List of contants

WrapperClass.HtmlContent htmlCont = new WrapperClass.HtmlContent();
htmlCont.type = 'RawHtmlContent'; // Html type
htmlCont.html = '<html><body><h1>Hi,</h1><br>Sample Test Content<br><h2>Thanks</h2></body></html>';

WrapperClass.Emails emailCont = new WrapperClass.Emails();
emailCont.name = 'Sample Test Email'; // Email Template Name
emailCont.subject = 'Welcome!!!'; // Email subject line
emailCont.htmlContent = htmlCont;
wrapp.email = emailCont;
contentBody = JSON.serializePretty(wrapp);

Now, construct a Http Request using REST API endpoint URL, send the email, and receive a HTTP Response back.

HttpRequest request=new HttpRequest();
            HttpResponse response=new HttpResponse();
            Http hp=new Http();
            request.setEndPoint('https://secure.eloqua.com/API/REST/1.0/assets/email/deployment');
            request.setHeader('Authorization',authorize);
            request.setHeader('Content-Type','application/json');
            request.setBody(contentBody);
            request.setMethod('POST');
            response=hp.send(request);
            sytem.debug('Response------'+response.getBody()); // Got the reponse body of the content

So, this functionality can be triggered by any event (for e.g. clicking a Button) in Salesforce, and emails can be sent to a list of contacts. If duplicate records exist in the list, then the email won’t be sent to any of the contacts.

The following is a wrapper class,

public class WrapperClass{
	public ContactList[] contacts;
	public Integer sendFromUserId; // sendFromUserId is a keyword
	public Emails email;
	public String name; // name is a keyword
	public String type; // type is a keyword
	public class ContactList{
	public Stirng id; // id is a keyword
	public String emailAddress; // emailAddress is a keyword
	}
	public class Emails{
	public HtmlContent htmlContent;
	public String name; // name is a keyword
	public String subject; // subject is a keyword
	}
	public class HtmlContent{
	public String type; // type is a keyword
	public String html; // html is a keyword
	}

Note: The “Eloqua Engage” User License is required for the “Eloqua User” account that can send emails. The user credential is used in the authorization header.

Reference Links:
Eloqua REST API Authentication
Sending Email Using Eloqua REST API
Sending Email to Contact Using Eloqua REST API