0.2.3 • Published 4 months ago

@pixelplex/mail-service v0.2.3

Weekly downloads
-
License
MIT
Repository
-
Last release
4 months ago

Mail Service

Library for fast implementation of the functionality of sending email messages.

The sends messages them via email.

How to use

  1. create template for html, plain text and subject
  2. add @pixelplex/mail-service to your application
  3. Call MailModule.forRoot from @pixelplex/mail-service with specified gateways and options
  4. send a message by calling this.mailSenderService.sendEmail('test@gmail.com', { htmlPath: htmlTemplatePath, plainPath: plainTextTemplatePath, subjectPath: subjectTemplatePath }, {year: 20}); where mailSenderService is MailSenderService

Currently supported gateways:

  • slack
  • console
  • smtp
  • sendgrid

Didn't understand anything? Okay, let's see more details

Prepare templates

The service uses Handlebars as template engine. So all your email templates should be prepared as handlebars templates.

For sending message your templates folder should include this files:

  • {name}.handlebars - body of your email where {name} is just your template name for email. For example welcome.handlebars for the welcome use email
  • {name}.subject.handlebars - subject (title) of your email. You can use subject field to specify subject directly. It will override subjectPath
  • {name}.plain.handlebars - subject (title) of your email. You can use plain field to specify plain directly. It will override plainPath
# welcome.subject.handlebars Welcome, {{name}}! This is subject
# welcome.handlebars

<html>
  <head></head>
  <body>
    <p>Welcome, {{name}}!</p>
    <p>How are you?</p>
  </body>
</html>
# welcome.plain.handlebars

<html>
  <head></head>
  <body>
    <p>Welcome, {{name}}!</p>
    <p>How are you?</p>
  </body>
</html>

Configuration

NameDescriptionExample
gatewaysArray of gateways to send email['slack']
customGatewaysArray of custom gateways classes[CustomGateway]
smtp.hostMail gateway host addresssmtp.gmail.com
smtp.portMail gateway port number586
smtp.usernameMail gateway usernameusername
smtp.passwordMail gateway passwordqh05780c2qcm034579
*.senderMail sender addresspostmail@pixelplex.io
slack.urlHook URL in case you want to duplicate messages to Slackhttps://hooks.slack.com
sendgrid.apiKeySendgrid api keyoiqur9823uiofhaoafoe

Trigger new message sending

1. Install npm package

yarn add @pixelplex/mail-service

2. Import MailModule in your module you'll send mail from:

import { Module } from '@nestjs/common';
import { MailModule } from '@pixelplex/mail-service';
import { AppService } from './app.service';

@Module({
  controllers: [],
  imports: [
    MailModule.forRoot({
      gateways: ['slack', 'smtp', 'console'],
      slack: { url: 'hooks.slack.com' },
      smtp: {
        username: 'postmail@pixelplex.io',
        sender: 'postmail@pixelplex.io',
        host: 'smtp.ethereal.com',
        port: 678,
        password: 'password',
      },
    }),
  ],
  providers: [AppService],
})
export class AppModule {}

3. Call sendEmail to send your mail:

import { Injectable } from '@nestjs/common';
import { MailSenderService } from '@pixelplex/mail-service';

@Injectable()
export class AppService {
  constructor(private mailSenderService: MailSenderService) {}

  async onModuleInit() {
    this.mailSenderService.sendEmail(
      'test@gmail.com',
      // additionaly you can use plain: plainText, subject: subjectText to specify plain or subject directly
      // if so, plainPath or subjectPath will be ignored
      { htmlPath: htmlTemplatePath, plainPath: plainTextTemplatePath, subjectPath: subjectTemplatePath },
      { year: 20 },
    );
  }
}

{year: 20} is the object with data which will used as payload data during processing your handlebars template.

Custom gateways

To add custom gateways, provide class, that extends IGateway to customGateways options

@Injectable()
class CustomGateway implements IGateway {
  async send(to: string, content: Content): Promise<void> {
    console.log('this is custom gateway');
  }
}

MailModule.forRoot({ customGateways: [CustomGateway] });
0.2.3

4 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.2.2

5 months ago

0.1.22

2 years ago

0.1.20

2 years ago

0.1.21

2 years ago

0.1.18

3 years ago

0.1.19

3 years ago

0.1.17

3 years ago

0.1.16

3 years ago

0.1.15

3 years ago

0.1.14

3 years ago

0.1.13

3 years ago

0.1.12

3 years ago

0.1.10

3 years ago