NestJS mailer 활용하여 이메일 보내는 방법
NestJS에는 다양한 모듈이 있는데요. 이 중 mailer 모듈을 활용하여 NestJS로 이메일을 손쉽게 보낼 수 있습니다. 이 글에서는 NestJS로 이메일 보내는 방법에 대해 소개하겠습니다.
NestJS mailer 사용법
NestJS에서 이메일을 보내기 위해서는@nestjs-modules/mailer 라는 모듈을 사용해야 합니다. 이를 사용하기 위해서는 아래의 과정이 필요합니다.
설치
yarn과 npm 중 사용자 환경에 맞는 설치를 진행합니다.
yarn add @nestjs-modules/mailer nodemailer
#or
npm install --save @nestjs-modules/mailer nodemailer
이메일 템플릿 설치
이는 별도의 템플릿 지정이 필요할 때 설치하면 되며, handlebars, pug, ejs 의 템플릿 중 원하는 하나를 설치하면 됩니다.
yarn
yarn add handlebars
#or
yarn add pug
#or
yarn add ejs
npm
npm install --save handlebars
#or
npm install --save pug
#or
npm install --save ejs
App.module 설정
App.module에 MailerModule을 추가합니다.
//app.module.ts
import { Module } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { PugAdapter } from '@nestjs-modules/mailer/dist/adapters/pug.adapter';
@Module({
imports: [
MailerModule.forRoot({
transport: 'smtps://user@domain.com:pass@smtp.domain.com',
defaults: {
from: '"nest-modules" <modules@nestjs.com>',
},
template: {
dir: __dirname + '/templates',
adapter: new PugAdapter(),
options: {
strict: true,
},
},
}),
],
})
export class AppModule {}
서비스 설정
아래와 같이 서비스 설정 후 사용가능합니다.
import { Injectable } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';
@Injectable()
export class ExampleService {
constructor(private readonly mailerService: MailerService) {}
public example(): void {
this.mailerService
.sendMail({
to: 'test@nestjs.com', // list of receivers
from: 'noreply@nestjs.com', // sender address
subject: 'Testing Nest MailerModule ✔', // Subject line
text: 'welcome', // plaintext body
html: '<b>welcome</b>', // HTML body content
})
.then(() => {})
.catch(() => {});
}
}
이밖에도 NestJS의 mailer 사용법 및 다양한 예시가 궁금하다면 아래 페이지를 참고 바랍니다.
NestJS - Mailer · Modules for the nestjs framework
Modules for the nestjs framework
nest-modules.github.io
▼ NestJS의 다양한 기능들 ▼
NestJS createQueryBuilder 사용하는 방법, 예시코드
NestJS createQueryBuilder 사용하는 방법, 예시코드 NestJS는 TypeScript 기반의 서버 사이드 애플리케이션을 개발하는 데 사용되는 강력한 프레임워크입니다. 데이터베이스와의 상호작용은 매우 중요하며
lemonlog.tistory.com
NestJS TypeORM 트랜잭션 사용방법, 예시코드
NestJS TypeORM 트랜잭션 사용방법, 예시코드 트랜잭션은 데이터베이스에서 수행되는 작업의 단위를 나타냅니다. 이는 다른 트랜잭션과 독립적으로 일관되고 안정적인 방식으로 처리되어야 합니다
lemonlog.tistory.com
'Programming & Platform > NestJS' 카테고리의 다른 글
TypeORM 에러 해결, Entity Metadata Not Found (0) | 2024.03.26 |
---|---|
NestJS createQueryBuilder 사용하는 방법, 예시코드 (0) | 2024.01.10 |
NestJS TypeORM 트랜잭션 사용방법, 예시코드 (0) | 2024.01.06 |
NestJS 빠른 테스트를 위한 명령어 npm run start:dev (0) | 2024.01.05 |
NestJS ejs 적용하는 방법, 동적 웹페이지 렌더링 하기 (0) | 2024.01.04 |