Angularjs with Nodemailer contact form

Step 1- Create an AngularJS form in a view
Step 2- In your AngularJS controller, use a $http request to send your data to your Express Route

'use strict';
angular.module('core').controller('ContactFormController', ['$scope', '$http', '$mdToast', '$animate',
    function($scope, $http, $mdToast, $animate) {
// Expose view variables
        $scope.toastPosition = {
            bottom: false,
            top: true,
            left: false,
            right: true
        };
        $scope.getToastPosition = function () {
            return Object.keys($scope.toastPosition)
                .filter(function (pos) {
                    return $scope.toastPosition[pos];
                })
                .join(' ');
        };
        this.sendMail = function () {
            var data = ({
                contactName : this.contactName,
                contactEmail : this.contactEmail,
                contactMsg : this.contactMsg
            });
            // Simple POST request example (passing data) :
            $http.post('/contact-form', data).
                success(function(data, status, headers, config) {
                    // this callback will be called asynchronously
                    // when the response is available
                    $mdToast.show(
                        $mdToast.simple()
                            .content('Thanks for your message ' + data.contactName + ' You Rock!')
                            .position($scope.getToastPosition())
                            .hideDelay(8000)
                    );
                }).
                error(function(data, status, headers, config) {
                    // called asynchronously if an error occurs
                    // or server returns response with an error status.
                });
        };
    }
]);

Step 3- ExpressJS Route as an API to call

'use strict';
module.exports = function(app) {
    // Root routing
    var core = require('../controllers/core.server.controller');
    app.route('/contact-form').post(core.sendMail);
};
 Step 4 - Send an email containing the data

'use strict';
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport();
/**
 * Send an email
 */
exports.sendMail = function(req, res) {
    var data = req.body;
    transporter.sendMail({
        from: data.contactEmail,
        to: 'jai@gmail.com',
        subject: 'Message from ' + data.contactName,
        text: data.contactMsg
    });
    res.json(data);
};


Share this

Related Posts

Previous
Next Post »

Pageviews from the past week