Send email using angularjs and nodejs nodemailer module
Published: 2023-09-22 10:20:11

Hey guys, In this tutorial we are going to send email using angularjs and nodejs nodemailer module. Since email verification is used for registration process, forgot password case etc. We are going to discuss mainly nodemailer module since I expect you are aware of express.js.

Have a look on folder structure:

``````client
    app.js
    index.html
package-lock.json
package.json
server.js``````

In our html page(index.html), We have  a form for input sending email and the message of the email and at the bottom of the page we have included angularjs file and included our angular app. Let's see the code of angularjs code(**app.js**).

``````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';
        });
    }
 
})``````

We have almost completed the frontend relates task in this application. Let's move to server end(nodejs). Now create **package.json** and put the below code.

``````{
    "name": "send_email",
    "version": "1.0.0",
    "description": "send email using angularjs and nodejs",
    "main": "server.js",
    "scripts": {
        "test": "node server.js",
        "start": "node server.js"
    },
    "author": "Suraj Roy",
    "license": "ISC",
    "dependencies": {
        "body-parser": "^1.18.2",
        "express": "^4.16.3",
        "nodemailer": "^4.6.4"
 
    }
 
}``````

In the above file we have package required for the application. Now we create server.js file which is actually responsible for sending the email with nodemailer package.

``````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'); });
 ``````

In the above file(server.js), at the top we have required the modules required. Below that we have set the constant in the **OTP_EMAIL_CONFIG.**  And below that we have definded Mail transport system(SMTP) so that from that E-mail box our e-mail will be sent.  at the bottom we have a method **sendemail** method where we have passed all the basic details like subject, from, to, message. And finally mail send with **mailModule.sendEmail(mailOptions);**

Thats it for now.  If you have any query, send email to us at : **demo.jsonworld@gmail.com**

This article is originally posted over [jsonworld](jsonworld.com)