我试图发送图像附件,它正在通过,但没有数据。大小为0kb。这是使用此代码:Nodemailer图像附件没有数据
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。完事。我的newFileName
param很好。该文件存在于指定的目录中。没有明确的错误。我肯定会喜欢一些指导:)
更新
这里是通往靠不住文件附件中的步骤,从客户机到服务器。
控制器方法:
$scope.Upload = function()
{
var file = $scope.fileModel;
var uploadUrl = '/upload';
if (formValid)
{
// Upload it
fileUpload.uploadFileToUrl(file, uploadUrl);
}
};
里面有上传功能(我无法找到一种方法,用表格的其余部分提交的文件,因此上传单独处理的服务,我是一个隐藏输入的值设置为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以完成p ayment,并且其中第二个访问这些会话变量,并且构造并发送电子邮件:
// 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
}
}
最后,构建并发送该消息的方法 - 2实际上,主要消息,并且向用户确认(很明显,占位符代替敏感信息):
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: '[email protected]',
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'不'filePath':https://github.com/andris9/Nodemailer#attachments – hassansin
我发现使用这两种属性名的人,所以我两个都试过。同样的结果。 – MyCompassSpins
我也尝试使用相对文件路径而不是'fs'。 – MyCompassSpins