node.js - Uploading multiple files with nodemailer/sendgrid -
is there way upload multiple attachments sendgrid nodemailer/express/multer? i'm able send single file thoroughly testing array of attachments failed.
this controller function submit files:
$scope.submitemail = function(email) { var formdata = new formdata(); $scope.email.to = $scope.email_employer; formdata.append('from', $scope.email.from); formdata.append('to', $scope.email.to); formdata.append('subject', $scope.email.subject); formdata.append('text', $scope.email.text); var fileselect = document.getelementbyid('file-select'); var files = fileselect.files; (var = 0; < files.length; i++) { var file = files[i]; // add file request. formdata.append('attachment', file, file.name); } /* object.keys($scope.email).foreach(function(key) { formdata.append(key, $scope.email[key]); }); */ $http.post('/api/email', formdata, { transformrequest: angular.identity, headers: {'content-type': undefined} }).then(function(data, status) { console.log("sent ok"); }, function(data, status) { //here have add error message e.g. "we not send form @ moment. try again later" console.log(data); }); };
this app.js try send file sendgrid
// set default node environment development process.env.node_env = process.env.node_env || 'development'; var express = require('express'); var config = require('./config/environment'); var http = require('http'); var bodyparser = require('body-parser'); var multer = require('multer'); var upload = multer({ dest: './uploads/' }); //var upload = multer(); var nodemailer = require('nodemailer'); var sgtransport = require('nodemailer-sendgrid-transport'); var options = { auth: { api_user: '##########', api_key: '###########' } } var mailer = nodemailer.createtransport(sgtransport(options)); // setup server var app = express(); var server = require('http').createserver(app); require('./config/express')(app); require('./routes')(app); app.post('/api/email', upload.array('attachment', 4), function(req, res, next) { console.log(req.file); var mailoptions = { to: req.body.to, from: req.body.from, subject: req.body.subject, text: req.body.text, attachments: [ { filename: req.file.originalname, path: req.file.path } ] }; mailer.sendmail(mailoptions, function(err, res) { if (err) { console.log(err); } console.log(res); }); }); // start server server.listen(config.port, config.ip, function () { console.log('express server listening on %d, in %s mode', config.port, app.get('env')); }); // expose app exports = module.exports = app;
the following error shown:
typeerror: cannot read property 'originalname' of undefined @ /applications/mamp/htdocs/myproject/server/app.js:51:35 @ layer.handle [as handle_request] (/applications/mamp/htdocs/myproject/node_modules/express/lib/router/layer.js:82:5) @ next (/applications/mamp/htdocs/myproject/node_modules/express/lib/router/route.js:100:13) @ done (/applications/mamp/htdocs/myproject/node_modules/multer/lib/make-middleware.js:36:7) @ indicatedone (/applications/mamp/htdocs/myproject/node_modules/multer/lib/make-middleware.js:40:51) @ /applications/mamp/htdocs/myproject/node_modules/multer/lib/make-middleware.js:142:11 @ writestream.<anonymous> (/applications/mamp/htdocs/myproject/node_modules/multer/storage/disk.js:43:9) @ emitnone (events.js:72:20) @ writestream.emit (events.js:166:7) @ finishmaybe (_stream_writable.js:468:14)
thank update. clear me. error means req.file undefined. , correct. check multer docs https://github.com/expressjs/multer:
if use upload.single file data in req.file. when use upload.array then, files data in req.files (plural form of file).
to fix problem change in code req.file req.files , remember array files data.
for example orginalname of first file in req.files[0].originalname
Comments
Post a Comment