Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hodfords-solutions/nestjs-mailer
Simplifies sending emails in NestJS apps
https://github.com/hodfords-solutions/nestjs-mailer
mailer nestjs nodejs
Last synced: about 12 hours ago
JSON representation
Simplifies sending emails in NestJS apps
- Host: GitHub
- URL: https://github.com/hodfords-solutions/nestjs-mailer
- Owner: hodfords-solutions
- Created: 2022-11-02T12:59:13.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-24T04:08:15.000Z (4 months ago)
- Last Synced: 2025-01-02T09:11:49.804Z (8 days ago)
- Topics: mailer, nestjs, nodejs
- Language: TypeScript
- Homepage: https://opensource.hodfords.uk/nestjs-mailer
- Size: 365 KB
- Stars: 41
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
nestjs-mailer simplifies integrating and managing email functionalities in NestJS applications, making email operations easier and more efficient.
## Installation 🤖
Install the `nestjs-mailer` package with:
```
npm install @hodfords/nestjs-mailer --save
```To configure the mailer module dynamically, use `forRoot` to define your email template renderers, transport settings, and default sender email.
```typescript
export const mailConfig = MailerModule.forRoot({
renders: {
adapters: [
new HbsAdapter({
templateFolder: path.join(env.ROOT_PATH, `mails/templates/${getEmailFolder()}`),
defaultVariable: async () => getMailConfigurations()
}),
new TranslateAdapter((text: string, options: any) => trans(text, options)),
new MjmlAdapter()
],
transport: env.MAILER_URL,
defaultFrom: env.CONTACT_MAIL_ADDRESS
},
...
});
```For more advanced use cases where additional services or repositories are required, you can register the module using `forRootAsync`. This allows injecting services, repositories, or even database connections for more complex setups
```typescript
export const mailConfig = MailerModule.forRootAsync({
imports: [CoreModule],
inject: [Connection, StorageService],
useFactory: (connection: Connection, storageService: StorageService) => {
const settingRepo = connection.getCustomRepository(SettingRepository);
const hbsAdapter = new HbsAdapter({
templateFolder: path.join(env.ROOT_PATH, `mails/templates/${getEmailFolder()}`),
defaultVariable: async (mail: BaseMail) => {
const variables = getMailConfigurations();
if (mail.isWhitelabeled) {
const setting = await settingRepo.findOne({ tenant: mail.tenantId });
variables.logoUrl = await storageService.generateBlobUrl(setting.blobLogo);
}
return variables;
}
});
return {
renders: {
adapters: [
hbsAdapter,
new TranslateAdapter((text: string, options: any) => trans(text, options)),
new MjmlAdapter()
]
},
transport: env.MAILER_URL,
defaultFrom: env.CONTACT_MAIL_ADDRESS
};
}
});
```## Usage 🚀
### Adapters
Currently, nestjs-mailer supports the following adapters:
- `HbsAdapter`: For rendering Handlebars templates with dynamic variables and templates.
- `TranslateAdapter`: For handling multi-language support and translations.
- `MjmlAdapter`: For generating responsive HTML emails using MJML templates.### Defining an Email
To define a custom email, extend the BaseMail class and specify the email subject, template path, and data.
Here's an example of how to define a `WelcomeEmail`:
```typescript
import { BaseMail } from '@hodfords/nestjs-mailer';export class WelcomeMail extends BaseMail {
constructor(private email: string) {
super();
}get subject(): string {
return 'Welcome to Hodfords!';
}get template(): string {
return path.join(env.ROOT_PATH, 'welcome-mail.mail.hbs');
}data(): Record {
return {
content:
"Welcome to our system! We're excited to have you on board and look forward to providing you with a seamless and enjoyable experience."
};
}get to(): string {
return this.email;
}
}
```### Sending an Email
To send an email, inject the `MailerService` into your service and utilize the appropriate method for sending emails
```typescript
import { MailService } from '@hodfords/nestjs-mailer';@Injectable()
class YourService {
constructor(private mailService: MailerService) {}
}
```You have two options for sending emails:
- **Send Immediately**: Send a single email right away.
```typescript
1. const mail = new WelcomeMail(user.email);
await this.mailService.send(mail);
```- **Add to Queue**: Use this method when you need to send a large number of emails. Emails will be queued and sent
asynchronously.```typescript
for (const user of users) {
const mail = new WelcomeMail(user.email);
await this.mailService.addToQueue(mail);
}
```## License 📝
This project is licensed under the MIT License