GithubHelp home page GithubHelp logo

adnanrahic / lambda-mailer Goto Github PK

View Code? Open in Web Editor NEW
83.0 6.0 11.0 43 KB

Simple module for receiving an email from a contact form on your website.

Home Page: https://www.npmjs.com/package/lambda-mailer

License: MIT License

JavaScript 100.00%
aws aws-lambda aws-ses aws-ses-transport nodejs no-dependencies node-module serverless serverless-framework

lambda-mailer's Introduction

dependencies contributors License: MIT license

cover

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

$ npm i lambda-mailer

3. require() it in your handler.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
// - email
// - 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
// - email
// - name
// - content
module.exports.sendFormEncoded = sendFormEncoded

4. Hook it up to API Gateway in the serverless.yml

service: lambda-mailer

custom:
  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 }
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.

<form action="https://{id}.execute-api.{region}.amazonaws.com/{stage}/dev/email/send/formencoded" method="POST">
  <input type="text" name="name" required>
  <input type="email" name="email" required>
  <textarea name="content" required></textarea>
</form>

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.

<form action="https://{id}.execute-api.{region}.amazonaws.com/{stage}/dev/email/send/formencoded?redirectUrl=https://someotherdomain.com" method="POST">
  <input type="text" name="name" required>
  <input type="email" name="email" required>
  <textarea name="content" required></textarea>
</form>

Enjoy using the module. Feel free to open issues or feature requests. ๐Ÿ˜„

lambda-mailer's People

Contributors

adnanrahic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

lambda-mailer's Issues

Refactor to work with default form POST

Current setup requires the payload to be a JSON object with three fixed params. Create a version/refactor so it works with accepting a regular form POST request without having to use any JavaScript on the front end.

How to add Google recaptcha ?

Hi,

First of all I want to thank you for your lambda-mailer repo because I now able to quicly create multiple serverless forms, even if I'm not a developper.

But now I'm stuck with adding Google recaptcha.

Could you help me (and others I guess) by adding recaptcha handling instructions ?

Thanks in advance.

Best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.