├── .gitignore ├── README.md ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lambda function to send emails with attachments 2 | An AWS Lambda function that sends attachments hosted on S3 by email with SES 3 | 4 | # How to use 5 | 6 | 1. Run `npm install` to install dependencies. 7 | 2. Compress the project directory and upload as a zip to a new (or existing) Lambda function. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var aws = require('aws-sdk'); 2 | var nodemailer = require('nodemailer'); 3 | 4 | var ses = new aws.SES(); 5 | var s3 = new aws.S3(); 6 | 7 | function getS3File(bucket, key) { 8 | return new Promise(function (resolve, reject) { 9 | s3.getObject( 10 | { 11 | Bucket: bucket, 12 | Key: key 13 | }, 14 | function (err, data) { 15 | if (err) return reject(err); 16 | else return resolve(data); 17 | } 18 | ); 19 | }) 20 | } 21 | 22 | exports.handler = function (event, context, callback) { 23 | 24 | getS3File('xoor-email-attachments', 'attachment.pdf') 25 | .then(function (fileData) { 26 | var mailOptions = { 27 | from: 'mauricio@xoor.io', 28 | subject: 'This is an email sent from a Lambda function!', 29 | html: `
You got a contact message from: ${event.emailAddress}
`, 30 | to: 'mauricio@xoor.io', 31 | // bcc: Any BCC address you want here in an array, 32 | attachments: [ 33 | { 34 | filename: "An Attachment.pdf", 35 | content: fileData.Body 36 | } 37 | ] 38 | }; 39 | 40 | console.log('Creating SES transporter'); 41 | // create Nodemailer SES transporter 42 | var transporter = nodemailer.createTransport({ 43 | SES: ses 44 | }); 45 | 46 | // send email 47 | transporter.sendMail(mailOptions, function (err, info) { 48 | if (err) { 49 | console.log(err); 50 | console.log('Error sending email'); 51 | callback(err); 52 | } else { 53 | console.log('Email sent successfully'); 54 | callback(); 55 | } 56 | }); 57 | }) 58 | .catch(function (error) { 59 | console.log(error); 60 | console.log('Error getting attachment from S3'); 61 | callback(err); 62 | }); 63 | }; 64 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lambda-send-attachment", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "nodemailer": { 8 | "version": "4.6.3", 9 | "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-4.6.3.tgz", 10 | "integrity": "sha512-1AmOpDZJtyPAO+gfUBfT+MWHbYwQ+DZvb1gvYaTxBZV/lUeysZIt4kDq8Dlwt6ViUZGp3cMGR+D1MNQYyYiVUg==" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "lambda-send-attachment", 3 | "version": "1.0.0", 4 | "description": "An AWS Lambda function that sends attachments hosted on S3 by email with SES", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/xoor-io/lambda-send-attachment.git" 12 | }, 13 | "author": "XOOR (hello@xoor.io)", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/xoor-io/lambda-send-attachment/issues" 17 | }, 18 | "homepage": "https://github.com/xoor-io/lambda-send-attachment#readme", 19 | "dependencies": { 20 | "nodemailer": "^4.6.3" 21 | } 22 | } 23 | --------------------------------------------------------------------------------