Nodemailer图像附件没有数据

我试图发送一个图像附件,它正在通过,但没有数据。 大小为0kb。 这是使用这个代码:

var path = require('path'); var uploadDir = path.dirname(require.main.filename); // This actually produces a filepath that starts at my /Applications dir on my Mac, but ends in the right spot. var img = require("fs").readFileSync(uploadDir + '/uploads/' + params.newFileName); // file was upload was handled on the server by multer and renamed to something like this: fc5c5921bd0892f114cd7b6f0d39d9a3.gif attachments: [{ filename: params.newFileName, filePath: img }] 

我基于在线研究尝试了大约一百个关于主题的变体,或者我根本没有获得任何附件,上面列出的结果,或者一个通用的attachment-1.bin。 我的newFileName参数是好的。 该文件存在于指定的目录中。 没有明确的错误。 我肯定会爱一些指导:)

更新

下面是从客户端到服务器的导致不可靠文件附件的步骤。

控制器方法:

 $scope.Upload = function() { var file = $scope.fileModel; var uploadUrl = '/upload'; if (formValid) { // Upload it fileUpload.uploadFileToUrl(file, uploadUrl); } }; 

包含上传function的服务(我无法find一种方式来提交与表单的其余部分,所以上传是分开处理的,我设置一个隐藏的input的值更名为multer文件名,与表格一起提交):

 uploadFileToUrl: function(file, uploadUrl) { var fd = new FormData(); fd.append('file', file); $http.post(uploadUrl, fd, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }) .success(function (res) { toastr.success(res.successMessage, 'Success'); $('#file-name').val(res.fileName) $('#file-name').trigger('input'); }) .error(function() { toastr.error(res.errorMessage, 'Error'); }); } 

表单数据在服务器上通过两种方式处理,第一种将参数存储在会话中,然后将用户发送到PayPal完成支付,第二个访问这些会话variables,并构造和发送电子邮件:

 // NOTE: leaving out the paypal code as it is not relevant CompletePayment: function(req, res) { // Get the request params var params = req.body; // Store request params in session variables so we can send // confirmation emails once payment has been completed req.session.requestFormData = { mediaType: params.mediaType, name: params.name, email: params.email, dedication: params.dedication, notes: params.notes, socialMedia: params.socialMedia, newFileName: params.newFileName } } 

最后,构造和发送消息的方法 – 实际上,主要消息和对用户的确认(明显地用占位符replace敏感信息):

 SendRequestEmail: function(req, res) { var params = req.session.requestFormData; // I split the nodemailer SMTP transport off into a service var transport = EmailService.SetupTransport(); if (params) { var mailOptions = { to: 'test@test.com', from: 'FromName <'+params.email+'>', subject: 'Request from ' + params.name, text: 'Someone has requested something! Get back to ' + params.name + ' right away!' + '\n\nType: ' + params.mediaType + '\nName: ' + params.name + '\nEmail: ' + params.email + '\nDedication: ' + params.dedication + '\nNotes: ' + params.notes + '\nCan We Post It: ' + params.socialMedia, attachments: [{ filename: params.newFileName, // NOTE: I changed the path here to one that should work in production - deployed and got the same result path: '/server/uploads/' }] }; var confirmationMailOptions = { // A much simpler version of above - no attachments, just text } // Send the request email transport.sendMail(mailOptions, function(error, info) { if(error){ res.send({ nodemailerError: error, errorMessage: 'Oops! Either the email address you entered doesn\'t exist, or the interwebs are misbehaving. Try again!' }); } else { // If booking request was successful, send the confirmation to the user transport.sendMail(confirmationMailOptions, function(error, info) { if (error) { console.log(error); } else { res.send({ successMessage: 'Confirmation message sent to: ' + params.email }); req.session.destroy(); } }); } }); } else { res.send({paramStatus: 'destroyed'}); } } 

我知道我有正确的path图像,并@hassansin指出我正确的方向关于缓冲区(和着名的nodemailer andris9提到的content: Buffer在这个问题的版本我张贴在GitHub页面。到这个工作版本:

 var path = require('path'); var uploadDir = path.dirname(require.main.filename); var img = require("fs").readFileSync(uploadDir + '/uploads/' + params.newFileName); attachments: [{ filename: params.newFileName, content: new Buffer(img) }]