Uncaught Error - Sending email using nodemailer and Angularjs

Asked

Viewed 67 times

-1

I have been racking my brain for some time trying to create a contact page where the user can send an email. I’m new to Ode, and that’s why I’ve been through some trouble. At the moment I can’t get my application to recognize the nodemailer and I’m getting the following error message:

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

Follow my code to analyze. Please help me, I’ve been through several tutorials and forums but I can’t 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);
		};

  • What is your version of Node.js? You can check the version using the command node -v in his prompt

  • The version is V8.12.0.

  • Have you tried to reinstall your Node.js?

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

1 answer

0

It seems to me you’re making a mess between the responsibilities of Angular and of Node.js. The NodeMailer is a package that must be executed on the server side, i.e., by Node.js. The Angular must request the Node.js send the email, making use of a pre-established route.

Your file of routes on 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 his controller that is referenced on 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 as your code is not executable I just made an implementation suggestion that has no guarantees of working, after all I could not test it. But note the changes that make clear what the expected responsibilities of the server.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.