Options
All
  • Public
  • Public/Protected
  • All
Menu

@thermopylae/lib.email

@thermopylae/lib.email

Version Node Version Documentation License: MIT

Email client.

Install

npm install @thermopylae/lib.email

Description

This package contains EmailClient which can send emails.
In order to accomplish this task, it uses internally nodemailer npm package.

Usage

import { EmailClient } from '@thermopylae/lib.email';

(async function main() {
    const emailClient = new EmailClient({
        transport: {
            options: {
                service: 'gmail',
                auth: {
                    type: 'OAuth2',
                    user: 'sample_user@gmail.com',
                    clientId: 'CLIENT-ID',
                    clientSecret: 'CLIENT-SECRET',
                    refreshToken: 'REFRESH-TOKEN',
                    accessToken: 'ACCESS-TOKEN'
                }
            },
            defaults: {
                from: 'sample_user@gmail.com'
            }
        },
        hooks: {
            onTransportError(err) {
                console.error('Error encountered in email transport: ', err);
            },
            onTransportIdle() {
                console.info('Email transport is idle.');
            }
        }
    });
    
    const sentEmailInfo = await emailClient.send({
        to: ['anoter_user@example.com'],
        subject: 'Demo Email',
        text: 'Demo text'
    });
    console.log('Email has been sent. Details: ', JSON.stringify(sentEmailInfo));
    
    emailClient.close();
})();

API Reference

API documentation is available here.

It can also be generated by issuing the following commands:

git clone git@github.com:marinrusu1997/thermopylae.git
cd thermopylae
yarn install
yarn workspace @thermopylae/lib.email run doc

Author

👤 Rusu Marin

📝 License

Copyright © 2021 Rusu Marin.
This project is MIT licensed.