Socket
Book a DemoInstallSign in
Socket

nuxt-mailchannels

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nuxt-mailchannels

MailChannels Email API integration for Nuxt

0.3.3
latest
Source
npmnpm
Version published
Weekly downloads
48
-7.69%
Maintainers
1
Weekly downloads
 
Created
Source

nuxt-mailchannels

Nuxt MailChannels

npm version npm downloads License Nuxt Modules

Simple MailChannels Email Send API integration for Nuxt.

Leverages mailchannels-sdk package, a MailChannels SDK for Node.js.

Contents

Features

  • Send emails using MailChannels Email Send API
  • Works on the edge
  • Exposed server utils
  • Email DKIM signing
  • Default global settings
  • Supports mustache templates
  • Text and HTML content types

Requirements

  • Nuxt 3 or higher
  • MailChannels account and Email API key

[!WARNING] This module only works with a Nuxt server running as it uses utils for server API routes (nuxt build). This means that you cannot use this module with nuxt generate.

Quick setup

  • Add nuxt-mailchannels dependency to your project
npx nuxi@latest module add mailchannels
  • Add the module in your nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    'nuxt-mailchannels'
  ],
})

Configuration

You can add the MailChannels API key and DKIM settings to the runtimeConfig property in nuxt.config.ts file.

If you want to use default global settings for all your email transactions, you can set it in the mailchannels app options property in your nuxt.config.ts file.

export default defineNuxtConfig({
  // Runtime config
  runtimeConfig: {
    mailchannels: {
      apiKey: '',
      dkim: {
        domain: '',
        privateKey: '',
        selector: '',
      },
    },
  },
  // Set the default settings for all your email transactions (optional)
  mailchannels: {
    bcc: { email: '', name: '' },
    cc: { email: '', name: '' },
    from: { email: '', name: '' },
    to: { email: '', name: '' },
  },
})

[!NOTE] bcc, cc, and to can be an object with email and name properties or a single email address string or an array of them.

Use the environment variables to set your API key, DKIM settings and default global settings.

# Runtime config
NUXT_MAILCHANNELS_API_KEY=

NUXT_MAILCHANNELS_DKIM_DOMAIN=
NUXT_MAILCHANNELS_DKIM_PRIVATE_KEY=
NUXT_MAILCHANNELS_DKIM_SELECTOR=

# App config (optional)

# NUXT_MAILCHANNELS_BCC=
NUXT_MAILCHANNELS_BCC_EMAIL=
NUXT_MAILCHANNELS_BCC_NAME=

# NUXT_MAILCHANNELS_CC=
NUXT_MAILCHANNELS_CC_EMAIL=
NUXT_MAILCHANNELS_CC_NAME=

# NUXT_MAILCHANNELS_FROM=
NUXT_MAILCHANNELS_FROM_EMAIL=
NUXT_MAILCHANNELS_FROM_NAME=

# NUXT_MAILCHANNELS_TO=
NUXT_MAILCHANNELS_TO_EMAIL=
NUXT_MAILCHANNELS_TO_NAME=

Server utils

The following helpers are auto-imported in your server/ directory.

// initialize a MailChannels instance
const mailchannels = useMailChannels(event)

TypeScript signature

const useMailChannels: (event?: H3Event) => {
  send: (options: MailChannelsEmailOptions, dryRun?: boolean) => Promise<{
    success: boolean,
    data: string[] | undefined,
  }>
}

Simple usage

const mailchannels = useMailChannels(event)
await mailchannels.send({
  from: {
    email: 'from@example.com',
    name: 'Example 2'
  },
  to: {
    email: 'to@example.com',
    name: 'Example 1'
  },
  subject: 'Your subject',
  html: '<p>Your email content</p>',
  text: 'Your email content',
})

Send method

The send method sends an email using the MailChannels API.

[!IMPORTANT] If you set the bcc, cc, from, to properties in the send method, they will override the default global settings set in the nuxt.config.ts or .env file.

Arguments

ArgumentTypeDescriptionRequired
optionsOptionsThe email options to send
dryRunbooleanWhen set to true, the message will not be sent. Instead, the fully rendered message will be returned in the data property of the response. The default value is false.

Options

Available options for the send method.

PropertyDescriptionRequired
attachmentsAn array of attachments to add to the email. Each attachment should be an object with filename, content, and type properties.
bccThe BCC recipients of the email. Can be an object with email and name properties or a single email address string or an array of them.
ccThe CC recipients of the email. Can be an object with email and name properties or a single email address string or an array of them.
fromThe sender of the email. Can be a string or an object with email and name properties. Required when the default global sender is not set.🟡
toThe recipient of the email. Can be an object with email and name properties or a single email address string or an array of them. Required when the default global recipient is not set.🟡
replyToThe email address to reply to. Can be a string or an object with email and name properties.
subjectThe subject of the email.
htmlThe HTML content of the email. Required if text is not set.🟡
textThe plain text content of the email. Required if html is not set.🟡
mustachesData to be used if the email is a mustache template, key-value pairs of variables to set for template rendering. Keys must be strings.

[!TIP] Including a plain text version of your email ensures that all recipients can read your message, including those with email clients that lack HTML support.

You can use the html-to-text package to convert your HTML content to plain text.

Response

The send method returns a promise that resolves to an object with the following properties.

PropertyTypeDescription
successbooleanIndicates the success or failure of the email sending operation.
datastring[] or undefinedThe fully rendered message if the dryRun argument is set to true.

Examples

Use the send method inside your API routes to send emails.

The recipient parameters can be either an email address string or an object with email and name properties.

This is the best way to add names to the recipients.

export default defineEventHandler(async (event) => {
  const mailchannels = useMailChannels(event)
  const { success } = await mailchannels.send({
    from: {
      email: 'from@example.com',
      name: 'Example 2'
    },
    to: {
      email: 'to@example.com',
      name: 'Example 1'
    },
    subject: 'Your subject',
    html: '<p>Your email content</p>',
    text: 'Your email content',
  })
  return { success }
})

Using string recipients

This is the simplest way to send an email.

export default defineEventHandler(async (event) => {
  const mailchannels = useMailChannels(event)
  const { success } = await mailchannels.send({
    from: 'from@example.com',
    to: 'to@example.com',
    subject: 'Your subject',
    html: '<p>Your email content</p>',
    text: 'Your email content',
  })
  return { success }
})

Array of recipients

You can also send an email to multiple recipients.

export default defineEventHandler(async (event) => {
  const mailchannels = useMailChannels(event)
  const { success } = await mailchannels.send({
    from: {
      email: 'from@example.com',
      name: 'Example 3'
    },
    to: [
      {
        email: 'to1@example.com',
        name: 'Example 1'
      },
      {
        email: 'to2@example.com',
        name: 'Example 2'
      }
    ],
    subject: 'Your subject',
    html: '<p>Your email content</p>',
    text: 'Your email content',
  })
  return { success }
})

or

export default defineEventHandler(async (event) => {
  const mailchannels = useMailChannels(event)
  const { success } = await mailchannels.send({
    from: 'from@example.com',
    to: ['to1@example.com', 'to2@example.com'],
    subject: 'Your subject',
    html: '<p>Your email content</p>',
    text: 'Your email content',
  })
  return { success }
})

Using mustache templates

You can use the mustaches property to render mustache templates.

export default defineEventHandler(async (event) => {
  const mailchannels = useMailChannels(event)
  const { success } = await mailchannels.send({
    from: 'from@example.com',
    to: 'to@example.com',
    subject: 'Mustaches test',
    html: '<p>Hello {{ world }}</p>',
    text: 'Hello {{ world }}',
    mustaches: {
      world: 'World',
    },
  })

  return { success }
})

Dry run

You can set the dryRun argument to test your email without sending it. It will return the fully rendered message in the data property of the response.

export default defineEventHandler(async (event) => {
  const mailchannels = useMailChannels(event)
  const response = await mailchannels.send({
    from: 'from@example.com',
    to: 'to@example.com',
    subject: 'Test',
    html: '<p>Test</p>',
    text: 'Test',
  }, true) // <-- `true` = dryRun enabled

  return response
})

Name-address pairs

You can use name-address pairs string format.

export default defineEventHandler(async (event) => {
  const mailchannels = useMailChannels(event)
  const { success } = await mailchannels.send({
    from: 'Sender Name <sender@example.com>',
    to: 'Recipient Name <recipient@example.com>',
    subject: 'Your subject',
    html: '<p>Your email content</p>',
    text: 'Your email content',
  })

  return { success }
})

Contribution

Local development
# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Run typecheck
npm run test:types

# Release new version
npm run release

Keywords

nuxt

FAQs

Package last updated on 06 Aug 2025

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.