Uncaught Error - Sending email using nodemailer and Angularjs

-1

I have been breaking head for some time trying to create a page of contacts where the user through the page can send an email. I am new with node and so I have had some problems. At the moment I can not get my application to recognize nodemailer and I'm having the following error message:

  Uncaught Error: Module name "nodemailer" has not been loaded yet for context: _. Use require ([])

Follow my code for analysis. Please help me, I have gone through several tutorials and forums but I can not solve the problem.

//CONTROLLER

(function() {
	'use strict';	
	var ContatoController = ['$scope', '$http', function($scope, $http){		
		
		$scope.formulario = {};
		$scope.formulario.nome;
		$scope.formulario.email;
		$scope.formulario.assunto;
		$scope.formulario.mensagem;		
		
		$scope.enviaEmail = function() {			
			$http({
				  method: 'POST',
				  url: '/contact-form',
				  headers: {'Content-Type': 'application/x-www-form-urlencoded'},
				  data: {
					  contactNome: $scope.formulario.nome,
			          contactEmail: $scope.formulario.email,
			          contactAssunto: $scope.formulario.assunto,
			          contactMensagem: $scope.formulario.mensagem
				  }
				})
				.success(function(data, status, headers, config) {
	        		console.log("Mensagem enviada com sucesso!");
	        	}).
	        	error(function(data, status, headers, config) {
	        		console.log("Não deu certo.");
	        	});        	
		};		
}];	

angular.module('contactForm').controller('ContatoController', ContatoController);		
})();	

//CONTROLLER-SERVER

'use strict';

var nodemailer = require('nodemailer');

app.post('/enviaEmail', function(req, res) {
	var transporter = nodemailer.createTransport({
		host: 'mail.meudominio.com',
		port: '465',
		secure: true,
		auth: {
			user: '[email protected]',
			pass: '****'
		} 
	}); 
	
	exports.enviaEmail = function(req, res) {
		var data = req.body;
		
	    transporter.enviaEmail({
	        from: data.contactEmail,
	        to: '[email protected]',
	        subject: data.contactAssunto + ' - Enviado por: ' + data.contactNome,
	        text: data.contactMensagem
	    });
	 
	    res.json(data);
	};
});

//ROUTES

'use strict';

	angular.module('contactForm')
		.exports = function(app) {
		
			var core = require('js/server/core.server.controller.js');		 
		    app.route('/contact-form').post(core.enviaEmail);
		};
    
asked by anonymous 08.10.2018 / 19:08

1 answer

0

It seems to me that you are making a confusion between the responsibilities of Angular and Node.js . NodeMailer is a package that must be run on the server side, that is, Node.js . The Angular should request the Node.js to send the email using a preestablished route.

Your route file in Node.js will look similar to the following:

const controller = require('../controller/email.controller');
const express = require('express');

const router = express.Router();

router.post('/enviarEmail', controller.enviarEmail);

module.exports = router;

And your controller that is referenced in the route will be similar to:

const { createTransport } = require('nodemailer');
const { promisify } = require('util');

const configurar = () => {
  const { sendMail } = createTransport({
    host: 'mail.meudominio.com',
    port: '465',
    secure: true,
    auth: {
      user: '[email protected]',
      pass: '****',
    }
  });

  return promisify(sendMail);
};

const enviarEmail = async (req, res) => {
  const { body: { contactEmail, contactAssunto, contactNome, contactMensagem } } = req;
  const enviar = configurar();

  const info = await enviar({
    from: contactEmail,
    to: '[email protected]',
    subject: '${contactAssunto} - Enviado por: ${contactNome}',
    text: contactMensagem,
  });

  res.json({ info, mensagem: 'E-mail enviado com sucesso.' });
};

module.exports = {
  enviarEmail,
}

Note that since your code is not executable, I only made one implementation suggestion that has no guarantee of operation, after all I could not test it. But note the changes that make clear what the server's expected responsibilities are.

    
11.10.2018 / 16:27