Skip to content

Instantly share code, notes, and snippets.

@olitomas
Created June 15, 2016 09:34
Show Gist options
  • Save olitomas/bad88a0d63f015ca89407a82edabc816 to your computer and use it in GitHub Desktop.
Save olitomas/bad88a0d63f015ca89407a82edabc816 to your computer and use it in GitHub Desktop.
var sendEmail = function(settings) {
//attachmentName should be the full name. Example: reikningur.pdf
var s = {
from: settings.from,
replyTo: settings.replyTo,
to: settings.to,
subject: settings.subject,
body: settings.body,
attachment: settings.attachment,
attachmentName: settings.attachmentName,
attachmentType: settings.attachmentType
}
var allEmailsValid = true;
//to variable can be a string or an array of strings
var recipients = [];
if (s.to.constructor === Array) {
$.each(s.to, function(key, value) {
if (!_isValidEmail(value)) allEmailsValid = false;
recipients.push({ 'email': value });
});
} else {
recipients.push({ 'email': s.to });
}
var m = new mandrill.Mandrill('hBA_uid-xHuZ68IaJs8LXQ');
var params = {
message: {
from_email: s.from || 'seatours@lendingar.is',
headers: {
'Reply-To': s.replyTo || 'lendingar@lendingar.is'
},
to: recipients,
subject: s.subject,
html: s.body,
autotext: true, //turns html content to plaintext if mailclient does not support html
track_opens: true,
track_clicks: true
}
};
if (s.attachment) {
params.message.attachments = [{
type: s.attachmentType,
name: s.attachmentName,
content: s.attachment,
}];
}
m.messages.send(params, function(rsp) {
return rsp;
}, function(error) {
console.log(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment