@rheactorjs/template-mailer-aws-lambda

1.0.2 • Public • Published

template-mailer-aws-lambda

npm version Build Status Greenkeeper badge js-standard-style semantic-release Code Climate

A transactional email mailer that runs on AWS Lambda and sends emails via AWS SES.

Setup

IAM

Create a role for the lambda function in IAM and attach these policies:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1459942470000",
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "arn:aws:s3:::template-mailer/*"
            ]
        }
    ]
}

*Replace template-mailer with your bucket name.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1475239732000",
            "Effect": "Allow",
            "Action": [
                "ses:SendEmail",
                "ses:SendRawEmail"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

S3

Create the S3 bucket used by the mailer.

Lambda

Copy config.json.dist to config.json and adapt to your needs.

Run

ROLE="…" make deploy

to publish the lambda function.

You can override the default function name with the environment variable FUNCTION_NAME.

You might also be interested in setting these environment variables for the aws CLI:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_DEFAULT_REGION

API Gateway

Then setup an API Gateway Lamdba Proxy.

Init configuration

Create the template foo:

curl -v -X PUT https://XXXX.execute-api.YYYY.amazonaws.com/production/templates/foo \
-H 'X-API-Key: ????' \
-H 'Content-Type: application/vnd.rheactorjs.template-mailer.v2+json; charset=utf-8' \
--data '{"subject":"Mail for <%= name %>","html":"Hello <%= name %>"}'

Create the transport bar:

curl -v -X PUT https://XXXX.execute-api.YYYY.amazonaws.com/production/transport/bar \
-H 'X-API-Key: ????' \
-H 'Content-Type: application/vnd.rheactorjs.template-mailer.v2+json; charset=utf-8' \
--data '{"email":"info@example.com","name":"Example Inc."}'

Note: In order to use the email, you need to configure it in AWS SES.

Send an email using the transport bar and the template foo:

curl -v -X POST https://XXXX.execute-api.YYYY.amazonaws.com/production/send/bar/foo \
-H 'X-API-Key: ????' \
-H 'Content-Type: application/vnd.rheactorjs.template-mailer.v2+json; charset=utf-8' \
--data '{"to":"john.doe@example.com","name":"John Doe"}'

Templates

The subject, html and text part of the template will be parsed through lodash's template function with the data provided in the body of this request.

You can provide the data with a formatter hint:

"subject": {
  "@markdown": "This will be parsed with *markdown*"
}

it will parsed into

"subject": {
  "@text": "This will be parsed with *markdown*",                // the original value
  "@html": "This will be parsed with <strong>markdown</strong>"  // the HTML result
}

and can be access in the template accordingly.

Package Sidebar

Install

npm i @rheactorjs/template-mailer-aws-lambda

Weekly Downloads

3

Version

1.0.2

License

MIT

Last publish

Collaborators

  • coderbyheart