Reputation: 1329
I'm trying to send an image attachment and it's coming through, but with no data. 0kb in size. That's using this code:
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
}]
I've tried about a hundred variations on the theme based on my research online, and either I get no attachment at all, the result outlined above, or a generic attachment-1.bin. My newFileName
param is fine. The file exists in the specified directory. There are no explicit errors. I would sure love some guidance :)
update
Here are the steps leading to the wonky file attachment, from the client to the server.
The controller method:
$scope.Upload = function()
{
var file = $scope.fileModel;
var uploadUrl = '/upload';
if (formValid)
{
// Upload it
fileUpload.uploadFileToUrl(file, uploadUrl);
}
};
The service which houses the upload function (I wasn't able to find a way to submit the file with the rest of the form, so the upload is handled separately, and I am setting the value of a hidden input to the multer-renamed file name, which is submitted with the form):
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');
});
}
The form data is handled on the server by two methods, the first of which stores the params in a session and then sends the user to Paypal to complete payment, and the second of which accesses those session variables, and constructs and sends the email:
// 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
}
}
Finally, the method that constructs and sends the message - two actually, the main message, and the confirmation to the user (obviously replacing sensitive info with placeholders):
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'});
}
}
Upvotes: 1
Views: 1520
Reputation: 1329
I knew I had the right path to the image, and @hassansin pointed me in the right direction about the buffer (and andris9 of the famed nodemailer mentioned content: Buffer
in the version of this question I posted on the GitHub page. Which led me to this working version:
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)
}]
Upvotes: 1