简体   繁体   English

Nodemailer 不在 NestJs 中发送电子邮件

[英]Nodemailer is not sending emails in NestJs

I have the next configuration for nodemailer package:我有 nodemailer package 的下一个配置:

 //App module @Module({ imports: [ MailerModule.forRoot({ transport: { host: 'localhost', port: 3000, secure: false, }, defaults: { from: '"nest-modules" <modules@nestjs.com>', }, template: { dir: __dirname + '/templates', adapter: new HandlebarsAdapter(), options: { strict: true, }, }, }), ... }) export class AppModule {}

And

 //Email service export class EmailService { constructor(private readonly mailerService: MailerService) {} public example(): void { this.mailerService.sendMail({ to: 'email@gmail.com', // list of receivers from: 'test@nestjs.com', // sender address subject: 'Testing Nest MailerModule ✔', // Subject line text: 'welcome', // plaintext body html: '<b>welcome</b>', // HTML body content }).then((r) => { console.log(r, 'email is sent'); }).catch((e) => { console.log(e, 'error sending email'); }); } }

I am using my local environement.我正在使用我当地的环境。 Tring the code above i get an error in catch block: Error: Greeting never received .尝试上面的代码,我在 catch 块中得到一个错误: Error: Greeting never received Why i get that error and how to send the email without any issue?为什么我收到该错误以及如何发送 email 没有任何问题?

  1. This code means这段代码意味着
MailerModule.forRoot({
      transport: {
        host: 'localhost',
        port: 3000,
        secure: false,
      },

nodemailer try to find SMTP mail transfer agent (relay) listening on localhost:3000. nodemailer 尝试在 localhost:3000 上查找 SMTP 邮件传输代理(中继)。 Its unlikely there is SMTP server on your machine on 3000 port, so most likely nodemailer cannot receive confirmation from anything on your 3000 port that it is SMTP server and throws error you mentioned.它不太可能在您的机器上的 3000 端口上存在 SMTP 服务器,因此很可能 nodemailer 无法从您的 3000 端口上的任何内容收到确认它是 SMTP 服务器并抛出您提到的错误。

  1. Try using this transport for nodemailer, it can work without SMTP relay https://www.npmjs.com/package/nodemailer-direct-transport尝试将此传输用于 nodemailer,它可以在没有 SMTP 中继https://www.npmjs.com/package/nodemailer-direct-transport的情况下工作

Code will be something like this:代码将是这样的:


import {directTransport} from 'nodemailer-direct-transport';

//App module 
@Module({
  imports: [
    MailerModule.forRoot({
      transport: directTransport({}),
      defaults: {
        from: '"nest-modules" <modules@nestjs.com>',
      },
      template: {
        dir: __dirname + '/templates',
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
    }),
   ...
})
export class AppModule {}

I'm not sure if you just wanna send an email using local SMTP or send an email in any SMTP available我不确定您是否只想使用本地 SMTP 发送 email 或发送 email 任何可用的 ZC2230ZF9171

if it's the latter you can use google SMTP or mailtrap.No need to set up SMTP server.如果是后者,您可以使用google SMTP 或mailtrap。无需设置SMTP 服务器。 The configuration will be配置将是

    MailerModule.forRoot({
   transport: {
        host: "smtp.gmail.com",
        port: "465",
        secure: true,
        auth: {
            user: "your_gmail_email",
            pass: "your_gmail_app_password"
          }
           }
         })
      // or for mailtrap.io 
       const nodemailer = require('nodemailer');
  let transporter = nodemailer.createTransport({
         host: 'smtp.mailtrap.io',
         port: 2525,
         auth: {
             user: "<user>",
             pass: "<pass>"
         }
 })

In the case of a local SMTP server, you can check smtp-server to create an SMTP server or MaiDev will help you to set up an SMTP server locally.如果是本地 SMTP 服务器,您可以查看smtp-server以创建 SMTP 服务器或MaiDev将帮助您在本地设置 SMTP 服务器。 MailDev has also available via docker that would be easier too. MailDev 也可以通过 docker 获得,这也会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM