Nodemailer图像附件没有数据

时间:2015-06-29 01:00:37

标签: angularjs node.js 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
}]

我已经根据我的在线研究尝试了大约一百个关于这个主题的变化,要么我根本没有附件,上面列出的结果,或者通用附件-1.bin。我的newFileName param很好。该文件存在于指定的目录中。没有明确的错误。我一定会喜欢一些指导:)

更新

以下是导致从客户端到服务器的不稳定文件附件的步骤。

控制器方法:

$scope.Upload = function()
{
    var file = $scope.fileModel;
    var uploadUrl = '/upload';

    if (formValid)
    {
        // Upload it
        fileUpload.uploadFileToUrl(file, uploadUrl);
    }
};

包含上传功能的服务(我无法找到使用表单其余部分提交文件的方法,因此上传是单独处理的,我将隐藏输入的值设置为multer-renamed文件名,以表格提交:

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');
    });
}

表单数据在服务器上通过两种方法处理,第一种方法将params存储在会话中,然后将用户发送到Paypal完成付款,第二种方法访问这些会话变量,构造和发送电子邮件:

// 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'});
    }
}

1 个答案:

答案 0 :(得分:1)

我知道我有正确的图像路径,@ hassansin指出我正确的方向关于缓冲区(和我在GitHub上发布的这个问题版本中提到的content: Buffer着名的nodemailer的andris9这导致我进入这个工作版本:

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)
}]