SENDING MAIL WITH NODEJS

Why would you do so ?

Obviously because it’s extremely cool to send mail through server (no wonders I only have 1.7 friends)

I have always had a non healthy fascination with anything that simulates online presence.

So, faking web session, sending emails, text messages from a remote server using a simple command line has always attracted me. Yes I know it’s weird.

Anyways, for the rest of the world reading this article, sending an email from a server would be useful for instance after a user purchase a good on a website, after a customer inquiry etc..

Ok so here is the process (dead simple)

  1. Install the dependency
npm install nodemailer --save

2. Make a mail

'use strict';
const nodemailer = require('nodemailer');

// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});

// setup email data with unicode symbols
let mailOptions = {
from: '"Fred Foo 👻" <foo@blurdybloop.com>', // sender address
to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));

// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@blurdybloop.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});

And then you just need to link that to a command line (too lazy to write it here) to send an email like a badass.

Like what you read? Give Yonathan Meguira a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.