Socket
Socket
Sign inDemoInstall

@netlify/plugin-emails

Package Overview
Dependencies
50
Maintainers
19
Versions
41
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @netlify/plugin-emails

A build plugin that creates an email handler and processes requests to send emails


Version published
Weekly downloads
531
decreased by-5.18%
Maintainers
19
Created
Weekly downloads
 

Readme

Source

Netlify Emails Plugin

🚧 Note: This plugin is pre-release software. Until version 1.0.0 is released, its API could change at any time.

The Netlify emails build plugin which is responsible for creating a serverless function to handle email requests, populating them with the provided templates and sending them using the specified email API provider.

Step 1: Enabling the plugin

Either add it to the netlify.toml as follows:

[[plugins]]
  package = "@netlify/emails-plugin"

Or via the Netlify app under Site Settings.

Step 2: Configuration

The following environment variables are required in order for the emails function to handle requests:

Variable NameDescriptionRequired
NETLIFY_EMAILS_PROVIDER"mailgun" | "sendgrid" | "postmark"Yes
NETLIFY_EMAILS_SECRETThe unique secret used to authenticate a request is genuineYes
NETLIFY_EMAILS_PROVIDER_API_KEYThe API key issued by the email providerYes
NETLIFY_EMAILS_MAILGUN_DOMAINIf the provider is set to Mailgun, the domain must be setNo
NETLIFY_EMAILS_DIRECTORY_OVERRIDEIf set, this will override the default directory ./emails when looking for templatesNo

Step 3: Adding Templates

Now that the setup is complete, you can add templates to your email directory. Each email template should be stored under a folder name that represents the route of your template and the email file should be named index.html. E.g. ./emails/welcome/index.html.

If there are variables that need replacing in your email template when the email is triggered, please use the handlebars.js syntax and pass the arguments in the request as shown in Step 4 below.

Step 4: Triggering an Email

You can now trigger the email handler by calling the following endpoint

With curl:

curl -X POST \
  '{process.env.URL}/.netlify/functions/emails/welcome' \
  --header 'netlify-emails-secret: process.env.NETLIFY_EMAILS_SECRET' \
  --header 'Content-Type: application/json' \
  --data-raw '{
  "from": "no-reply@yourdomain.com",
  "to": "alexanderhamilton@test.com",
  "subject": "Welcome",
  "parameters": {
    "products": ["product1", "product2", "product3"]
    "name": "Alexander",
  }
}'

With node-fetch:

 import fetch from 'node-fetch'

 await fetch(
    `${process.env.URL}/.netlify/functions/emails/confirm`,
    {
      headers: {
        "netlify-emails-secret": process.env.NETLIFY_EMAILS_SECRET,
      },
      method: "POST",
      body: JSON.stringify({
        from: "no-reply@yourdomain.com",
        to: "alexanderhamilton@test.com",
        subject: "Welcome",
        parameters: {
          products: ["product1", "product2", "product3"]
          name: "Alexander",
        },
      }),
    }
  );

You can also trigger the email locally by running netlify build, then netlify dev and making the above request.

Step 5: Previewing emails locally

Visit http://localhost:{PORT}/.netlify/functions/emails/_preview to preview your email templates.

Please note, this preview endpoint is not made available in production and is only made available locally or when viewing a deploy preview.

Keywords

FAQs

Last updated on 18 Oct 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc