Giter Club home page Giter Club logo

lambda-ses-mailer's Introduction

AWS SES Lambda

The purpose of this lambda function is to receive structured payloads from an Amazon Kinesis stream, retrieve the corresponding templates from S3, render the template(s) using Mustache.js and then use SES to send the email.

All templates and subject lines for emails are stored in an S3 bucket and the function itself is configuration-less. The function expects to receive Kinesis payloads similar to the following:

{
  "Configuration": {
    "Bucket": "my-email-templates-bucket"
  },
  "Email" : {
    "Properties": {
      "TemplateKey" : "templates/en/welcome",
      "Partials" : "partials/en",
      "Data" : {
        "name" : "Jeremy"
      }
    },
    "Payload": {
      "Destination": {
        "BccAddresses": [],
        "CcAddresses": [],
        "ToAddresses": ["[email protected]"]
      },
      "Source": "[email protected]",
      "ReplyToAddresses": ["[email protected]"]
    }
  }
}

Templates & Partials

The TemplateKey field determines where the function will attempt to retrieve the template files from inside the configured S3 bucket (Configuration.Bucket), in-order to construct the email's body and subject line.

Using the example payload above, the function would expect the following files to be present:

s3://my-email-templates-bucket/templates/en/welcome.subj
s3://my-email-templates-bucket/templates/en/welcome.html
s3://my-email-templates-bucket/templates/en/welcome.txt

The Partials key indicates where partials are to be located. In the above example, let us assume that a partial of footer exists.

s3://my-email-templates-bucket/partials/en/footer.html
s3://my-email-templates-bucket/partials/en/footer.txt

Note: The .html or .txt versions of emails are optional. However, the .subj is required.

  1. welcome.subj - Determines the subject line for the email.
  2. welcome.html - The HTML content of the email.
  3. welcome.txt - The plain text content of the email.
  4. footer.html - The footer partial in html format.
  5. footer.txt - The footer partial in txt format.

All files retrieved from S3 are rendered using Mustache, with the Email.Properties.Data object passed into the renderer. Therefore, if welcome.subj contained the following content:

Welcome to my website, {{name}}

Using the example above, it would be rendered as "Welcome to my website, Jeremy". If you need more help with what mustache templates are capable of doing, be sure to checkout the documentation.

Partials

These may help with your templates if you have lots of emails that all require common content. If a partial is defined, it is required to be present in .html, format if a .html template version is present, likewise for .txt.

Note: All partials located under the path are loaded at runtime, although their use is optionally required.

Given the example payload above, you can incorporate partials into your templates as follows:

welcome.html
<html>
<body>
  <h1>Hello {{ name }}!</h1>
  {{> footer}}
</body>
</html>
footer.html (partial)
<p>Goodbye {{ name }}</p>
Rendered email
<html>
<body>
  <h1>Hello Jeremy!</h1>
  <p>Goodbye Jeremy</p>
</body>
</html>

Payload

The Payload key is the base object passed into the AWS SES sendEmail call. It corresponds to the documented request payload here. This makes it easy to pass in additional keys for your implementation, such as ReturnPath or SourceArn. The only object that is injected in is Message, which is generated by the function itself.

lambda-ses-mailer's People

Contributors

stratmm avatar davidkelley avatar

Stargazers

Vivek Kushwaha avatar SDF avatar Cam Jackson avatar Omar avatar

Watchers

James Cloos avatar Shane Leonard avatar  avatar  avatar Jonathan Kelley avatar

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.