@techie04/xpresser-mailer

Mailer Plugin for xpresser

Usage no npm install needed!

<script type="module">
  import techie04XpresserMailer from 'https://cdn.skypack.dev/@techie04/xpresser-mailer';
</script>

README

XpresserJs Mailer Plugin

Build Status License npm version

A library to help you send mails.

This plugin makes use of nodemailer and aws-sdk ses.

MENU

Installation

npm i @techie04/xpresser-mailer

# OR
yarn add @techie04/xpresser-mailer

Add to plugins.json

{
  "npm://@techie04/xpresser-mailer": true
}

Add to your project config.

({
    // SMTP CONFIG
    "mailer": {
        provider: "smtp", // smtp, ses,postmark
        configs: {
            // Smtp config inherits nodemailers's config.
            smtp: {
                host: "", // SMTP Server Host
                port: "", // SMTP Server Port
                auth: {
                    user: "", // SMTP Server Username
                    pass: "", // SMTP Server Password
                },
                fromEmail: "", // Sender Email
            },
            
            aws: {
                region: "", // AWS Server Region
                fromEmail: "no-reply@example.com", // From email
                // AWS credentials
                AWS_ACCESS_KEY_ID: "",
                AWS_SECRET_ACCESS_KEY: ""
            },
            
            postmark: {
                apiToken: ""
            }
        }
    },

})

Usage

In your controller or anywhere in your project.

Javascript
const { sendMail } = require("@techie04/xpresser-mailer");

await sendMail(message);
Typescript
import { sendMail } from "@techie04/xpresser-mailer";

await sendMail<Message, MessageResponse>(message);
  • Message: Type of message content (depending on provider)
  • MessageResponse: Type of response returned to the sendMail function (depending on provider)

Creating a custom provider

Create a file, maybe providers/CustomProvider.ts

import { MailProvider } from "@techie04/xpresser-mailer/MailProvider";

// The name should be the same with the config key.
const CustomProvider = new MailProvider("customProvider", {
    /**
     * Initialize your provider.
     * @param config - The config for your provider. i.e mailer.configs.customProvider
     * @param $ - xpresser instance
     */
    initialize(config, $) {
        // Validate config here
        // return client.
    },

    /**
     * Send mail function.
     * @param mail - the mail object or data
     * @param config - The config for your provider. i.e mailer.configs.customProvider
     * @param client - The client returned from the initialize function above.
     * @param $
     */
    sendMail({ mail, config, client }, $) {
        // Send mail here.
    }
});

export default CustomProvider;
Add to config
const config = {
    "mailer": {
        // ... other configs
        customProviders: {
            // key is providers name
            // value is path to provider file without .js or .ts
            customProvider: "providers/CustomProvider"
        }
    }
}