21

I'd like to send mails on my local server but it seems not working with Nodemailer and NodeJS.

Is there any solutions to send mails from local?

    var contact = {subject: 'test', message: "test message", email: 'visitor@gmail.com'};
    var to = "myemail@gmail.com";
    var transporter = nodemailer.createTransport();
    transporter.sendMail({
      from: contact.email,
      to: to,
      subject: contact.subject,
      text: contact.message
    });
elreeda
  • 4,525
  • 2
  • 18
  • 45
tonymx227
  • 5,293
  • 16
  • 48
  • 91

5 Answers5

11
const transporter = nodemailer.createTransport({
  port: 25, // Postfix uses port 25
  host: 'localhost',
  tls: {
    rejectUnauthorized: false
  },
});

var message = {
  from: 'noreply@domain.com',
  to: 'whatever@otherdomain.com',
  subject: 'Confirm Email',
  text: 'Please confirm your email',
  html: '<p>Please confirm your email</p>'
};

transporter.sendMail(message, (error, info) => {
  if (error) {
      return console.log(error);
  }
  console.log('Message sent: %s', info.messageId);
});

I'm current triggering this through an restful API on express.js. This should work, I used this to set up my postfix on digitalocean.

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
suckschool
  • 187
  • 2
  • 9
  • Interesting, don't you need auth credentials? Because it's localhost? – João Pimentel Ferreira Mar 27 '22 at 14:27
  • 2
    Whatever happened in 2019 it's not working here in 2022 for me ! I just tried it for a reg confirm email and got a response *Error: connect ECONNREFUSED 127.0.0.1:25 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) { errno: -111, code: 'ESOCKET', syscall: 'connect', address: '127.0.0.1', port: 25, command: 'CONN' }* – Trunk Aug 26 '22 at 11:27
  • For me it worked by using the IPv6 address ::1 for localhost – intagli Nov 16 '22 at 19:58
0

Additional to what @cheniel said: Make sure the "from" email is the same user email as the email in the transporter object. And the "to" must be a valid email and exist (check if the value is correctly set (not undefined and not null)).

nanachimi
  • 406
  • 6
  • 23
-5

Shouldn't be an issue with running from a local server.

It looks like you are using direct transport, which is described in the docs as:

...not reliable as outgoing port 25 used is often blocked by default. Additionally mail sent from dynamic addresses is often flagged as spam. You should really consider using a SMTP provider.

One solution is to define a SMTP transport:

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

There are other solutions here, such as using a Transport Plugin.

Regardless, it is tough to diagnose your issue when you are not adding callbacks to your sendMail function. When you send mail, do it like this, and then you can check the console to see what the error may be:

transporter.sendMail({
  from: contact.email,
  to: to,
  subject: contact.subject,
  text: contact.message
}, function(error, info){
    if(error){
        console.log(error);
    }else{
        console.log('Message sent: ' + info.response);
    }
});
cheniel
  • 3,473
  • 1
  • 14
  • 15
-5
var nodemailer = require('nodemailer');

// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
    service: 'Hotmail',
    auth: {
        user: "username",
        pass: "password"
    }
});

// Message object
var message = {

// sender info
from: 'name@hotmail.com',

// Comma separated list of recipients
to: req.query.to,

// Subject of the message
subject:req.query.subject, //'Nodemailer is unicode friendly ✔', 

// plaintext body
text: req.query.text //'Hello to myself!',

 // HTML body
  /*  html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
     '<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'*/
};

console.log('Sending Mail');
transport.sendMail(message, function(error){
if(error){
  console.log('Error occured');
  console.log(error.message);
  return;
}
console.log('Message sent successfully!');

 // if you don't want to use this transport object anymore, uncomment    
 //transport.close(); // close the connection pool
});

You must specify the transport protocol like that of SMTP or create youy own transport.You have not specified this in you code.  

Sir Sudo
  • 31
  • 1
  • 5
aaditya
  • 555
  • 7
  • 19
  • 2
    OP asked about sending an email from localhost but your answer is demonstrating how to send email through Hotmail! – Farzan Jul 31 '21 at 17:25
-5

In the localhost its not working because a secure and safetly connection is required for sending an email but using gmail[smtp(simple mail transfer protocol)] we can achieve it functionality.

Don't forget to first do the setting - Allow less secure apps to access account.

its given permission for access gmail account.

by default this settings is off and you simply turn it on. Now move on coding part.

****************************--------------------------------------------------------------------*********************

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
    user: 'your.gmail.account@gmail.com', // like : abc@gmail.com
    pass: 'your.gmailpassword'           // like : pass@123
}
});

let mailOptions = {
 from: 'your.gmail.account@gmail.com',
 to: 'receivers.email@domain.com',
 subject: 'Check Mail',
 text: 'Its working node mailer'
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
     return console.log(error.message);
  }
console.log('success');
}); 
  • 2
    OP asked about sending an email from localhost but your answer is demonstrating how to send email through Gmail! – Farzan Jul 31 '21 at 17:25
  • Since June 2022 Gmail won't allow external apps to send emails through its Gmail server and the option to permit this has been removed from the Gmail settings. This is a great inconvenience to those of us developing Node apps on the local machine prior to running them on the remote server. – Trunk Aug 26 '22 at 11:30