An open API service indexing awesome lists of open source software.

https://github.com/ifaim/nestjs-nodemailer


https://github.com/ifaim/nestjs-nodemailer

Last synced: 11 months ago
JSON representation

Awesome Lists containing this project

README

          


Nest Logo

[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master
[travis-url]: https://travis-ci.org/nestjs/nest
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux
[linux-url]: https://travis-ci.org/nestjs/nest

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.



NPM Version
Package License
NPM Downloads
Travis
Linux
Coverage
Gitter
Backers on Open Collective
Sponsors on Open Collective



## Description

Nodemailer utilities module for [Nest](https://github.com/nestjs/nest) based on the [nodemailer](https://github.com/nodemailer/nodemailer) package.

## Installation

```bash
$ npm i --save @iaminfinity/nodemailer
```
## Usage

Import `NodemailerModule`:

```typescript
@Module({
imports: [
NodemailerModule.register({
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: 'username',
pass: 'password'
}
})
],
providers: [...]
})
export class AppModule {}
```

Inject `NodemailerService`:

```typescript
@Injectable()
export class AppService {
constructor(private readonly nodemailerService: NodemailerService) {}
}
```

## Async options

Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.

**1. Use factory**

```typescript
NodemailerModule.registerAsync({
useFactory: () => ({
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: 'username',
pass: 'password'
}
})
})
```

Obviously, our factory behaves like every other one (might be `async` and is able to inject dependencies through `inject`).

```typescript
NodemailerModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => configService.getMailConfig(),
inject: [ConfigService],
})
```

**2. Use class**

```typescript
NodemailerModule.registerAsync({
useClass: NodemailerConfigService
})
```

Above construction will instantiate `JwtConfigService` inside `JwtModule` and will leverage it to create options objec

```typescript
class NodemailerConfigService implements NodemailerOptionsFactory {
createNodemailerOptions(): NodemailerModuleOptions {
return {
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: 'username',
pass: 'password'
}
};
}
}
```

**3. Use existing**

```typescript
NodemailerModule.registerAsync({
imports: [ConfigModule],
useExisting: ConfigService
})
```

It works the same as `useClass` with one critical difference - `NodemailerModule` will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.

## Stay in touch

- Author - [Fahim Rahman](https://github.com/ifaim)