Kselax.ru

Hacker Kselax – the best hacker in the world

Menu
  • Blog
  • Contacts
  • wp plugin generator
  • English
    • Русский
Menu
main-node.js

How to send an email in node.js

Posted on 7 July, 201828 August, 2018 by admin

For sending emails in node.js you have to use a separated module like nodemailer.

For sending a mail to localhost we have to have an installed postfix on your local machine

The code for sending an email to local postfix for testing apps

Here I’ve got an error related to the self-signed certificate for solving this we must add this line of the row to the transporter definition

1
tls: { rejectUnauthorized: false }

the full code example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
  tls: { rejectUnauthorized: false },
  // host: 'localhost', // default is localhost
  // port: 25,
  // secure: false,
  // // auth: {
  // //   neo: 'neo@neo.ru',
  // //   pass: 'ninja234'
  // // }
});
let mailOptions = {
  from: '"american-chat.ru" <american-chat@kselax.ru>', // sender address
  to: 'neo@neo.ru', // list of receivers
  subject: 'test-subject', // Subject line
  text: 'this is the text', // plain text body
  html: '<b>NodeJS Email Tutorial</b>' // html body
};
 
transporter.sendMail(mailOptions, (error, info) => {
  if(error){
      return console.log('error = ' , error);
  }
  console.log('Message %s sent: %s', info.messageId, info.response);
  res.render('index');
});

This works, after running this code we’ll get an email to the mailbox. Here we created the transporter with default settings there is the host by default have a means localhost.

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • bash (1)
  • English (9)
  • JavaScript (4)
  • node.js (22)
  • photoshop (1)
  • php (3)
  • React (9)
  • sclerotic (6)
  • Ubuntu (10)
  • Uncategorized (15)
  • Wordpress (1)

Tags

Ajax apache2 automation bash chrome-extension command line editor ejs email English English-grammar framework functions git graphql handlebars hybrid app installation javascript js linux newbie node.js node.js javascript nodemailer npm objects Performance php phpmyadmin playonlinux promise rabbitmq React react-router redis reverse-proxy session shell socket.io sublime text 3 time zones ubuntu unity webpack

Recent Comments

  • damien on How to install npm and nodejs the latest versions on ubuntu
  • Cam on How to install npm and nodejs the latest versions on ubuntu
  • Pierre on socket.io with apache as a reverse proxy on the CentOS
  • admin on How to use react-router with a few languages
  • admin on How to install npm and nodejs the latest versions on ubuntu
©2021 Kselax.ru Theme by ThemeGiant