. . .

Mandrill Module

Using the Mandrill module you can send emails directly from SDK or Server Code. Mandrill supports all kinds of email, from plaintext to template based HTML ones.

To use this module, you have to register at Mandrill first. To our knowledge, it’s free to send 12k mails per month.

images/download/attachments/10718869/db3-module-mandrill.PNG

Configuration

Your mandrill API Key

The API key you get after registering at mandrill api credentials

Usage

After adding the Mandrill module to your app, you will find a bunch of classes in your SDK. The central class for sending an email is the Message class. Let us send a simple plaintext email for explanation:

Android
private void setup() {
// set up the receiver
final Recipient rec = new Recipient();
rec.setEmail("receiver@domain.com");
rec.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
setUpMessage(rec);
}
}
});
}
 
private void setUpMessage(final Recipient rec) {
final Message msg = new Message();
msg.setFromEmail("my-email-address@domain.com");
msg.setText("My email content");
msg.setSubject("My email subject");
msg.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
postRecipients(msg, rec);
}
}
});
}
 
private void postRecipients(final Message msg, final Recipient rec) {
// you can send to multiple recipients, but we use only one in this
// example
msg.postRecipientsAsync(rec, new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
sendMessage(msg);
}
}
});
}
 
private void sendMessage(final Message msg) {
// send it. You can set a timetamp to send the email
// in future as UTC timestamp in YYYY-MM-DD HH:MM:SS format.
// If you specify a time in the past, the message will be sent
// immediately.
// An additional fee applies for scheduled email, and this feature is
// only available to accounts with a positive balance
msg.sendMessageAsync("", new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
// ...
}
}
});
}
iOS
//set up the receiver
AOMRecipient *rec = [[AOMRecipient alloc] init];
[rec setEmail:@"receiver@domain.com"];
[rec saveAsyncWithBlock:^(NSError *error) {
 
}];
 
//set up the message
AOMMessage *msg = [[AOMMessage alloc] init];
[msg setFromEmail:@"my-email-address@domain.com"];
[msg setText:@"My email content"];
[msg setSubject:@"My email subject"];
[msg saveAsyncWithBlock:^(NSError *error) {
//you can send to multiple recipients,
// but we use only one in this example
[msg postRecipients:rec andWithBlock:^(NSError *error) {
//send it away. You can set a timetamp to send the email
// in future as UTC timestamp in YYYY-MM-DD HH:MM:SS format.
// If you specify a time in the past, the message will be sent immediately.
// An additional fee applies for scheduled email, and this feature is
// only available to accounts with a positive balance
[msg sendMessageAsync:@"" andWithBlock:^(NSError *error) {
 
}];
}];
}];
JavaScript
function setupMandrill () {
var rec= new Apiomat.Recipient();
rec.setEmail("receiver@domain.com");
 
var recSaveCB = {
onOk : function() {
setUpMessage(rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
rec.save(recSaveCB);
}
 
function setUpMessage(rec) {
var msg= new Apiomat.Message();
msg.setFromEmail("my-email-address@domain.com");
msg.setText("My email content");
msg.setSubject("My email subject");
 
var msgSaveCB = {
onOk : function() {
postRecipients(msg, rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
msg.save(msgSaveCB);
}
 
function postRecipients(msg, rec) {
// you can send to multiple recipients, but we use only one in this
// example
var postRecipientsCB = {
onOk : function() {
sendMessage(msg);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
msg.postRecipients(rec, postRecipientsCB);
}
 
function sendMessage(msg) {
// send it. You can set a timetamp to send the email
// in future as UTC timestamp in YYYY-MM-DD HH:MM:SS format.
// If you specify a time in the past, the message will be sent
// immediately.
// An additional fee applies for scheduled email, and this feature is
// only available to accounts with a positive balance
var sendMessageCB = {
onOk : function() {
// ...
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
msg.sendMessage("", sendMessageCB);
 
}

You can create templates at Mandrill and set up variables, which can be used in the email content text:

Android
private void setupTemplate() {
// set up the receiver
final Recipient rec = new Recipient();
rec.setEmail("receiver@domain.com");
rec.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
// you want to let mandrill replace so-called merge tags in your template:
replaceMergeTags(rec);
}
}
});
}
 
private void replaceMergeTags(final Recipient rec) {
final MergeVariable mv = new MergeVariable();
mv.setName("MERGETAG");
mv.setContent("Text which should automatically replace the merge tag");
mv.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
// set up the mergetag for a specific recipient only
setUpMergeTag(mv, rec);
}
}
});
}
 
private void setUpMergeTag(final MergeVariable mv, final Recipient rec) {
final RecipientMergeVariables rmv = new RecipientMergeVariables();
rmv.setRecipientEmail("receiver@domain.com");
rmv.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
postVars(rmv, mv, rec);
}
}
});
}
 
private void postVars(final RecipientMergeVariables rmv,
final MergeVariable mv, final Recipient rec) {
rmv.postVarsAsync(mv, new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
setUpMessage(rmv, rec);
}
}
});
}
 
private void setUpMessage(final RecipientMergeVariables rmv,
final Recipient rec) {
// set up the message
final Message msg = new Message();
msg.setFromEmail("my-email-address@domain.com");
// select a template name you created in Mandrill before
msg.setTemplateName("templateName");
msg.setSubject("My email subject");
msg.saveAsync(new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
postMergeVars(msg, rmv, rec);
}
}
});
}
 
private void postMergeVars(final Message msg,
final RecipientMergeVariables rmv, final Recipient rec) {
msg.postMergeVarsAsync(rmv, new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
// you can send to multiple recipients, but we use only one in this example
postRecipients(msg, rec);
}
}
});
}
 
private void postRecipients(final Message msg, final Recipient rec) {
msg.postRecipientsAsync(rec, new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
sendMsg(msg);
}
}
});
}
 
private void sendMsg(final Message msg) {
msg.sendMessageWithTemplateAsync("", new AOMEmptyCallback() {
@Override
public void isDone(ApiomatRequestException exception) {
if (exception != null) {
// ...
}
}
});
}
iOS
//set up the receiver
AOMRecipient *rec = [[AOMRecipient alloc] init];
[rec setEmail:@"receiver@domain.com"];
[rec saveAsyncWithBlock:^(NSError *error) {
 
}];
//you you want to let mandrill replace so-called merge tags in your template:
AOMMergeVariable *mv = [[AOMMergeVariable alloc] init];
[mv setName:@"MERGETAG"];
[mv setContent:@"Text which should automatically replace the merge tag"];
[mv saveAsyncWithBlock:^(NSError *error) {
//set up the mergetag for a specific recipient only
AOMRecipientMergeVariables *rmv = [[AOMRecipientMergeVariables alloc] init];
[rmv setRecipientEmail:@"receiver@domain.com"];
[rmv saveAsyncWithBlock:^(NSError *error) {
[rmv postVars:mv andWithBlock:^(NSError *error) {
 
}];
}];
}];
//set up the message
AOMMessage *msg = [[AOMMessage alloc] init];
[msg setFromEmail:@"my-email-address@domain.com"];
//select a template name you created in Mandrill before
[msg setTemplateName:@"templateName"];
[msg setSubject:@"My email subject"];
[msg saveAsyncWithBlock:^(NSError *error) {
 
[msg postMergeVars:rmv andWithBlock:^(NSError *error) {
//you can send to multiple recipients, but we use only one in this example
[msg postRecipients:rec andWithBlock:^(NSError *error) {
//send it away (pay attention on the other method name here!!!)
[msg sendMessageWithTemplateAsync:@"" andWithBlock:^(NSError *error) {
 
}];
}];
}];
}];
JavaScript
function setupTemplate() {
// set up the receiver
var rec = new Apiomat.Recipient();
rec.setEmail("receiver@domain.com");
 
var saveCB = {
onOk : function() {
// you want to let mandrill replace so-called merge tags in your template:
replaceMergeTags(rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
rec.save(saveCB);
}
 
function replaceMergeTags(rec) {
var mv = new Apiomat.MergeVariable();
mv.setName("MERGETAG");
mv.setContent("Text which should automatically replace the merge tag");
var saveCB = {
onOk : function() {
// set up the mergetag for a specific recipient only
setUpMergeTag(mv, rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
mv.save(saveCB);
}
 
function setUpMergeTag(mv, rec) {
var rmv = new Apiomat.RecipientMergeVariables();
rmv.setRecipientEmail("receiver@domain.com");
var saveCB = {
onOk : function() {
postVars(rmv, mv, rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
rmv.save(saveCB);
}
 
function postVars(rmv, mv, rec) {
var postVarsCB = {
onOk : function() {
setUpMessage(rmv, rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
rmv.postVars(mv, postVarsCB);
}
 
function setUpMessage( rmv, rec) {
// set up the message
var msg = new Apiomat.Message();
msg.setFromEmail("my-email-address@domain.com");
// select a template name you created in Mandrill before
msg.setTemplateName("templateName");
msg.setSubject("My email subject");
 
var saveCB = {
onOk : function() {
postMergeVars(msg, rmv, rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
 
msg.save(saveCB);
}
 
function postMergeVars(msg, rmv, rec) {
var postMergeVarsCB = {
onOk : function() {
// you can send to multiple recipients, but we use only one in this example
postRecipients(msg, rec);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
msg.postMergeVarsAsync(rmv,postMergeVarsCB);
}
 
function postRecipients(msg, rec) {
var postRecipientsCB = {
onOk : function() {
sendMsg(msg);
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
msg.postRecipients(rec, postRecipientsCB);
}
 
function sendMsg(msg) {
var sendMessageWithTemplateCB = {
onOk : function() {
// ...
},
onError : function(error) {
console.log("Some error occured: (" + error.statusCode + ")" + error.message);
}
};
msg.sendMessageWithTemplate("",sendMessageWithTemplateCB);
}

You can even send emails via server code:

JavaScript
var rec = AOM.createObject("Mandrill","Recipient" );
rec.setEmail( "receiver@domain.com" );
rec.save();
 
var msg = AOM.createObject("Mandrill","Message" );
msg.setFromEmail( "my-email-address@domain.com" );
msg.setText( "My email text" );
msg.setSubject( "My email subject" );
msg.save();
 
msg.postRecipients( rec );
msg.sendMessage( "" );

The Mandrill module is available in all paid plans.

* link only available in Enterprise Documentation