node file attachment from $http post is not recognized (nodemailer/angular)

I have researched some of the other methods recommending file attachment to be included WITH a form (including File Upload using AngularJS) however, none seem to help with how to handle the file in node.

The email content sends successfully using nodemailer and even indicates the attachment in the email client where the email is received, but the file itself is not there in the email client (see screenshots below): As I step through the front-end code, it seems correct: node file attachment from $http post is not recognized (nodemailer/angular) node file attachment from $http post is not recognized (nodemailer/angular)

I went the route of using an Angular Directive:

html

<form class="col s12" ng-submit="postCareers(careerData)">
  <input id="email" type="email" class="validate form-career-input-field" aria-required="true" ng-model="careerData.email">
  // lots of other <inputs>
  <input type="file" file="file" aria-required="true">
</form>

app.js:

    .directive('file', function () {
        return {
            scope: {
                file: '='
            },
            link: function (scope, el, attrs) {
                el.bind('change', function (event) {
                    var file = event.target.files[0];
                    scope.file = file ? file : undefined;
                    scope.$apply();
                });
            }
        };
    })

controller.js

    $scope.$on("fileSelected", function (event, args) {
        $scope.$apply(function () {
            //add the file object to the scope's files collection
            $scope.files.push(args.file);
        });
    });

    $scope.postCareers = function (careerData) {
        var req = {
            method: 'POST',
            url: '/api/career_submit',
            headers: {
                'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
            },
            data: {
                model: $scope.postData,
                file: $scope.file
            }
        };

        $http(req)
            .then(function successCallback(response) {
                //do something after success
            }, function errorCallback(response) {
                //do something after error
            });
    };

I cannot get the form to submit if the headers are just 'Content-Type': undefined

Here is the node.js/nodemailer code routes.js

var apiRouter = require('express').Router();
var express = require('express');
var fs = require('fs');
var safeKey = JSON.parse(fs.readFileSync('./safekey.json', 'utf-8'));
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    pool: true,
    host: safeKey.emailHost,
    port: safeKey.emailPort,
    secure: true,
    auth: {
        user: safeKey.emailUser,
        pass: safeKey.emailPass
    }
});
apiRouter.post('/api/career_submit', function (req, res) {
    var data = req.body.model;
    var file = req.body.file;
    console.log(req.body); // see below for terminal log
    var mailOptions = {
        from: data.email,
        to: '[email protected]',
        subject: '[INQUIRY FORM] | ' + data.position,
        text: 'You have receieved and email from ' + data.first + ' ' + data.last + ' in reference to the open position on your Careers page, for the ' + data.position + ' position.  See attachment for resume/cover letter.  Details:  Name: ' + data.first + ' ' + data.last + ', Application Position: ' + data.position + ', E-mail: ' + data.email + ', Phone: ' + data.phone + ', Address: ' + data.address + '.  They are interested in this position because: ' + data.message + '.',
        attachments: [{
            filename: file.name
        }]
    };

    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            console.log(error);
            res.json(
                { 
                    info: 'Email not sent'
                }, {
                    error: error
                });
        } else {
            console.log('Message sent: ' + info.response);
            console.log('Data sent:' + data);
            res.json(
                {
                    success: 'Email has been sent.'
                }, {
                    response: info.response
                }, {
                    data: data
                });
        }
    });
});

terminal log:

{ model: 
   { first: 'fdf',
     last: 'Lucks',
     email: '[email protected]',
     phone: '123456',
     address: 'df',
     position: 'Account Manager,
     message: 'df' },
  file: {} } // << this is empty but above in the req it's is there.
Message sent: 250 2.0.0 Ok: queued as B179020082
Data sent:[object Object]

I am hoping it's something small that I am missing. I appreciate any info!!

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image