我正在尝试使用 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: '[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);
})
}
}
你根本不能在前端使用nodemailer。 Nodemailer 和其他依赖它的项目(即 gmail-send)是为在后端使用 Nodejs 而制作的。
相反,您应该考虑使用第 3 方 smtp 服务(例如 AWS-SES),或者通过在后端使用 nodemailer 来创建自己的 smtp 服务,前端将通过 https 请求之类的方式调用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)