【问题标题】:Can't resolve dns and child_process when using Nodemailer and Webpack使用 Nodemailer 和 Webpack 时无法解析 dns 和 child_process
【发布时间】:2017-12-22 15:57:54
【问题描述】:

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

控制台日志:

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);
    })
  }
}

【问题讨论】:

    标签: node.js angular webpack nodemailer


    【解决方案1】:

    @Abdullah Adeeb 是正确的。我最终只使用 AWS-SES 并从前端调用它。

    修复组件代码:

    /// <reference types="aws-sdk" />
    import { Component } from '@angular/core';
    import * as aws from 'aws-sdk';
    import { SES } from 'aws-sdk'
    import { AwsConfig } from '../../../awsConfig';
    
    @Component({
      selector: 'app-contact',
      templateUrl: './contact.component.html',
      styleUrls: ['./contact.component.scss']
    })
    export class ContactComponent {
    
      private _ses: SES;
    
      constructor() {
        this.configureSES();
      }
    
      public sendMessage(): void {
        let params;
          params = {
            Destination: {
              ToAddresses: [ 'recipient@sample.com' ]
            },
            Message: {
              Body: {
                Html: {
                  Charset: 'UTF-8',
                  Data: '<h1>HTML needed inside of your email</h1>'
                },
                Text: {
                  Charset: 'UTF-8',
                  Data: 'Or you can use plain text'
                }
              },
              Subject: {
                Charset: 'UTF-8',
                Data: 'Sample message subject'
              }
            },
            Source: 'sender@sample.com' // Must be registered with AWS
          };
          this.sendEmail(params);
        }
      }
    
      private configureSES(): void {
        aws.config.credentials = {
          accessKeyId: AwsConfig.accessKeyId,
          secretAccessKey: AwsConfig.secretAccessKey
        };
        aws.config.update({
          region: AwsConfig.region
        });
        this._ses = new SES({
          apiVersion: '2010-12-01'
        });
      }
    
      private sendEmail(params): void {
        this._ses.sendEmail(params, function(err, data) {
          if (err) {
            console.log(err, err.stack);
          } else {
            console.log(data);
          }
        });
      }
    }
    

    【讨论】:

      【解决方案2】:

      您根本不能在前端使用 nodemailer。 Nodemailer 和其他依赖它的项目(即 gmail-send)是为后端的 nodejs 使用而制作的。

      相反,您应该考虑使用像 AWS-SES 这样的第 3 方 smtp 服务,或者通过在后端使用 nodemailer 来制作自己的服务,前端会通过 https 请求之类的方式调用它。

      【讨论】:

      • 谢谢!我要调查一下。我会回来报告的。
      猜你喜欢
      • 1970-01-01
      • 2015-07-12
      • 2014-08-12
      • 2017-04-24
      • 2010-09-18
      • 2021-09-27
      • 2018-05-03
      • 2016-07-29
      • 2021-01-12
      相关资源
      最近更新 更多