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.