我試圖發送圖像附件,它正在通過,但沒有數據。大小爲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