编程语言
首页 > 编程语言> > javascript – 我可以在浏览器中使用nodemailer吗?

javascript – 我可以在浏览器中使用nodemailer吗?

作者:互联网

我创建了一个简单的react redux应用程序(使用webpack)和一个联系表单,当用户点击提交时我想生成并发送一封电子邮件.为此,我一直在尝试使用nodemailer,如下所示:

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

…但是我在节点编辑器导入时遇到了很多错误.

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)

我在这里遗漏了什么或者是不是设计用于浏览器的nod​​emailer?如果是这样的话,我应该看另一种选择吗?

解决方法:

node.js用于服务器端JavaScript,允许您执行许多无法在浏览器中完成的操作.

发送电子邮件不是可以在浏览器提供的沙箱中完成的,除了mailto links的有限功能之外,它基本上可以弹出用户的电子邮件客户端并填写一些链接.

您需要实现服务器端代码来执行此操作,由客户端的某些操作(可能是node.js和nodemailer)触发.

标签:nodemailer,javascript,reactjs,node-js,webpack
来源: https://codeday.me/bug/20191008/1874126.html