I am using an angular controller to send an http post request to my express route and then data is being sent to my gmail client using nodemailer. The $http request works and I do receive emails in my gmail clients however when I try to log information to my console or create javascript alerts nothing happens. Ideally when a user sends an email through the contact form I would like two alerts. 1.) email is being sent and 2.) email has been sent! The problem is that the callback functions in angular won't allow me to do so? any ideas? Cheers!
/* server.js */
var express = require('express');
var app = express();
var http = require('http');
var httpServer = http.Server(app);
var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');
app.use(bodyParser.json({}));
app.use(express.static(__dirname + '/public'));
app.use('/', express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
var server = app.listen(9000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'solarcode94@gmail.com',
pass: '********'
}
});
app.route('/').post(function(req, res) {
var data = req.body;
transporter.sendMail({
from: data.contactEmail,
sender: data.contactEmail,
to: 'solarcode94@gmail.com',
subject: 'Message from ' + data.contactName,
text: data.contactEmail + 'swws' + data.contactMsg
});
});
<!-- index.html -->
<!DOCTYPE html>
<html lang="en" ng-app="mailingApp">
<head>
<meta charset="UTF-8">
<title>nodejs mailing</title>
<script src="http://code.angularjs.org/1.2.13/angular.js"></script>
<script src="node-email-controller.js"></script>
</head>
<body ng-controller="mailingCtrl">
<form name="contactForm" data-ng-submit="sendMail()" novalidate>
Name: <input type="text" data-ng-model="contactName">
Email: <input type="text" data-ng-model="contactEmail">
Message: <textarea ng-model="contactMsg" columns="1" required></textarea>
<button type="submit">Send</button>
</form>
</body>
</html>
/* angular controller */
var mailingApp = angular.module('mailingApp', []);
mailingApp.controller('mailingCtrl', ['$scope', '$http', function($scope, $http){
$scope.sendMail = function(){
var data = ({
contactName : this.contactName,
contactEmail : this.contactEmail,
contactMsg : this.contactMsg
});
$http.post('/', data).
then(function(response) {
}, function(response) {
});
};
}]);
If you are familiar with nodemailer or express sometimes I end up receiving two emails per form submission and have no idea how to solve that. Thanks!
First, this needs some kind of a response returned:
app.route('/').post(function(req, res) {
var data = req.body;
transporter.sendMail({
from: data.contactEmail,
sender: data.contactEmail,
to: 'solarcode94@gmail.com',
subject: 'Message from ' + data.contactName,
text: data.contactEmail + 'swws' + data.contactMsg
});
});
Second, you could do the alerts like this:
$scope.sendMail = function(){
var data = ({
contactName: this.contactName,
contactEmail: this.contactEmail,
contactMsg: this.contactMsg
});
alert('Sending the email');
$http.post('/', data).then(function(response) {
// provided that the `transporter.sendMail` returned a good response
alert('The email was sent');
}, function(response) {
// provided that the `transporter.sendMail` returned a bad response
alert('The email did not send');
});
};