├── .gitignore ├── README.md ├── conf └── config.json ├── handler.js ├── package.json └── serverless.yml /.gitignore: -------------------------------------------------------------------------------- 1 | .serverless/ 2 | node_modules/ 3 | jspm_packages/ 4 | 5 | *~ 6 | #*# 7 | *.bak 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Serverless Form Mail 2 | 3 | [![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com) 4 | 5 | With this serverless app, you can add forms and receive answers by email. 6 | 7 | ## How to Deploy 8 | 9 | ``` 10 | $ git clone https://github.com/takahashim/sls-form-mail.git 11 | $ cd sls-form-mail 12 | $ npm install 13 | $ npm run deploy 14 | ``` 15 | 16 | ## How to Use 17 | 18 | (TBD) 19 | -------------------------------------------------------------------------------- /conf/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "mailTitle": "[form] お問い合わせがありました", 3 | "formKeys": ["name", "content"], 4 | "mailTo": "takahashimm@gmail.com", 5 | "topicName": "SlsFormMailTopic" 6 | } 7 | -------------------------------------------------------------------------------- /handler.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var AWS = require('aws-sdk'); 4 | var sns = new AWS.SNS(); 5 | var qs = require('qs'); 6 | var Entities = require('html-entities').XmlEntities; 7 | const entities = new Entities(); 8 | const conf = require('./conf/config.json'); 9 | 10 | const topicName = conf['topicName']; 11 | const formKeys = conf['formKeys']; 12 | const mailTitle = conf['mailTitle']; 13 | 14 | module.exports.send_mail = (event, context, callback) => { 15 | const functionArnCols = context.invokedFunctionArn.split(':'); 16 | const region = functionArnCols[3]; 17 | const accountId = functionArnCols[4]; 18 | const topicArn = 'arn:aws:sns:' + region + ':' + accountId + ':' + topicName; 19 | const done = (err, result) => callback(null, { 20 | statusCode: err ? '500' : '200', 21 | body: err ? err.message : JSON.stringify(result), 22 | headers: { 23 | 'Content-Type': 'application/json', 24 | }, 25 | }); 26 | var obj = qs.parse(event.body); 27 | var message = ""; 28 | formKeys.forEach(function(val, i) { 29 | message += val + ":" + entities.decode(""+obj[val]) + "\n"; 30 | }); 31 | message += "\n\n-----\n\n"; 32 | sns.publish({ 33 | Message: message, 34 | Subject: mailTitle, 35 | TopicArn: topicArn 36 | }, done); 37 | }; 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sls-form-mail", 3 | "version": "1.0.0", 4 | "description": "serverless form mail", 5 | "main": "handler.js", 6 | "scripts": { 7 | "sls": "serverless", 8 | "serverless": "serverless", 9 | "deploy": "serverless deploy", 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": "takahashim/sls-form-mail", 13 | "author": "takahashim ", 14 | "license": "MIT", 15 | "dependencies": { 16 | "html-entities": "^1.2.0", 17 | "qs": "^6.3.0", 18 | "serverless": "^1.1.0" 19 | }, 20 | "devDependencies": { 21 | "aws-sdk": "^2.6.15" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: sls-form-mail 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs4.3 6 | region: ap-northeast-1 7 | # stage: dev 8 | 9 | iamRoleStatements: 10 | - Effect: Allow 11 | Action: 12 | - "sns:Publish" 13 | Resource: "arn:aws:sns:*:*:${self:custom.conf.topicName}" 14 | 15 | functions: 16 | SendMail: 17 | handler: handler.send_mail 18 | events: 19 | - http: 20 | path: send 21 | method: post 22 | 23 | resources: 24 | Resources: 25 | MailQueue: 26 | Type: AWS::SNS::Topic 27 | Properties: 28 | DisplayName: "Serverlss Form Mail" 29 | TopicName: ${self:custom.conf.topicName} 30 | Subscription: 31 | - Endpoint: ${self:custom.conf.mailTo} 32 | Protocol: "email" 33 | 34 | custom: 35 | conf: ${file(conf/config.json)} --------------------------------------------------------------------------------