一尘不染

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

node.js

我已经创建了一个简单的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');
     });
}

…但是我在nodemailer导入过程中遇到了很多错误。

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根本不是为在浏览器中使用而设计的?如果是这种情况,我应该考虑其他选择吗?


阅读 310

收藏
2020-07-07

共1个答案

一尘不染

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

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

为此,您需要实现服务器端代码,这是由客户端的某些动作触发的,可能是node.js和nodemailer。

2020-07-07