├── .gitignore ├── LICENSE ├── README.md ├── SendSesEmail.sln └── SendSesEmail ├── SendSesEmail.njsproj ├── Templates └── Template.html ├── _sampleEvent.json ├── _testdriver.js ├── config.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Eleven41 Software Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aws-lambda-send-ses-email 2 | 3 | An AWS Lambda function to send emails using Amazon SES. 4 | 5 | The primary purpose of this function is to provide a server-side back-end for sending emails 6 | from static websites. 7 | 8 | By using AWS Lambda, we can eliminate the need to host your (almost) static website on 9 | EC2 instances. 10 | 11 | ## Installation 12 | 13 | 1. Create an IAM Role for executing AWS Lambda functions. 14 | 2. Give your new IAM Role the following policy: 15 | 16 | ```json 17 | { 18 | "Version" : "2012-10-17", 19 | "Statement" : [ 20 | { 21 | "Effect" : "Allow", 22 | "Action" : [ 23 | "logs:CreateLogGroup", 24 | "logs:CreateLogStream", 25 | "logs:PutLogEvents" 26 | ], 27 | "Resource" : "arn:aws:logs:*:*:*" 28 | }, 29 | { 30 | "Effect" : "Allow", 31 | "Action" : [ 32 | "cloudwatch:PutMetricData" 33 | ], 34 | "Resource" : "*" 35 | }, 36 | { 37 | "Effect" : "Allow", 38 | "Action" : [ 39 | "ses:SendEmail" 40 | ], 41 | "Resource" : "*" 42 | }, 43 | { 44 | "Effect" : "Allow", 45 | "Action" : [ 46 | "s3:GetObject" 47 | ], 48 | "Resource" : "*" 49 | } 50 | ] 51 | } 52 | ``` 53 | 54 | 3. Create an S3 bucket to store the email template(s). 55 | 56 | 4. Create a template file (HTML or text file). A sample file is provided in the `Templates/` folder. 57 | 58 | 5. Upload the template file to your S3 bucket. 59 | 60 | 6. Download the latest release ZIP file. 61 | 62 | 7. Edit `config.js` providing your own details. Put your customized file back into the ZIP file. 63 | 64 | 8. Create an AWS Lambda function using your custom ZIP file as the source code. 65 | 66 | ## Usage 67 | 68 | Call the Lambda function with any number of properties. The properties will be passed on to the 69 | template file for substitution. 70 | 71 | ### Notable Parameters 72 | 73 | email: This parameter is required. It's used to populate the "Reply-To" field of the email. 74 | 75 | name: This parameter is optional, but when omitted, the value from `email` will be used. 76 | 77 | ### Example 1 78 | 79 | Template File: 80 | 81 | ``` 82 |

83 | Name: {{name}}
84 | Email: {{email}}
85 |

86 |

{{message}}

87 | ``` 88 | 89 | Input Parameters: 90 | 91 | ``` 92 | { 93 | "name": "John", 94 | "email": "john@example.com", 95 | "message": "This is my message" 96 | } 97 | ``` 98 | 99 | ## Credits 100 | 101 | The following libraries are used: 102 | 103 | * AWS SDK for NodeJS 104 | * Markup.js - https://github.com/adammark/Markup.js/ 105 | -------------------------------------------------------------------------------- /SendSesEmail.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.40629.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}") = "SendSesEmail", "SendSesEmail\SendSesEmail.njsproj", "{93A99353-34B5-47FA-87C9-63D274E18909}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {93A99353-34B5-47FA-87C9-63D274E18909}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {93A99353-34B5-47FA-87C9-63D274E18909}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {93A99353-34B5-47FA-87C9-63D274E18909}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {93A99353-34B5-47FA-87C9-63D274E18909}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /SendSesEmail/SendSesEmail.njsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 11.0 5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 6 | SendSesEmail 7 | SendSesEmail 8 | _testdriver.js 9 | 10 | 11 | Debug 12 | 2.0 13 | {99999999-9999-9999-9999-999999999999} 14 | 15 | 16 | 17 | Debug 18 | 2.0 19 | 93a99353-34b5-47fa-87c9-63d274e18909 20 | . 21 | False 22 | 23 | 24 | . 25 | . 26 | v4.0 27 | {3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD} 28 | ShowAllFiles 29 | false 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /SendSesEmail/Templates/Template.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

9 | Name: {{name}}
10 | Email: {{email}}
11 |

12 |

{{message}}

13 | 14 | -------------------------------------------------------------------------------- /SendSesEmail/_sampleEvent.json: -------------------------------------------------------------------------------- 1 | { 2 | "email": "customer@customer-domain.com", 3 | "name": "Customer Name", 4 | "message": "Hello World!\nGoing to the moon!" 5 | } -------------------------------------------------------------------------------- /SendSesEmail/_testdriver.js: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a utility file to help invoke and debug the lambda function. It is not included as part of the 3 | * bundle upload to Lambda. 4 | * 5 | * Credentials: 6 | * The AWS SDK for Node.js will look for credentials first in the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY and then 7 | * fall back to the shared credentials file. For further information about credentials read the AWS SDK for Node.js documentation 8 | * http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html#Credentials_from_the_Shared_Credentials_File_____aws_credentials_ 9 | * 10 | */ 11 | 12 | // Set the region to the locations of the S3 buckets 13 | process.env['AWS_REGION'] = 'us-east-1'; 14 | 15 | var fs = require('fs'); 16 | var app = require('./index'); 17 | 18 | // Load the sample event to be passed to Lambda. The _sampleEvent.json file can be modified to match 19 | // what you want Lambda to process on. 20 | var event = JSON.parse(fs.readFileSync('_sampleEvent.json', 'utf8').trim()); 21 | 22 | var context = {}; 23 | context.done = function () { 24 | console.log("Lambda function complete"); 25 | } 26 | context.succeed = function (msg) { 27 | console.log("Lambda function success: " + msg); 28 | } 29 | context.fail = function (msg) { 30 | console.log("Lambda function failed: " + msg); 31 | } 32 | 33 | app.handler(event, context); -------------------------------------------------------------------------------- /SendSesEmail/config.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var config = { 4 | "templateBucket" : "my-bucket", 5 | "templateKey" : "Templates/Template.html", 6 | "targetAddress" : "my-email@domain.com", 7 | "fromAddress": "Me ", 8 | "defaultSubject" : "Email From {{name}}" 9 | } 10 | 11 | module.exports = config 12 | -------------------------------------------------------------------------------- /SendSesEmail/index.js: -------------------------------------------------------------------------------- 1 | console.log('Version 0.1.3'); 2 | 3 | var aws = require('aws-sdk'); 4 | 5 | var ses = new aws.SES(); 6 | var s3 = new aws.S3(); 7 | 8 | exports.handler = function (event, context) { 9 | 10 | console.log("Event: " + JSON.stringify(event)); 11 | 12 | // Check required parameters 13 | if (event.email == null) { 14 | context.fail('Bad Request: Missing required member: email'); 15 | return; 16 | } 17 | 18 | var config = require('./config.js'); 19 | 20 | // Make sure some expected results are present 21 | if (event.name == null) { 22 | event.name = event.email; 23 | } 24 | 25 | // Make sure we have a subject. 26 | // If the event didn't include it, then 27 | // pull it from the configuration. 28 | // If we still don't have a subject, then 29 | // just make one up. 30 | if (event.subject == null) { 31 | event.subject = config.defaultSubject; 32 | 33 | if (event.subject == null) { 34 | event.subject = "Mail from {{name}}"; 35 | } 36 | } 37 | 38 | console.log('Loading template from ' + config.templateKey + ' in ' + config.templateBucket); 39 | 40 | // Read the template file 41 | s3.getObject({ 42 | Bucket: config.templateBucket, 43 | Key: config.templateKey 44 | }, function (err, data) { 45 | if (err) { 46 | // Error 47 | console.log(err, err.stack); 48 | context.fail('Internal Error: Failed to load template from s3.') 49 | } else { 50 | var templateBody = data.Body.toString(); 51 | console.log("Template Body: " + templateBody); 52 | 53 | // Convert newlines in the message 54 | if (event.message != null) { 55 | event.message = event.message 56 | .replace("\r\n", "
") 57 | .replace("\r", "
") 58 | .replace("\n", "
"); 59 | } 60 | 61 | // Perform the substitutions 62 | var mark = require('markup-js'); 63 | 64 | var subject = mark.up(event.subject, event); 65 | console.log("Final subject: " + subject); 66 | 67 | var message = mark.up(templateBody, event); 68 | console.log("Final message: " + message); 69 | 70 | var params = { 71 | Destination: { 72 | ToAddresses: [ 73 | config.targetAddress 74 | ] 75 | }, 76 | Message: { 77 | 78 | Subject: { 79 | Data: subject, 80 | Charset: 'UTF-8' 81 | } 82 | }, 83 | Source: config.fromAddress, 84 | ReplyToAddresses: [ 85 | event.name + '<' + event.email + '>' 86 | ] 87 | }; 88 | 89 | var fileExtension = config.templateKey.split(".").pop(); 90 | if (fileExtension.toLowerCase() == 'html') { 91 | params.Message.Body = { 92 | Html: { 93 | Data: message, 94 | Charset: 'UTF-8' 95 | } 96 | }; 97 | } else if (fileExtension.toLowerCase() == 'txt') { 98 | params.Message.Body = { 99 | Text: { 100 | Data: message, 101 | Charset: 'UTF-8' 102 | } 103 | }; 104 | } else { 105 | context.fail('Internal Error: Unrecognized template file extension: ' + fileExtension); 106 | return; 107 | } 108 | 109 | // Send the email 110 | ses.sendEmail(params, function (err, data) { 111 | if (err) { 112 | console.log(err, err.stack); 113 | context.fail('Internal Error: The email could not be sent.'); 114 | } else { 115 | console.log(data); // successful response 116 | context.succeed('The email was successfully sent to ' + event.email); 117 | } 118 | }); 119 | } 120 | }); 121 | }; 122 | -------------------------------------------------------------------------------- /SendSesEmail/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-lambda-send-ses-email", 3 | "version": "0.1.4", 4 | "description": "AWS Lambda function to send email through Amazon SES.", 5 | 6 | "homepage": "https://github.com/eleven41/aws-lambda-send-ses-email", 7 | "author": { 8 | "name": "Matt Houser" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/eleven41/aws-lambda-send-ses-email.git" 13 | }, 14 | 15 | "dependencies": { 16 | "aws-sdk": ">=2.1.8", 17 | "markup-js": "1.5.21" 18 | } 19 | } --------------------------------------------------------------------------------