How to send an e-mail from Node.js?

…衆ロ難τιáo~ 提交于 2019-12-04 17:07:18

I use nodemailer to send emails and just love how simple it is.

This is the website to that module: https://nodemailer.com/about/

It has all the examples that you can follow Below is an example I got from w3school. I always use this and works like charm. Depends on what kind of email you use (gmail, hotmail, outlook,...), make proper changes and you're good

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

I found the cause of the problem. Originally, the function for sending the e-mail was located inside a React component. An answer in this question said it's impossible to send an e-mail from the front end.

So I moved the e-mail sending routine into server.js and now the original error disappeared.

Hi @DP_ and everyone.-

I had succesfuly implemented nodemailer, thanks to you also, i had the same DNS error and moved the Send function to server.js and that helped me a lot. However to make it work with Nodemailer i used Hapi.js and also xoauth2 becouse i want to send a custom e-mail through gmail, which is very important for me. You need to configure Xoauth2 first at Google Api and add the api's to your account first, then wait some time till your refreshToken is approved and add it to your app. And that is in short what i did, hope this helps somebody becouse it took me some time to get it working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!