Keplers Mail Service

NodeJS

Node.js integration examples for Keplers Mail Service

Send emails using Node.js with both REST API and SMTP approaches.

Installation

npm install axios

Basic Setup

# .env
KEPLERS_API_KEY=kms_your_api_key.live_...

REST API Integration

Basic Client

const axios = require('axios');

class KeplersMailClient {
    constructor(apiKey) {
        this.client = axios.create({
            baseURL: 'https://api.keplars.com',
            headers: {
                'Authorization': `Bearer ${apiKey}`,
                'Content-Type': 'application/json'
            }
        });
    }

    async sendEmail(emailData) {
        try {
            const response = await this.client.post('/api/v1/send-email/queue', emailData);
            return response.data;
        } catch (error) {
            throw new Error(`Failed to send email: ${error.response?.data?.message || error.message}`);
        }
    }

    async sendInstantEmail(emailData) {
        try {
            const response = await this.client.post('/api/v1/send-email/instant', emailData);
            return response.data;
        } catch (error) {
            throw new Error(`Failed to send instant email: ${error.response?.data?.message || error.message}`);
        }
    }
}

module.exports = KeplersMailClient;

Usage Examples

const KeplersMailClient = require('./keplers-client');

const client = new KeplersMailClient(process.env.KEPLERS_API_KEY);

async function sendWelcomeEmail(userEmail, userName) {
    try {
        const result = await client.sendEmail({
            to: [userEmail],
            subject: `Welcome ${userName}!`,
            body: `<h1>Welcome ${userName}!</h1><p>Thank you for joining our platform.</p>`,
            is_html: true
        });
        
        console.log('Email sent:', result);
        return result;
    } catch (error) {
        console.error('Failed to send email:', error.message);
        throw error;
    }
}

async function sendVerificationEmail(userEmail, code) {
    try {
        const result = await client.sendInstantEmail({
            to: [userEmail],
            subject: 'Verify Your Email',
            body: `Your verification code: ${code}`,
            is_html: false
        });
        
        console.log('Verification email sent:', result);
        return result;
    } catch (error) {
        console.error('Failed to send verification email:', error.message);
        throw error;
    }
}

sendWelcomeEmail('[email protected]', 'John');
sendVerificationEmail('[email protected]', '123456');

Express.js Integration

const express = require('express');
const KeplersMailClient = require('./keplers-client');

const app = express();
const mailClient = new KeplersMailClient(process.env.KEPLERS_API_KEY);

app.use(express.json());

app.post('/api/send-email', async (req, res) => {
    try {
        const { email, name } = req.body;
        
        const result = await mailClient.sendEmail({
            to: [email],
            subject: `Welcome ${name}!`,
            body: `<h1>Welcome ${name}!</h1>`,
            is_html: true
        });
        
        res.json({ success: true, data: result });
    } catch (error) {
        console.error('Failed to send email:', error);
        res.status(500).json({ error: 'Failed to send email' });
    }
});

app.listen(3000, () => {
    console.log('Server running on port 3000');
});

SMTP Integration

Installation

npm install nodemailer

SMTP Client

const nodemailer = require('nodemailer');

class KeplersEmailSMTPClient {
    constructor(smtpConfig) {
        this.transporter = nodemailer.createTransporter({
            host: 'smtp.keplars.com',
            port: 587,
            secure: false,
            auth: {
                user: smtpConfig.username,
                pass: smtpConfig.password
            }
        });
    }

    async sendEmail(emailData) {
        try {
            const mailOptions = {
                to: emailData.to.join(', '),
                subject: emailData.subject,
                html: emailData.is_html ? emailData.body : undefined,
                text: !emailData.is_html ? emailData.body : undefined
            };

            const info = await this.transporter.sendMail(mailOptions);
            
            return {
                success: true,
                data: { id: info.messageId, status: 'sent' }
            };
        } catch (error) {
            throw new Error(`SMTP send failed: ${error.message}`);
        }
    }
}

module.exports = KeplersEmailSMTPClient;

SMTP Usage

const KeplersEmailSMTPClient = require('./smtp-client');

const smtpClient = new KeplersEmailSMTPClient({
    username: process.env.KEPLERS_SMTP_USERNAME,
    password: process.env.KEPLERS_SMTP_PASSWORD
});

async function sendEmailViaSMTP(userEmail, subject, body) {
    try {
        const result = await smtpClient.sendEmail({
            to: [userEmail],
            subject: subject,
            body: body,
            is_html: true
        });
        
        console.log('SMTP email sent:', result);
        return result;
    } catch (error) {
        console.error('SMTP send failed:', error.message);
        throw error;
    }
}

sendEmailViaSMTP('[email protected]', 'Test Email', '<p>Hello World!</p>');

Choose REST API for advanced features or SMTP for traditional email client integration.

On this page