Sending AngularJS form to NodeMailer to send email

前端 未结 2 1986
北荒
北荒 2021-01-22 15:52

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 an

2条回答
  •  不要未来只要你来
    2021-01-22 15:56

    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.com',     
    "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

提交回复
热议问题