planacte planacte - 4 months ago 101
AngularJS Question

Attach file to the Form with Nodemailer and Angular

I followed this stackoverflow guidance to attach a file to the form. But keep getting an Internal error 500 and Error 400. I couldn't find any tutorial with similar task. If you can accompany me, I would be very grateful!


Server.js


var mailOptions = {
from: data.contactFrom, // sender address
contactEmail: data.contactEmail,
to: 'somebody@gmail.com', // list of receivers
subject: "Request for a Quote from " + data.contactName, // Subject line
contactMsg: data.contactMsg, // plaintext body
attachments: [{ filename: req.file.originalname, content: req.file.buffer]};

console.log(mailOptions)

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){ return console.log(error); }
console.log('Message sent: ' + info.response);
res.json(info);
});



Client.js


app.controller('QuoteCtrl', ['$scope','$http',
function($scope, $http) {

$scope.message = {};
$scope.sendMail = function () {

// //Include the file in AJAX request
var formData = new FormData();
Object.keys($scope.message).forEach(function(key) {
formData.append(key, $scope.message[key]);
});

$http.post('/send/sendQuote', formData, $scope.message, {
transformRequest: angular.identity, headers: {'Content-Type': undefined}}).
success(function(data) {
$scope.message = data;
});

$scope.message = {};
}


}]);

What are possible errors and any improvements to make life easier?

Answer

Here what worked form me. I used FormData including input file and req.body values.

Server.js

router.post('/send', upload.array('attachment', 4), 
function(req, res, next) {

    //attaching files
    var attachementList = [];
    for (var i = 0; i < req.files.length; i++) {
        attachementList.push({
            filename: req.files[i].originalname,
            path: req.files[i].path
        });
    }

    var mailOption = {...}

    $http.post('/send/sendVoice', 
        formData, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
    },  
        $scope.application).
    success(function(data) {
        $scope.application = data;
  });
    //clearing inputs 
    $scope.application = {};
    angular.element("input[type='file']").val(null); }

Client.js

var formData = new FormData();

    //select input file
    var fileSelect = document.getElementById('file-select');
    var files = fileSelect.files;
    for(var i =0; i<files.length; i++) {
        var file = files[i];

        formData.append('attachment', file, file.name);
    }
    formData.append('bday', $scope.application.bday);

          console.log($scope.application);

        $http.post('/send/sendVoice', 
            formData, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        },  
        $scope.application).
        success(function(data) {
           $scope.application = data;
      });