UNPKG

nuxt-mailchannels

Version:

MailChannels Email API integration for Nuxt

78 lines (77 loc) 2.98 kB
import { MailChannelsClient, Emails } from "mailchannels-sdk"; import { createError } from "h3"; import { overrideRecipient } from "../utils/helpers.js"; import { useRuntimeConfig } from "#imports"; import defu from "defu"; export const useMailChannels = (event) => { const config = useRuntimeConfig(event).mailchannels; if (!config.apiKey) { throw createError({ status: 500, message: "Missing MailChannels API key" }); } const mailchannels = new MailChannelsClient(config.apiKey); const emails = new Emails(mailchannels); const getOverrides = (options) => ({ ...options, to: overrideRecipient(config.to, options.to), from: overrideRecipient(config.from, options.from), cc: overrideRecipient(config.cc, options.cc), bcc: overrideRecipient(config.bcc, options.bcc), dkim: defu(options.dkim, config.dkim) }); return { /** * Send an email using MailChannels Email API * @param options - The email options to send * @param dryRun - When 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`. * @example * ```ts * // Inside an API route handler * export default defineEventHandler(async (event) => { * const mailchannels = useMailChannels(event) * * const { data, error } = await mailchannels.send({ * to: 'to@example.com', * from: 'from@example.com', * subject: 'Test', * html: 'Test', * }) * * return { data } * }) * ``` */ send: async (options, dryRun) => emails.send(getOverrides(options), dryRun), /** * Queues an email message for asynchronous processing and returns immediately with a request ID. * * The email will be processed in the background, and you'll receive webhook events for all delivery status updates (e.g. `dropped`, `processed`, `delivered`, `hard-bounced`). These webhook events are identical to those sent for the synchronous /send endpoint. * * Use this endpoint when you need to send emails without waiting for processing to complete. This can improve your application's response time, especially when sending to multiple recipients. * @param options - The email options to send. * @example * ```ts * // Inside an API route handler * export default defineEventHandler(async (event) => { * const mailchannels = useMailChannels(event) * * const { data, error } = await mailchannels.queue({ * to: 'to@example.com', * from: 'from@example.com', * subject: 'Test', * html: 'Test', * }) * * return { data } * }) * ``` */ queue: async (options) => emails.queue(getOverrides(options)), /** * @deprecated Use `queue` instead. */ sendAsync: async (options) => emails.queue(getOverrides(options)) }; };