In this article, I will demonstrate a simple example of sending emails using G-Mail SMTP in Express/ Node.js. fortunately sending emails in express/node js pretty easy, in node.js there are many open source email libraries are available.
In this tutorial we are going to use express-mail
library. For sending html emails we are going to use template engine called pugjs
, this project was formerly known as Jade
.
Create package.json file and keep it in any folder. Put the below code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
{ "name": "sending-emails-with-express-mailer-gmail-smtp", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "express", "mailer" ], "author": "arjunphp.com", "license": "ISC", "dependencies": { "express": "^4.14.0", "pug": "^2.0.0-beta6", "express-mailer": "^0.3.1" } } |
or
issue npm init
command to generate package.json and after generating, issue following commands to install express js, pugjs and express mailer dependencies, npm install --save pug express express-mailer
server.js
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
// call the packages we need const express = require('express'); // call express const mailer = require('express-mailer'); // call express const app = express(); // create a server const port = process.env.PORT || 8000; // set our port // set the view folder to views app.set('views', __dirname + '/views'); // set the view engine to pug app.set('view engine', 'pug'); // Configure express-mail and setup default mail data. mailer.extend(app, { host: 'smtp.gmail.com', // hostname secureConnection: true, // use SSL port: 465, // port for secure SMTP transportMethod: 'SMTP', // default is SMTP. Accepts anything that nodemailer accepts auth: { pass: 'yourPassword' // gmail password } }); // test route to trigger emails app.get('/', function (req, res) { // Setup email data. var mailOptions = { subject: 'Email from SMTP sever', user: { // data to view template, you can access as - user.name name: 'Arjun PHP', message: 'Welcome to arjunphp.com' } } // Send email. app.mailer.send('email', mailOptions, function (err, message) { if (err) { console.log(err); res.send('There was an error sending the email'); return; } return res.send('Email has been sent!'); }); }); app.listen(port, function () { console.log(`Example app listening on port ${port}!`); }); |
views/email.pug
1 2 3 4 5 6 7 8 |
doctype html head body div p b Hi #{user.name} div p #{user.message} |
Run the app with the following command:
1 |
$ node app.js |
Then, load http://localhost:8000/
in a browser to see the output.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.
what is the limit of sending mails per day?
approximately 100-150 emails per day
i have this problem when im trying to run the code
{ Error: connect ETIMEDOUT 108.177.15.108:465
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: ‘ETIMEDOUT’,
errno: ‘ETIMEDOUT’,
syscall: ‘connect’,
address: ‘108.177.15.108’,
port: 465,
stage: ‘init’ }
and on browser
There was an error sending the email