Nodemailer 图片附件没有数据
Nodemailer Image Attachment Has No Data
我正在尝试发送图像附件,它正在通过,但没有数据。大小为 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
}]
根据我在网上的研究,我已经尝试了大约一百种主题变体,要么我根本没有得到附件,即上面列出的结果,要么得到一个通用的附件 -1.bin。我的 newFileName
参数没问题。该文件存在于指定目录中。没有明显的错误。我肯定会喜欢一些指导:)
更新
以下是导致从客户端到服务器的不稳定文件附件的步骤。
控制器方法:
$scope.Upload = function()
{
var file = $scope.fileModel;
var uploadUrl = '/upload';
if (formValid)
{
// Upload it
fileUpload.uploadFileToUrl(file, uploadUrl);
}
};
包含上传功能的服务(我找不到将文件与其余表单一起提交的方法,因此上传是单独处理的,我正在设置隐藏输入的值与表单一起提交的多次重命名文件名):
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');
});
}
表单数据在服务端有两种处理方式,第一种是将参数保存在session中,然后将用户发送到Paypal完成支付,第二种是访问那些session变量,构造并发送电子邮件:
// 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
}
}
最后,构造和发送消息的方法 - 实际上是两个,主要消息和对用户的确认(显然用占位符替换敏感信息):
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'});
}
}
我知道我有正确的图像路径,@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)
}]
我正在尝试发送图像附件,它正在通过,但没有数据。大小为 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
}]
根据我在网上的研究,我已经尝试了大约一百种主题变体,要么我根本没有得到附件,即上面列出的结果,要么得到一个通用的附件 -1.bin。我的 newFileName
参数没问题。该文件存在于指定目录中。没有明显的错误。我肯定会喜欢一些指导:)
更新
以下是导致从客户端到服务器的不稳定文件附件的步骤。
控制器方法:
$scope.Upload = function()
{
var file = $scope.fileModel;
var uploadUrl = '/upload';
if (formValid)
{
// Upload it
fileUpload.uploadFileToUrl(file, uploadUrl);
}
};
包含上传功能的服务(我找不到将文件与其余表单一起提交的方法,因此上传是单独处理的,我正在设置隐藏输入的值与表单一起提交的多次重命名文件名):
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');
});
}
表单数据在服务端有两种处理方式,第一种是将参数保存在session中,然后将用户发送到Paypal完成支付,第二种是访问那些session变量,构造并发送电子邮件:
// 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
}
}
最后,构造和发送消息的方法 - 实际上是两个,主要消息和对用户的确认(显然用占位符替换敏感信息):
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'});
}
}
我知道我有正确的图像路径,@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)
}]