Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email from local host with Nodemailer

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: '[email protected]'};
    var to = "[email protected]";
    var transporter = nodemailer.createTransport();
    transporter.sendMail({
      from: contact.email,
      to: to,
      subject: contact.subject,
      text: contact.message
    });
like image 949
tonymx227 Avatar asked Aug 28 '15 06:08

tonymx227


People also ask

Does Nodemailer work on localhost?

Nodemailer works on local PC, but not on server.

How do I send email from node mailer?

Include the nodemailer module in the code using require('nodemailer'). Use nodemailer. createTransport() function to create a transporter who will send mail. It contains the service name and authentication details (user ans password).

How do I use SMTP with Nodemailer?

For more details, refer to the Nodemailer documentation. With SMTP, everything is pretty straightforward. Set host, port, authentication details and method, and that's it. It's also useful to verify that SMTP connection is correct at this stage: add verify(callback) call to test connection and authentication.

Can we send HTML with Nodemailer module?

In this tutorial, we'll cover how to configure an email address for Nodemailer, send emails from inside of an application, attach files to an email, add fields for CC and BCC, and finally, add CSS and HTML styling to an email template.


1 Answers

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

var message = {
  from: '[email protected]',
  to: '[email protected]',
  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.

like image 54
suckschool Avatar answered Sep 23 '22 11:09

suckschool