Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

javascript - Can I use nodemailer in browser?

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: '[email protected]',
        from: '[email protected]',
        subject: 'Message from bar',
        text: 'Hello world!'
    };

    const connectionInfo = {
         host: '...',
         port: 123,
         secure: true,
         auth: {
              user: '[email protected]',
              pass: 'foo123'
         }
     };

     const transporter = nodemailer.createTransport('SMTP', connectionInfo);
     transporter.sendMail(emailInfo, function(error){
          if(error){ console.log('error'); }
          console.log('sent');
     });
}

... 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)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

node.js is for server side JavaScript, and allows you to do many things that can't be done in a browser.

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.

You need to implement server side code to do this, triggered by some action from the client, which could be node.js and nodemailer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...