├── .gitignore ├── README.md ├── app.js ├── config-sample.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | config.json 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AWS SES NodeJS Example 2 | 3 | Follow the instructions below on an EC2 Ubuntu instance. This tutorial will help you setup the AWS SDK using NodeJS. There are a few endpoints to send email with attachments, create a verified sender, delete a verified sender, and list all of your verified senders. If you have any questions please contact me! 4 | 5 | ``` 6 | sudo su 7 | apt-get update 8 | apt-get upgrade -y 9 | apt-get dist-upgrade -y 10 | apt-get autoremove -y 11 | apt-get install nodejs npm git -y 12 | ln -s /usr/bin/nodejs /usr/bin/node 13 | git clone https://github.com/andrewpuch/aws-ses-node-js-examples.git 14 | cd aws-ses-node-js-examples 15 | npm install 16 | cp config-sample.json config.json 17 | ``` 18 | 19 | ***NOTE: Here you will want to edit config.json with your AWS keys and in app.js you will want to edit the email address with your own email address. For this tutorial we will just send email to ourselves.*** 20 | 21 | ``` 22 | node app.js 23 | ``` 24 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // Require objects. 2 | var express = require('express'); 3 | var app = express(); 4 | var aws = require('aws-sdk'); 5 | 6 | // Edit this with YOUR email address. 7 | var email = "hello@example.com"; 8 | 9 | // Load your AWS credentials and try to instantiate the object. 10 | aws.config.loadFromPath(__dirname + '/config.json'); 11 | 12 | // Instantiate SES. 13 | var ses = new aws.SES(); 14 | 15 | // Verify email addresses. 16 | app.get('/verify', function (req, res) { 17 | var params = { 18 | EmailAddress: email 19 | }; 20 | 21 | ses.verifyEmailAddress(params, function(err, data) { 22 | if(err) { 23 | res.send(err); 24 | } 25 | else { 26 | res.send(data); 27 | } 28 | }); 29 | }); 30 | 31 | // Listing the verified email addresses. 32 | app.get('/list', function (req, res) { 33 | ses.listVerifiedEmailAddresses(function(err, data) { 34 | if(err) { 35 | res.send(err); 36 | } 37 | else { 38 | res.send(data); 39 | } 40 | }); 41 | }); 42 | 43 | // Deleting verified email addresses. 44 | app.get('/delete', function (req, res) { 45 | var params = { 46 | EmailAddress: email 47 | }; 48 | 49 | ses.deleteVerifiedEmailAddress(params, function(err, data) { 50 | if(err) { 51 | res.send(err); 52 | } 53 | else { 54 | res.send(data); 55 | } 56 | }); 57 | }); 58 | 59 | // Sending RAW email including an attachment. 60 | app.get('/send', function (req, res) { 61 | var ses_mail = "From: 'AWS Tutorial Series' <" + email + ">\n"; 62 | ses_mail = ses_mail + "To: " + email + "\n"; 63 | ses_mail = ses_mail + "Subject: AWS SES Attachment Example\n"; 64 | ses_mail = ses_mail + "MIME-Version: 1.0\n"; 65 | ses_mail = ses_mail + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"; 66 | ses_mail = ses_mail + "--NextPart\n"; 67 | ses_mail = ses_mail + "Content-Type: text/html; charset=us-ascii\n\n"; 68 | ses_mail = ses_mail + "This is the body of the email.\n\n"; 69 | ses_mail = ses_mail + "--NextPart\n"; 70 | ses_mail = ses_mail + "Content-Type: text/plain;\n"; 71 | ses_mail = ses_mail + "Content-Disposition: attachment; filename=\"attachment.txt\"\n\n"; 72 | ses_mail = ses_mail + "AWS Tutorial Series - Really cool file attachment!" + "\n\n"; 73 | ses_mail = ses_mail + "--NextPart--"; 74 | 75 | var params = { 76 | RawMessage: { Data: new Buffer(ses_mail) }, 77 | Destinations: [ email ], 78 | Source: "'AWS Tutorial Series' <" + email + ">'" 79 | }; 80 | 81 | ses.sendRawEmail(params, function(err, data) { 82 | if(err) { 83 | res.send(err); 84 | } 85 | else { 86 | res.send(data); 87 | } 88 | }); 89 | }); 90 | 91 | // Start server. 92 | var server = app.listen(80, function () { 93 | var host = server.address().address; 94 | var port = server.address().port; 95 | 96 | console.log('AWS SES example app listening at http://%s:%s', host, port); 97 | }); 98 | -------------------------------------------------------------------------------- /config-sample.json: -------------------------------------------------------------------------------- 1 | { 2 | "accessKeyId": "ACCESS_KEY", 3 | "secretAccessKey": "SECRET_KEY", 4 | "region": "us-east-1" 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-ses-tutorial", 3 | "version": "0.0.0", 4 | "private": true, 5 | "dependencies": { 6 | "express": "~4.12.4", 7 | "aws-sdk": "~2.1.35" 8 | } 9 | } 10 | --------------------------------------------------------------------------------