admin管理员组文章数量:1391969
I have a built an email form in a modal instance using AngularJS that contains the field sending email, recipient email, subject, and email content. The form uses input boxes and ng-models to keep track of the data. When the user hits the send button on this form, I would like to send all of the information collected by the models to the server to send an email.
Currently, I have a draft button and when the user hits the draft button, I send all of the information collected in the models to a controller who sends it to a service so that I can repopulate the information if the user decides to close the modal instance and send the email later. However, if the user hits the send button, I have this object with all of the information but don't know how to send it to the server. I'm very new to node.js and backend in general so if you someone could provide a concrete short example for me to learn from that would be great.
I'm looking to use the nodemailer module which I have installed using npm.
Thanks!
I have a built an email form in a modal instance using AngularJS that contains the field sending email, recipient email, subject, and email content. The form uses input boxes and ng-models to keep track of the data. When the user hits the send button on this form, I would like to send all of the information collected by the models to the server to send an email.
Currently, I have a draft button and when the user hits the draft button, I send all of the information collected in the models to a controller who sends it to a service so that I can repopulate the information if the user decides to close the modal instance and send the email later. However, if the user hits the send button, I have this object with all of the information but don't know how to send it to the server. I'm very new to node.js and backend in general so if you someone could provide a concrete short example for me to learn from that would be great.
I'm looking to use the nodemailer module which I have installed using npm.
Thanks!
Share Improve this question edited Sep 2, 2014 at 21:14 es3735746 asked Sep 2, 2014 at 1:26 es3735746es3735746 8514 gold badges17 silver badges40 bronze badges2 Answers
Reset to default 4At the node side you can create a REST end point in order to recieve a POST request from the server for the form submission of email in the modal box. The REST end-point will look like this if you have an express server.
var express = require('express');
var router = express.Router();
var app = express();
app.post('/postEmail',function(req,res){
//Your NodeMailer logic es here
});
In the angular part, just like you have written a service, you can create an angular factory to invoke this REST end point.
For example:
myApp.factory('postEmailForm',['$http',function($http){
return {
postEmail: function(emailData,callback){
$http.post("/postEmail/", emailData).success(callback);
}
}
}]);
where myApp is your angular module, emailData is the emailForm data which you want to send to the server (it will be posted in the body of the request)
Angular code:
angular.module('sendmailApp', [])
.controller('MailController', function ($scope,$http) {
$scope.loading = false;
$scope.send = function (mail){
$scope.loading = true;
$http.post('/sendemail', {
to: mail.to,
subject: 'Message from AngularCode',
text: mail.message
}).then(res=>{
$scope.loading = false;
$scope.serverMessage = 'Email sent successfully';
});
}
})
Node.js code:
let express = require('express'),
app = express(),
bodyParser = require('body-parser'),
nodemailer = require("nodemailer"),
server = require('http').Server(app);
const OTP_EMAIL_CONFIG= {
"host": 'smtp.gmail.',
"port": 465,
"secure": true,
"auth": {
"user": 'enter gmail account',
"pass": 'gmail password...'
} };
let mailModule = nodemailer.createTransport(OTP_EMAIL_CONFIG);
app.use(express.static(__dirname + '/client'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/',function(req,res){
res.send('hello world!');
}) // Method to send email....
app.post('/sendemail',function(req,res){
console.log(req.body,'request');
var mailOptions = {
from: '"jsonworld " enter gmail account which you want to use for sending email',
to: req.body.to,
subject: "mail sending with angularjs and nodejs",
text: "Congratulations! mail is sending properly now."
};
mailModule.sendMail(mailOptions);
res.status(200).send('Mail sent successfully'); })
server.listen(3003,function(){ console.log('server listening on port: 3003'); });
For getting working demo go here
本文标签: javascriptSending AngularJS form to NodeMailer to send emailStack Overflow
版权声明:本文标题:javascript - Sending AngularJS form to NodeMailer to send email - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744691312a2620010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论