Skip to content

Nuxt

Send emails using Nuxt and the MailChannels Node.js SDK.

Prerequisites

1. Install

Add the mailchannels-sdk package dependency to your Nuxt project.

sh
npm i mailchannels-sdk
sh
yarn add mailchannels-sdk
sh
pnpm add mailchannels-sdk
sh
bun add mailchannels-sdk
sh
deno add npm:mailchannels-sdk

2. Configure your API key

Add your MailChannels API key to your .env file.

.env
sh
NUXT_MAILCHANNELS_API_KEY=your-api-key

Then, add the mailchannels object and apiKey property to the runtimeConfig in your nuxt.config.ts. The value of apiKey should be an empty string, which will be automatically set at runtime using the value of process.env.NUXT_MAILCHANNELS_API_KEY.

nuxt.config.ts
ts
export default defineNuxtConfig({
  runtimeConfig: {
    mailchannels: {
      apiKey: '' // This should be an empty string here
    }
  }
})

3. Send email using HTML

Register a Server handler under server/api/send.post.ts.

Use the html property to send an email with HTML content.

server/api/send.post.ts
ts
import { MailChannels } from 'mailchannels-sdk'

export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig(event)

  const mailchannels = new MailChannels(config.mailchannels.apiKey)

  const { data, error } = await mailchannels.emails.send({
    from: 'Name <from@example.com>',
    to: 'to@example.com',
    subject: 'Test email',
    html: '<p>Hello World</p>'
  })

  if (error) {
    throw createError({
      status: error.statusCode || 400,
      message: error.message
    })
  }

  return data
})

Examples

Released under the MIT License.