How to send email with attachment using angular and node js or in mean stack

Today i am working on to send email using angular and node js. After googling alot finally i found a node module Node Mailer . Its a very nice module of node you can also send email via smtp by doing a very small configuration. In my below code i am not using smtp but if you need then can configuration setting according to the node mailer documentation.


Below is the steps to send email :


Step 1 : First of all you have to install the node mailer module. To do this just write  

npm install nodemailer

Step 2 : After that you have to include the node mailer libraries like below

var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport();


Step 3 : Now this is my code to send email of my index.js node server file.

// This Is Use To Send Email.
app.post('/sendemail',function(req,res){
var attachment = (typeof req.body.attachment !="undefined") ? req.body.attachment : '';
var mailOptions = {
  from: "salman@phpsollution.blogspot.com",
  to: req.body.to,
  subject: req.body.subject,
  html: req.body.text,
     attachments:[{path: '/var/www/FAIRQUID/client/app/loanpdf/'+ attachment}]

}

smtpTransport.sendMail(mailOptions, function(error, info){
  if(error){
res.json({status: 'error'});
}else{
res.json({status: 'success'});
};
});
});


Step 4 : Now this is my http request code from angular js

var mailData = {
  to: 'naved.nice@gmail.com',
  subject: 'testing email',
  text: 'This is just a testing email send by the phpsollution.blogspot.com',
    attachment : pdfName
}

$http.post('/sendemail', mailData).success(function(data){
if(data.status == 'success'){
console.log("Email send successfully");
}
else{
console.log("Some error occour while sending email");
}
});


If you have any problem while using this code please comment below i will reply you back as soon as possible.

Chears 
Happy Coding :)

2 Comments

  1. Thanks, Salman for very helpful solution and post....

    ReplyDelete
  2. Where does the sendemail from the app.post and http.post camefrom?? and whats the use of it thank you :)

    ReplyDelete
Previous Post Next Post