JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q67338F
  • License MIT

Module for sending emails with Nodemail for CMMV

Package Exports

  • @cmmv/email

Readme

CMMV Logo

Contract-Model-Model-View (CMMV)
Building scalable and modular applications using contracts.

NPM Version Package License

DocumentationReport Issue

Description

The @cmmv/email module provides a unified interface for email delivery using nodemailer. It supports SMTP, AWS SES, and other transport options, allowing developers to send emails with ease using the CMMV framework.

Features

  • Multi-Transport Support: Works with SMTP, AWS SES, and custom transports.

  • Integration with CMMV Framework: Seamless integration with other CMMV modules.

  • Logging and Error Handling: Built-in logging for monitoring email sending.

Installation

Install the @cmmv/email package via npm:

$ pnpm add @cmmv/email

Configuration

The @cmmv/email module requires configuration for the email provider and credentials. This can be set in the .cmmv.config.js file:

module.exports = {
    env: process.env.NODE_ENV,

    email: {
        host: process.env.EMAIL_HOST || "smtp.example.com",
        port: process.env.EMAIL_PORT || 587,
        secure: false,
        auth: {
            user: process.env.EMAIL_USER || "user@example.com",
            pass: process.env.EMAIL_PASS || "password"
        },
        SES: process.env.AWS_SES_ENABLED ? {
            accessKeyId: process.env.AWS_ACCESS_KEY_ID,
            secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
            region: process.env.AWS_REGION
        } : null,
        logger: process.env.EMAIL_LOGGER || false
    }
};

Setting Up the Application

In your index.ts, include the EmailService in the application:

import { Application } from "@cmmv/core";
import { EmailService } from "@cmmv/email";

Application.create({
    services: [EmailService],
});

Sending Emails

Emails can be sent using the EmailService:

import { EmailService } from "@cmmv/email";

async function sendEmail() {
    const success = await EmailService.getInstance().send(
        "from@example.com",
        "to@example.com",
        "Test Email",
        "<h1>Hello World</h1>",
        { cc: "cc@example.com" }
    );

    if (success) {
        console.log("Email sent successfully!");
    } else {
        console.error("Failed to send email.");
    }
}

AWS SES Integration

The module supports AWS Simple Email Service (SES) for scalable email delivery.

To enable SES, configure it in .cmmv.config.js:

email: {
    SES: {
        accessKeyId: "your-access-key",
        secretAccessKey: "your-secret-key",
        region: "us-east-1"
    }
}