Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/webtretech/nestjs-mailer-react-adapter

๐Ÿ“จ Build and send emails in Nest framework using React.js
https://github.com/webtretech/nestjs-mailer-react-adapter

email-template mailer nest nestjs nestjs-mailer nodejs nodemailer react reactjs typescript

Last synced: 7 days ago
JSON representation

๐Ÿ“จ Build and send emails in Nest framework using React.js

Awesome Lists containing this project

README

        



Nest Logo


๐Ÿ“จ Build and send emails in Nest framework using React.js


NPM Version
Package License
NPM Downloads

## Features

- ๐Ÿฆพ Write your email templates in [React](https://github.com/facebook/react/) and [TypeScript](https://www.typescriptlang.org/)

- ๐Ÿ“ฌ No more template not found / sending blank emails.

- ๐Ÿ”ฐ No more missing context / variables from template.

- ๐Ÿงช Write testable templates intended for email clients.

- ๐Ÿ’Œ Built on top of [`react-email`](https://github.com/resendlabs/react-email) โ€” the next generation of writing emails.

## Installation

> This library is an adapter for the [`@nestjs-modules/mailer`](https://github.com/nest-modules/mailer) module, so we'll install the dependencies alongside by running the command below.

```sh
npm i @webtre/nestjs-mailer-react-adapter @nestjs-modules/mailer nodemailer
```

### Getting Started

To add support for `React` to your project, modify `tsconfig.json`

```javascript
{
"compilerOptions": {
// add this line
"jsx": "react-jsx"
}
}
```

### Configuration

```javascript
// src/app.module.ts
import { Module } from "@nestjs/common";
import { MailerModule } from "@nestjs-modules/mailer";
import { ReactAdapter } from "@webtre/nestjs-mailer-react-adapter";

@Module({
imports: [
MailerModule.forRoot({
transport: {
host: "smtp.domain.com",
port: 2525,
secure: false,
auth: {
user: "[email protected]",
pass: "password",
},
},
defaults: {
from: '"Webtre Technologies" ',
},
template: {
dir: __dirname + "/templates",
// Use the adapter
adapter: new ReactAdapter(),

// Or with optional config
adapter: new ReactAdapter({
pretty: false,
plainText: true,
htmlToTextOptions: {
wordwrap: 130,
limits: {
ellipsis: "...",
},
},
}),
},
}),
],
})
export class AppModule {}
```

To see more options that can be passed to the `htmlToTextOptions` object, [click here](https://github.com/html-to-text/node-html-to-text/tree/master/packages/html-to-text#options).

### Service Provider

```javascript
import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';

@Injectable()
export class ExampleService {
constructor(private readonly mailerService: MailerService) {}

async public example(): Promise {
await this.mailerService
.sendMail({
to: '[email protected]',
subject: 'Testing react template',
template: 'welcome', // The compiled extension is appended automatically.
context: { // Data to be passed as props to your template.
code: '123456',
name: 'John Doe',
},
});
}
}
```

### React Template

```javascript
// src/templates/welcome.tsx
interface Props {
code: string;
name: string;
}

export default function Welcome({ name, code }: Props) {
return (


Hi {name}, thanks for signing up. Your code is {code}

);
}
```

## Example

You can also check the [example folder](./example) in this repository for a working usage example.

## Credits

- [`react-email`](https://github.com/resendlabs/react-email) โ€” build and send emails using React
- [`@nestjs-modules/mailer`](https://github.com/nest-modules/mailer) โ€” a mailer module for Nest framework (node.js)

## License

[MIT](./LICENSE) License ยฉ 2022 [Webtre Technologies](https://github.com/webtretech)