Skip to content

Instantly share code, notes, and snippets.

@torpio
Created May 22, 2014 16:04
Show Gist options
  • Save torpio/1ca70412e8ead03bb915 to your computer and use it in GitHub Desktop.
Save torpio/1ca70412e8ead03bb915 to your computer and use it in GitHub Desktop.
// First we fetch the logo that we'll embed in the email
http.get({ uri: 'https://s3-eu-west-1.amazonaws.com/torpio-logos/torpio_wide_560.png', encoding: null }, function (error, response, body) {
if (error || response.statusCode !== 200) {
log.error(error, response, body);
} else {
// Gather some values that we'll use in our email templates
var values = {
name: 'Fred',
from: 'Me'
};
// Create our email templates
template = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head></head><body><img src="cid:logo" width="280" height="65"><p>Hi {{name}},<br><br>Here is a HTML email!<br><br>{{from}}</p></body></html>';
templateText = 'Hi {{name}},\n\nHere is the text alternative!\n\n{{from}}';
// Now we put our email object together
var data = {
text: helper.template(templateText, values), // Using the template helper to replace {{placeholders}}
from: 'Me <[email protected]>',
to: '[email protected]', // TODO: Change this!
subject: 'Sending you an HTML email from Torpio',
attachment: [
{
data: helper.template(template, values), // Using the template helper to replace {{placeholders}}
alternative: true
},
{
data: body.toString('base64'),
encoded: true,
inline: true,
name: 'my-image.png',
type: 'image/png',
headers: { 'Content-ID': '<logo>' } // Use <img src="cid:logo"> in the HTML
}
]
};
// All we have to do now is send!
service.post('email', 'email', data, function (error, status, result) {
if (error) {
log.error(error);
}
});
}
});
// Install with #torpio at https://torpio.com/app/scripts/shared/view/33508
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment