Skip to content

Instantly share code, notes, and snippets.

@uhlenbrock
Last active February 7, 2019 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uhlenbrock/67ab81e704f7a2175d3a51020d050cdf to your computer and use it in GitHub Desktop.
Save uhlenbrock/67ab81e704f7a2175d3a51020d050cdf to your computer and use it in GitHub Desktop.
Use Lambda & S3 to save a copy of all sent email via Braze
Webhook URL: https://<api_gateway_url>/prod/uploadEmail?user_id={{${user_id}}}&template_name=<template_name>
Request Body: (select raw text), paste in entire email HTML
'use strict';
const uploadService = require('./service');
exports.handler = (event, context, callback) => {
const filename = uploadService.buildFilename(event.queryStringParameters);
uploadService.saveEmailToS3(event.body, filename);
const response = {
statusCode: 200,
body: JSON.stringify({ "message": 'Email ' + filename + ' saved!'})
};
callback(null, response);
};
'use strict';
const AWS = require('aws-sdk');
function buildFilename(params) {
const now = Date.now();
return params.user_id + "/" + now + "_" + params.template_name + ".html";
}
function saveEmailToS3(data, filename) {
AWS.config.update({ accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_KEY });
const s3 = new AWS.S3();
console.log("Start uploading file to S3");
const param = { Bucket: env.BUCKET, Key: filename, Body: data };
s3.putObject(param, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log('Successfully uploaded data');
}
});
}
module.exports = {
saveEmailToS3: saveEmailToS3,
buildFilename: buildFilename
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment