superhero
superhero

Reputation: 6472

how to send an email in nodejs

I read the following, Sending emails in Node.js? but I'm looking for a way to send an email, not through an smtp server. As in the linux envirement you have different options such as sendmail and others

I could ofc use the environment I'm in to make use of the already existing functionality, but I would be interested to learn how one would dispatch the email using only js, if even possible..


I set up an smtp server using the smtp module: https://github.com/andris9/smtp-server why I'm interested in the delivery part of a server I already setup.

Upvotes: 7

Views: 16532

Answers (4)

coder
coder

Reputation: 73

First:Install nodemailer
npm install nodemailer
Then put this into your node file:

var nodemailer = require('nodemailer');
var http = require('http');
var url = require('url');
console.log("Creating Transport")
var transporter = nodemailer.createTransport({
    service:'Hotmail',
    auth: {
        user:'[email protected]',
        pass: 'alice123@'
   }
});
var mailOptions = {
    from:'[email protected]',
    to: '[email protected]',
    subject: 'This is a test: test',
    text:'TgK'
}
console.log("Sending mail")
transporter.sendMail(mailOptions, function(error, info) {
    if (error) {
        console.log(error);
    } else {
        console.log('Email sent: ' + info.response)
    }
})

It usally works Sources:W3Schools and Nodemailer's official site

Upvotes: 3

GameBoy
GameBoy

Reputation: 119

You can use sendmail in Node js. I'v using it and it's working fine for me.

npm install sendmail --save

const sendmail = require('sendmail')();
 
sendmail({
    from: '[email protected]',
    to: '[email protected], [email protected], [email protected] ',
    subject: 'test sendmail',
    html: 'Mail of test sendmail ',
  }, function(err, reply) {
    console.log(err && err.stack);
});

https://www.npmjs.com/package/sendmail

Upvotes: 0

Siddharth Shukla
Siddharth Shukla

Reputation: 1131

     var nodemailer = require('nodemailer');
     var send = require('gmail-send');
     var mailserverifo = nodemailer.createTransport({
     service: 'gmail',
     host : "smtp.gmail.com",
     port : "465",
     ssl : true,
     auth: {
     user: '[email protected]',
     pass: 'password@'
    }
    });
      var Mailinfo = {
      from: '[email protected]',
      to: '[email protected]',
      subject: 'Testing email from node js server',
      text: 'That was easy!'
     };

    mailserverifo.sendMail(Mailinfo, function(error, info){
    if (error) {
     console.log(error);
    } else {
     console.log('Email Send Success: ' + info.response);
    }
    });

   Enable less secure app form setting -
   https://www.google.com/settings/security/lesssecureapps
   Disable Captcha  - 
   https://accounts.google.com/b/0/displayunlockcaptcha

Upvotes: 0

user1695032
user1695032

Reputation: 1142

Take a look at node-mailer. You can set it up without smtp server. https://github.com/nodemailer/nodemailer

Upvotes: 6

Related Questions