https://github.com/ifaim/nestjs-nodemailer
https://github.com/ifaim/nestjs-nodemailer
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ifaim/nestjs-nodemailer
- Owner: ifaim
- Created: 2018-09-07T19:29:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-03T00:59:01.000Z (over 7 years ago)
- Last Synced: 2025-04-10T20:27:48.973Z (about 1 year ago)
- Language: TypeScript
- Size: 47.9 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[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.
## 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)