我可以在浏览器中使用nodemailer吗? [英] Can I use nodemailer in browser?
问题描述
我创建了一个简单的 react
redux
app(使用 webpack
)使用联系表单,当用户点击提交时,我想生成并发送电子邮件。要做到这一点,我一直在尝试使用 nodemailer
,如下所示:
I've created a simple react
redux
app (using webpack
) with a contact form which when the user clicks submit I want to generate and send an email. To do this I have been trying to use nodemailer
like so:
import nodemailer from 'nodemailer';
//...
sendEmail () {
const emailInfo = {
to: 'foo@bar.com',
from: 'bar@foo.com',
subject: 'Message from bar',
text: 'Hello world!'
};
const connectionInfo = {
host: '...',
port: 123,
secure: true,
auth: {
user: 'foo@bar.com',
pass: 'foo123'
}
};
const transporter = nodemailer.createTransport('SMTP', connectionInfo);
transporter.sendMail(emailInfo, function(error){
if(error){ console.log('error'); }
console.log('sent');
});
}
...但是我遇到了很多错误 nodemailer
导入。
... however I've been getting a lot of errors to do with the nodemailer
import.
ERROR in ./~/nodemailer/package.json
Module parse failed: /Users/myUser/website/node_modules/nodemailer/package.json Unexpected token (2:8)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:8)
ERROR in ./~/nodemailer/lib/http-proxy.js
Module not found: Error: Cannot resolve module 'net' in /Users/myUser/website/node_modules/nodemailer/lib
@ ./~/nodemailer/lib/http-proxy.js 7:10-24
ERROR in ./~/nodemailer/lib/http-proxy.js
Module not found: Error: Cannot resolve module 'tls' in /Users/myUser/website/node_modules/nodemailer/lib
@ ./~/nodemailer/lib/http-proxy.js 8:10-24
ERROR in ./~/nodemailer/~/nodemailer-direct-transport/lib/direct-transport.js
Module not found: Error: Cannot resolve module 'dns' in /Users/myUser/website/node_modules/nodemailer/node_modules/nodemailer-direct-transport/lib
@ ./~/nodemailer/~/nodemailer-direct-transport/lib/direct-transport.js 5:10-24
ERROR in ./~/nodemailer/~/nodemailer-direct-transport/lib/direct-transport.js
Module not found: Error: Cannot resolve module 'net' in /Users/myUser/website/node_modules/nodemailer/node_modules/nodemailer-direct-transport/lib
@ ./~/nodemailer/~/nodemailer-direct-transport/lib/direct-transport.js 6:10-24
ERROR in ./~/nodemailer/~/nodemailer-direct-transport/package.json
Module parse failed: /Users/myUser/website/node_modules/nodemailer/node_modules/nodemailer-direct-transport/package.json Unexpected token (2:8)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:8)
我在这里遗漏了什么或是 nodemailer
根本没有设计用于浏览器?如果是这种情况,我应该看看另一个选项吗?
Am I missing something here or is nodemailer
simply not designed for use in the browser? If that's the case is there another option I should be looking at?
推荐答案
node.js用于服务器端JavaScript,并允许你可以做许多在浏览器中无法完成的事情。
node.js is for server side JavaScript, and allows you to do many things that can't be done in a browser.
发送电子邮件不是可以在浏览器提供的沙箱中完成的,超出限制范围 mailto links 的功能,基本上可以弹出用户的电子邮件客户端填写了一些链接。
Sending email is not something that can be done in the sandbox provided by a browser, beyond the limited capabilities of mailto links which can basically pop up the user's email client with some links filled in.
您需要实现服务器端代码来执行此操作,由客户端的某些操作触发,可能是node.js和nodemailer 。
You need to implement server side code to do this, triggered by some action from the client, which could be node.js and nodemailer.
这篇关于我可以在浏览器中使用nodemailer吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!