Create a multipart/form-data encoded string. Useful for sending attachments with Mailgun.
// Call MultipartFormData() anywhere in your server code
/*
Structure of 'parts' parameter
--- Can take any key-value pair required by email API
--- 'attachment' key should include object using options below. Will be treated separately to send file data appropriately
*/
parts = {
to: '[email protected]',
from: 'Bill Gates <[email protected]>',
subject: 'How are you?',
attachment: {
contentType: 'text/calendar',
filename: 'CoffeeDate.ics',
value: IcsData
}
}
var formatted = MultipartFormData(parts);
// MultipartFormData returns headers and content
var headers = formatted.headers;
var content = formatted.content;
/*
Sending through Mailgun. Using Mailgun 'recipient variables' and attachment
*/
var parts = {
from: '[email protected]',
to: ['[email protected]', '[email protected]'],
'h:Reply-To': '[email protected]',
subject: subject,
html: '<p>How is everything going %recipient.name%?</p><p>Your friend,<br>Steve</p>',
'recipient-variables': JSON.stringify({
'[email protected]': {
name: 'Jony'
},
'[email protected]': {
name: 'Steve'
}
}),
'o:tag': 'Personal Message',
attachment: {
contentType: 'text/csv',
name: 'spreadsheet1.csv',
value: CsvData.generate() // fake function to generate csv data
}
};
var formData = MultipartFormData(parts);
HTTP.call(
'POST',
'https://api.mailgun.net/v2/yourdomain.com/messages',
{
auth: 'api:key-***************************',
content: formData.content,
headers: formData.headers
},
function (error, result) {
if (error) {
console.log(error, 'Error sending email.');
}
}
);