Sunday 28 May 2017

how to send a email using nodejs and express

this is not a question, it is a solution. i worked two days to find the correct solution and finally coded this and my mail is sent .i hope this is help full to you.

add this to app.js file

var  mailer = require('express-mailer');
  // view engine setup
 app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade'); 
mailer.extend(app, {
from: 'your mail id',
host: 'smtp.gmail.com', 
secureConnection: true,  
port: 465, 
transportMethod: 'SMTP',
 auth: {
user: 'your mail id',
pass: 'password'
}
});

app.post('/send', function (req, res, next) {
app.mailer.send('email', {
to: req.body.email, 
subject: 'mail test', 
  text: 'That was easy!',
otherProperty: 'Other Property' 
 }, function (err) {
if (err) {
  // handle error 
  console.log(err);
  res.send('There was an error sending the email');
  return;
}
res.send('Email Sent');
});
});

now add a jade file with name email.jade in your views directory with the code

doctype transitional
html
head
meta(http-equiv = 'Content-Type', content = 'text/html; charset=UTF-8')
title= subject
body=otherProperty+' /n '+text

now give the receivers email id in the body ex: {"email":"example@ex.com"} and give a post request in postman



via Srichandra Amrutaluri

No comments:

Post a Comment