Salesforce imposes a daily email limit of 5,000 messages per 24-hour period. Emails dispatched from our package contribute towards this constraint. To circumvent this limitation, we offer our clients an integration method with third-party Transactional Email Services, enabling them to send translated emails via the respective third-party API.
In this step-by-step guide we will use as an example mailchimp’s transactional email service, Mandrill. You need to adapt the implementation for your chosen provider.
Making sure you have the latest Unbabel Connector and Unbabel for SF Service Cloud packages installed, follow these steps:
1 - Implement IExternalEmailService
Unbabel has the following interface that you must implement
global interface IExternalEmailService {
unbabelsc.ExternalEmailServiceResponse sendTransactionalEmail(EmailMessage emailMessage, List<Attachment> attachments);
}
Below you can see a simple example of such implementation.
public with sharing class MandrillEmailService implements unbabelsc.IExternalEmailService{
public unbabelsc.ExternalEmailServiceResponse sendTransactionalEmail(EmailMessage em, List<Attachment> attachments){
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://mandrillapp.com/api/1.0/messages/send');
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json');
Body testBody = new Body();
testBody.key = 'KEY';
testBody.message = new Message();
testBody.message.from_email = em.FromAddress;
testBody.message.subject = em.subject;
testBody.message.text = em.textbody;
testBody.message.to = new List<To>();
To toItem = new To();
toItem.email = em.ToAddress;
toItem.type = 'to';
testBody.message.to.add(toItem);
req.setBody(JSON.serialize(testBody));
HTTPResponse res = new Http().send(req);
System.debug(res.getBody());
Map<String, Object> responseMap = (Map<String, Object>)(((List<Object>) JSON.deserializeUntyped(res.getBody()))[0]);
return new unbabelsc.ExternalEmailServiceResponse (res.getStatusCode() == 200 && (String) responseMap.get('status') == 'sent', (String) responseMap.get('reject_reason'), (String) responseMap.get('_id'), (Object) res);
}
private class Body {
String key;
Message message;
}
private class Message{
String from_email;
String subject;
String text;
List<To> to;
}
private class To{
String email;
String type;
}
}
The Class that implements IExternalEmailService should return a ExternalEmailServiceResponse
global class ExternalEmailServiceResponse {
global Boolean isSuccess { get; private set;}
global String errorMessage { get; private set;}
global String emailId { get; private set;}
global Object response { get; private set;}
global ExternalEmailServiceResponse( Boolean isSuccess, String errorMessage, String emailId, Object response){
this.isSuccess = isSuccess;
this.errorMessage = errorMessage;
this.emailId = emailId;
this.response = response;
}
global ExternalEmailServiceResponse( Boolean isSuccess, String errorMessage, String emailId){
this.isSuccess = isSuccess;
this.errorMessage = errorMessage;
this.emailId = emailId;
}
}
2 - Create Remote Site Setting
Head to Setup -> Security -> Remote Site Settings and click New to create a new Remote Site Setting. Following the example with Mandrill it should look like this:
Note: Use the url for your own Email Service
The metadata will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<RemoteSiteSetting xmlns="http://soap.sforce.com/2006/04/metadata">
<disableProtocolSecurity>false</disableProtocolSecurity>
<isActive>true</isActive>
<url>https://mandrillapp.com</url>
</RemoteSiteSetting>
3 - Create Metadata Record
Head to Setup -> Custom Code -> Custom Metadata Types -> Unbabel Email Dispatcher Option -> Manage Unbabel Email Dispatcher Option and select New
Type the developerName of the Apex class responsible for preparing the request to call your transactional email service and Save
The metadata should look like this:
<?xml version="1.0" encoding="UTF-8"?>
<CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<label>Mandrill</label>
<protected>false</protected>
<values>
<field>ImplementationApexClass__c</field>
<value xsi:type="xsd:string">MandrillEmailService</value>
</values>
<values>
<field>IsActive__c</field>
<value xsi:type="xsd:boolean">true</value>
</values>
</CustomMetadata>
4 - Test the integration to see if everything is working.
Comments
0 comments
Please sign in to leave a comment.