Jac*_*obB 6 node.js nodemailer webpack angular
我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)
有问题的代码:
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);
})
}
}
Run Code Online (Sandbox Code Playgroud)
Abd*_*eeb 18
你根本不能在前端有nodemailer.Nodemailer和其他依赖它的项目(即gmail-send)是为后端使用的nodejs而制作的.
相反,您应该考虑使用像AWS-SES这样的第三方smtp服务,或者通过在后端使用nodemailer来创建自己的服务,前端会通过https请求等方式调用它.
| 归档时间: |
|
| 查看次数: |
5478 次 |
| 最近记录: |