使用nodemailer / sendgrid上传多个文件

时间:2015-09-21 13:37:29

标签: node.js express sendgrid multer

有没有办法通过Nodemailer / Express / Multer上传多个附件到Sendgrid?我能够发送一个文件,但是使用一系列附件进行彻底测试失败了。

这是我的控制器功能,我提交文件:

    $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;
    for (var i = 0; i < files.length; i++) {
      var file = files[i];

      // Add the file to the 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 we have to add an error message e.g. "We could not send the form at this moment. Try again later"
        console.log(data);
    });
};

这是我的app.js,我尝试将文件发送到Sendgrid

// Set default node environment to 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;

显示以下错误:

TypeError: Cannot read property 'originalname' of undefined
at /Applications/MAMP/htdocs/myproject/server/app.js:51:35
at Layer.handle [as handle_request] (/Applications/MAMP/htdocs/myproject/node_modules/express/lib/router/layer.js:82:5)
at next (/Applications/MAMP/htdocs/myproject/node_modules/express/lib/router/route.js:100:13)
at done (/Applications/MAMP/htdocs/myproject/node_modules/multer/lib/make-middleware.js:36:7)
at indicateDone (/Applications/MAMP/htdocs/myproject/node_modules/multer/lib/make-middleware.js:40:51)
at /Applications/MAMP/htdocs/myproject/node_modules/multer/lib/make-middleware.js:142:11
at WriteStream.<anonymous> (/Applications/MAMP/htdocs/myproject/node_modules/multer/storage/disk.js:43:9)
at emitNone (events.js:72:20)
at WriteStream.emit (events.js:166:7)
at finishMaybe (_stream_writable.js:468:14)

1 个答案:

答案 0 :(得分:0)

感谢您的更新。现在对我来说很清楚。 您的错误意味着req.file未定义。这是正确的。 检查multer docs https://github.com/expressjs/multer

如果你使用upload.single,那么文件数据在req.file中。但是当你使用upload.array时,你的文件数据是req.files(复数形式的文件)。

要解决此问题,请将代码req.file中的更改更改为req.files,并记住这是包含文件数据的数组。

例如,第一个文件的orginalname位于req.files [0] .originalname