2017-07-18 212 views
3

我正嘗試使用nodemailer發送簡單的電子郵件,但遇到以下問題。這似乎是問題與webpack。我已經包含錯誤和有問題的代碼。謝謝!當使用Nodemailer和Webpack時無法解析dns和child_process

控制檯日誌:

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: '[email protected]', 
      pass: 'samplePassword' 
     } 
    }); 

    let mailOptions = { 
     from: '"Mr. Sender" <[email protected]>', 
     to: '[email protected]', 
     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); 
    }) 
    } 
} 

回答

9

你根本無法在前端nodemailer。 Nodemailer和其他依賴它的項目(例如gmail-send)是爲後端的nodejs使用而設計的。

相反,您應該考慮使用AWS-SES等第三方smtp服務,或者通過在後端使用nodemailer製作自己的服務器,並且前端通過https請求等方式調用它。

+0

謝謝!我要去看看它。我會回報。 – jab88

0

@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: [ '[email protected]' ] 
     }, 
     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: '[email protected]' // 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); 
     } 
    }); 
    } 
} 
相關問題