Email sending to client is requirment for every morden project. Sending email from node js application with template is most common feature now a days.In this project, i have build functionality which will help the developer to configure the email sending functionality with template very easily. I have use the express js
and nodemailer
and my own custom mail configuration to sending the email through gmail smtp
and shared hosting email smtp
.
- nodemailer
- nodemailer-express-handlebars
- dotenv
- Clone the Git repository git clone https://github.com/tariqulislam/express-email-project
- Run command: For npm
npm install
for yarnyarn install
There is a .env
file at root of the project, you just change the environment variable for email server
###For Gmail configuration
GMAIL_SERVICE_NAME = gmail # service name
GMAIL_SERVICE_HOST = smtp.gmail.com # service host name
GMAIL_SERVICE_SECURE = false # Service security
GMAIL_SERVICE_PORT = 587 # service port
GMAIL_USER_NAME = <email> # email address
GMAIL_USER_PASSWORD = <password> # email address password
<h1>You Node mailer is working</h1>
<p>
Your Name: {{name}}
<br/>
Your address: {{address}}
<br/>
Your Email: {{email}}
</p>
For testing purpose, we will create get
request for sending the email through Gmail
smtp
- Turn on the less secure app from this link https://myaccount.google.com/lesssecureapps
Then add the express-nodmailer-handlebars
to routes/index.js
and email.js
file from config->email.js
routes/index.js
var MailConfig = require('../config/email');
var hbs = require('nodemailer-express-handlebars');
var gmailTransport = MailConfig.GmailTransport;
router.get('/email/template', (req, res, next) => {
MailConfig.ViewOption(gmailTransport,hbs);
let HelperOptions = {
from: '"Tariqul islam" <tariqul.islam.rony@gmail.com>',
to: 'tariqul@itconquest.com',
subject: 'Hellow world!',
template: 'test',
context: {
name:"tariqul_islam",
email: "tariqul.islam.rony@gmail.com",
address: "52, Kadamtola Shubag dhaka"
}
};
gmailTransport.sendMail(HelperOptions, (error,info) => {
if(error) {
console.log(error);
res.json(error);
}
console.log("email is send");
console.log(info);
res.json(info)
});
});
After that add html template support to when sending the email we will call the MailConfig.viewOption()
function at express router get
request function at routes/index.js
file.
let HelperOptions = {
from: '"Tariqul islam" <tariqul.islam.rony@gmail.com>',
to: 'tariqul@itconquest.com',
subject: 'Hellow world!',
template: 'test',
context: {
name:"tariqul_islam",
email: "tariqul.islam.rony@gmail.com",
address: "52, Kadamtola Shubag dhaka"
}
};
HelperOptions
object is simple configuration object for email templating
form
value will be, from which email address sends the email (sender name)to
value will be, Receiver email addresssubject
email subjecttemplate
this will be .hbs template which will be create in ``views/email/``` foldercontext
will the arguments or paramter to send the dynamic value to template
gmailTransport.sendMail(HelperOptions, (error,info) => {
if(error) {
console.log(error);
res.json(error);
}
console.log("email is send");
console.log(info);
res.json(info)
});
gmailTransport.sendMail
is the nodemailer
default function to send email to sender address,
it takes two arguments:
- HelperOptions (simple template configuration object)
error_first_callback
function which contains (error, success) arguments
- From command line or cmd run command: for npm
npm run start
for yarnyarn start
- Hit the url at
postman
withget
request http://localhost:3000/email/template - the result will be:
For testing purpose, we will create get
request for sending the email through own smtp server
- Go to Email account sections
- Select Email and you will be option
email client configuration
- Then get the shared hosting smtp account mail server information for account
- After that configure the smtp email to
.env
file:
SMTP_SERVICE_HOST=<smtp host name>
SMTP_SERVICE_SECURE=<conection is secure or not>
SMTP_SERVICE_PORT=<smtp port>
SMTP_USER_NAME=<email Address>
SMTP_USER_PASSWORD=<password>
router.get('/email/smtp/template', (req, res, next) => {
MailConfig.ViewOption(smtpTransport,hbs);
let HelperOptions = {
from: '"Tariqul islam" <tariqul@falconfitbd.com>',
to: 'tariqul.islam.rony@gmail.com',
subject: 'Hellow world!',
template: 'test',
context: {
name:"tariqul_islam",
email: "tariqul.islam.rony@gmail.com",
address: "52, Kadamtola Shubag dhaka"
}
};
smtpTransport.verify((error, success) => {
if(error) {
res.json({output: 'error', message: error})
res.end();
} else {
smtpTransport.sendMail(HelperOptions, (error,info) => {
if(error) {
res.json({output: 'error', message: error})
}
res.json({output: 'success', message: info});
res.end();
});
}
})
});
- Configure the email template like
gmailTransport
by callingMailConfig.viewOption()
methodMailConfig.ViewOption(smtpTransport,hbs);
- Configure the email template Helper Option Object to sending the email through smtp email:
let HelperOptions = {
from: '"Tariqul islam" <tariqul@falconfitbd.com>',
to: 'tariqul.islam.rony@gmail.com',
subject: 'Hellow world!',
template: 'test',
context: {
name:"tariqul_islam",
email: "tariqul.islam.rony@gmail.com",
address: "52, Kadamtola Shubag dhaka"
}
};
HelperOptions
object is simple configuration object for email templating
form
value will be, from which email address sends the email (sender name)to
value will be, Receiver email addresssubject
email subjecttemplate
this will be .hbs template which will be create in ``views/email/``` foldercontext
will the arguments or paramter to send the dynamic value to template
smtpTransport.verify((error, success) => {
if(error) {
res.json({output: 'error', message: error})
res.end();
} else {
smtpTransport.sendMail(HelperOptions, (error,info) => {
if(error) {
res.json({output: 'error', message: error})
}
res.json({output: 'success', message: info});
res.end();
});
}
})
smtpTransport.verify()
method will check the smtp sever connection is valid and ping to server to all is okay for serversmtpTransport.sendMail
is work likegmailTransport.sendMail
function
- From command line or cmd run command: for npm
npm run start
for yarnyarn start
- Hit the url at
postman
withget
request http://localhost:3000/email/smtp/template - the result will be: