Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adnanrahic/lambda-mailer
Simple module for receiving an email from a contact form on your website.
https://github.com/adnanrahic/lambda-mailer
aws aws-lambda aws-ses aws-ses-transport no-dependencies node-module nodejs serverless serverless-framework
Last synced: 14 days ago
JSON representation
Simple module for receiving an email from a contact form on your website.
- Host: GitHub
- URL: https://github.com/adnanrahic/lambda-mailer
- Owner: adnanrahic
- License: mit
- Created: 2018-07-14T21:14:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-21T16:05:38.000Z (over 6 years ago)
- Last Synced: 2024-10-20T14:08:00.769Z (22 days ago)
- Topics: aws, aws-lambda, aws-ses, aws-ses-transport, no-dependencies, node-module, nodejs, serverless, serverless-framework
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/lambda-mailer
- Size: 42 KB
- Stars: 82
- Watchers: 6
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
![dependencies](https://img.shields.io/badge/dependencies-0-brightgreen.svg)
![contributors](https://img.shields.io/badge/contributors-1-blue.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![license](https://img.shields.io/badge/eslint-standard-yellowgreen.svg)# Lambda Mailer
Simple module for receiving an email from a contact form on your website.# Note!
**Module needs Node.js version 8 or above.**## Usage
Configuration is rather simple.#### 1. Enable your email address in the AWS console -> Simple Email Service
#### 2. Install the module
```bash
$ npm i lambda-mailer
```
#### 3. `require()` it in your `handler.js`
```js
// define the options for your email and domain
const options = {
myEmail: process.env.EMAIL, // myEmail is the email address you enabled in AWS SES in the AWS Console
myDomain: process.env.DOMAIN // add the domain of your website or '*' if you want to accept requests from any domain
}// initialize the function
const { sendJSON, sendFormEncoded } = require('lambda-mailer')(options)// Content-Type: application/json
// The event.body needs to be a JSON object with 3 properties
// - name
// - content
module.exports.sendJSON = sendJSON// Content-Type: application/x-www-form-urlencoded
// The event.body needs to a URI encoded string with 3 parameters
// - name
// - content
module.exports.sendFormEncoded = sendFormEncoded```
#### 4. Hook it up to API Gateway in the `serverless.yml`
```yaml
service: lambda-mailercustom:
secrets: ${file(secrets.json)}provider:
name: aws
runtime: nodejs8.10
stage: ${self:custom.secrets.NODE_ENV}
region: us-east-1
profile: ${self:custom.secrets.PROFILE}
environment:
NODE_ENV: ${self:custom.secrets.NODE_ENV}
EMAIL: ${self:custom.secrets.EMAIL}
DOMAIN: ${self:custom.secrets.DOMAIN}
iamRoleStatements:
- Effect: "Allow"
Action:
- "ses:SendEmail"
Resource: "*"functions:
sendJSON:
handler: handler.sendJSON
events:
- http:
path: email/send/json
method: post
cors: true
sendFormEncoded:
handler: handler.sendFormEncoded
events:
- http:
path: email/send/formencoded
method: post
cors: true
```#### 5. Send an HTTP request to the API Gateway endpoint
Send a POST request of the type JSON with the body as stated below. The sample below is with CURL.
- request method: **POST**
- request body type: **application/json**
- request body: **{ email: String, name: String, content: String }**```bash
curl --header "Content-Type: application/json" \
--request POST \
--data '{"email":"[email protected]","name":"John Doe","content":"I need some help!"}' \
https://{id}.execute-api.{region}.amazonaws.com/{stage}/email/send/json
```#### 6. Send a regular form POST request to the API Gateway endpoint
Send a POST request using an HTML form. Sample below shows a simple HTML form.
```html
```
By default the request will redirect back to the initial page the request was sent from. You can edit the `redirectUrl` by adding it as a query string parameter. Example below.
```html
```
---
Enjoy using the module. Feel free to open issues or feature requests. :smile: