Nestjs mailer module cannot find SMTP user and password problem
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@$!'