使用Nodemailer和Webpack时无法解析dns和child_process [英] Can't resolve dns and child_process when using Nodemailer and Webpack

查看:333
本文介绍了使用Nodemailer和Webpack时无法解析dns和child_process的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用nodemailer发送简单的电子邮件,但是遇到了以下问题.看来问题出在webpack.我已经包含了错误和有问题的代码.谢谢!

I'm trying to send a simple email using nodemailer, but am getting the following issues. It seems like the issue is with webpack. I've included the error and the code in question. Thanks!

控制台日志:

ERROR in ./~/nodemailer/lib/mailer/index.js
Module not found: Error: Can't resolve 'dns' in 
'/Users//Users/user/Projects/world-domination/project-folder/node_modules/nodemailer/lib/mailer'
@ ./~/nodemailer/lib/mailer/index.js 14:12-26
@ ./~/nodemailer/lib/nodemailer.js
@ ./src/app/components/contact/contact.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

ERROR in ./~/nodemailer/lib/sendmail-transport/index.js
Module not found: Error: Can't resolve 'child_process' in 
'/Users/user/Projects/world-domination/project-folder/node_modules/nodemailer/lib/sendmail-transport'
@ ./~/nodemailer/lib/sendmail-transport/index.js 3:14-38
@ ./~/nodemailer/lib/nodemailer.js
@ ./src/app/components/contact/contact.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

有问题的代码:

import { Component, OnInit } from '@angular/core';
import * as nodemailer from 'nodemailer';

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.scss']
})
export class ContactComponent implements OnInit {
    public sendMessage(): void {
        let transporter = nodemailer.createTransport({
          host: 'smtp.example.com',
          port: 465,
          secure: true,
          auth: {
            user: 'sampleAddress@gmail.com',
            pass: 'samplePassword'
         }
    });

    let mailOptions = {
        from: '"Mr. Sender" <sampleAddress@gmail.com>',
        to: 'sampleRecipient@gmail.com',
        subject: 'Test email subject',
        text: 'Test email body'
    };

    transporter.sendMail(mailOptions, (error, info) => {
      if (error) {
        return console.log(error);
      }
      console.log('Message %s sent: %s', info.messageId, info.response);
    })
  }
}

推荐答案

您根本无法在前端使用nodemailer. Nodemailer和其他依赖于它的项目(即gmail-send)是为在后端使用nodejs而设计的.

You simply can't have nodemailer in the frontend. Nodemailer and other projects depending on it (i.e. gmail-send) are made for nodejs use in the backend.

相反,您应该研究使用诸如AWS-SES之类的第三方smtp服务,或者通过在后端使用nodemailer来创建自己的服务,并且前端将通过诸如https请求之类的东西来调用它.

Instead, you should look into either using a 3rd party smtp service like AWS-SES or make your own by using nodemailer in the backend and the front end would invoke it through something like https requests.

这篇关于使用Nodemailer和Webpack时无法解析dns和child_process的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆