YesCoding

Go to English
search:

Nestjs mailer module cannot find SMTP user and password problem

thumbnail_nestjs_mailer_smtp

Nestjs mailer λͺ¨λ“ˆμ„ μ‚¬μš©ν•΄ 이메일을 λ³΄λ‚Όλ•Œ SMTP user, password 인증이 λ˜μ§€ μ•Šμ•˜λ˜ 이슈λ₯Ό ν•΄κ²°ν•œ troubleshooting κΈ°λ‘μž…λ‹ˆλ‹€.

상황

Nestjs 둜 RestAPI μ„œλ²„λ₯Ό λ§Œλ“€λ˜ 쀑 νšŒμ›κ°€μž…μ„ ν•œ μœ μ €μ—κ²Œ 확인 이메일을 보내야 ν•˜λŠ” μž‘μ—…μ„ ν•˜κ³  μžˆμ—ˆλ‹€. nestjs mailer λͺ¨λ“ˆμ„ μ„€μΉ˜ν•΄μ„œ κ΅¬ν˜„ν•˜λ˜ 쀑 env νŒŒμΌμ— SMTP_USER 와 SMTP_PASSWORD λ₯Ό 넣은 것이 μ½νžˆμ§€ μ•Šμ•„ mailer module 을 μ‹€ν–‰ν•  λ•Œ μ—λŸ¬κ°€ λ‚˜ 메일이 보내지지 μ•Šμ•˜λ‹€.

κΈ°μ‘΄ μ½”λ“œ

/src/mail/mail.module.ts import { MailerModule } from '@nestjs-modules/mailer'; import { EjsAdapter } from '@nestjs-modules/mailer/dist/adapters/ejs.adapter'; import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import * as path from 'path'; @Module({ imports: [ MailerModule.forRootAsync({ imports: [ConfigModule], inject: [ConfigService], useFactory: (configService: ConfigService) => ({ transport: { host: 'smtp.gmail.com', port: 587, secure: false, auth: { user: configService.get<string>('SMTP_AUTH_USER'), pass: configService.get<string>('SMTP_AUTH_PW'), }, }, defaults: { from: process.env.SUPPORT_EMAIL, }, template: { dir: `${path.resolve(__dirname, '../../templates')}`, adapter: new EjsAdapter(), options: { strict: true, }, }, }), }), ], }) export class MailModule {}
.env SMTP_AUTH_USER=my-email@mail.com SMTP_AUTH_PW=abcd@$!

configService.get<string>('SMTP_AUTH_USER'), configService.get<string>('SMTP_AUTH_PW') 이 μ œλŒ€λ‘œ λ™μž‘ν•˜μ§€ μ•Šμ•„ user, pass κ°€ λΉ„μ–΄μ„œ λ“€μ–΄κ°€μ„œ smtp.google.com 을 μ‚¬μš©ν•  수 μ—†λŠ” 것이 λ¬Έμ œμ˜€λ‹€.

env μ—λŠ” λΆ„λͺ…νžˆ 잘 λ„£μ—ˆκ³  mail.module.ts μ—μ„œ env 도 잘 μ½νžˆλŠ”λ° μ–΄λ–€ 것이 잘λͺ»μΈμ§€ λͺ°λΌ ν—€λ§Έλ‹€.

μ‚½μ§ˆλμ— μ•Œκ²Œλœ 원인은 λ°”λ‘œ SMTP_AUTH_PW 에 있던 $ μ˜€λ‹€.

https://docs.nestjs.com/techniques/configuration#expandable-variables

@nestjs/config λͺ¨λ“ˆμ„ μ‚¬μš©ν•΄μ„œ env ν™˜κ²½ λ³€μˆ˜λ“€μ„ μ½μ–΄μ˜€κ³  μžˆλŠ”λ°, 이 λͺ¨λ“ˆμ€ λ¬Έμ„œμ— λ³΄λ‹ˆ expandable λ³€μˆ˜λΌλŠ” 것을 μ§€μ›ν•˜κ³  μžˆλ‹€.

APP_URL=mywebsite.com SUPPORT_EMAIL=support@${APP_URL}

${} μ•ˆμ— λ³€μˆ˜λ₯Ό λ„£μœΌλ©΄ λ³€μˆ˜μ²˜λŸΌ μ‚¬μš©ν•  수 μžˆλŠ” κΈ°λŠ₯인데,

@nestjs/config κ°€ λ‚΄λΆ€μ μœΌλ‘œ μ‚¬μš©ν•˜κ³  μžˆλ‹€λŠ” dotenv-expand λͺ¨λ“ˆμ— λ“€μ–΄κ°€λ³΄λ‹ˆ

PASSWORD="s1mpl3" DB_PASS=$PASSWORD

dotenv-expand λͺ¨λ“ˆμ—μ„œλŠ” $ ν‘œμ‹œλ§ŒμœΌλ‘œλ„ λ³€μˆ˜λΌκ³  μΈμ‹ν•˜κ³  μžˆμ—ˆλ‹€.

SMTP_AUTH_PW=abcd@$!

μ΄λ ‡κ²Œ $ ν‘œμ‹œλ₯Ό 넣어버렸기 λ•Œλ¬Έμ—, 뒀에 ! κ°€ λ³€μˆ˜λ‘œ μΈμ‹λ˜μ–΄μ„œ 인증에 μ‹€νŒ¨ν–ˆλ˜ 것이닀.

κ·Έλž˜μ„œ μ•žλ’€μ— β€˜ λ₯Ό λΆ™μ—¬ string 으둜 λ³€ν™˜ν•΄ ν•΄κ²°ν•˜μ˜€λ‹€.

solution

SMTP_AUTH_PW='abcd@$!'
Recommend Post
nestjs request dto and response dto
nestjs request dto and response dto
google social login ν•œ μœ μ €μ˜ 생년월일 κ°€μ Έμ˜€κΈ°
google social login ν•œ μœ μ €μ˜ 생년월일 κ°€μ Έμ˜€κΈ°
TypeORM soft delete on cascade
TypeORM soft delete on cascade
typeORM entity μ—μ„œ λΉ„λ°€λ²ˆν˜Έ hash ν•˜κΈ°
typeORM entity μ—μ„œ λΉ„λ°€λ²ˆν˜Έ hash ν•˜κΈ°
Β© Copyright 2022, YesCoding