1

<!-- Server.js file of Node JS-->

var http = require('http');
var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');

var connect = require('connect');

var app = connect();
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
 extended: true
}));
sendemail = function(request, response, next){
 var transporter = nodemailer.createTransport({
  service: 'gmail',
    auth: {
      user: 'senderid@gmail.com',
      pass: 'senderpassword',
      host: 'smtp.gmail.com', 
      ssl: true
    }
 });
 var mailOptions = {
   from: 'senderid@gmail.com',
   to: request.body.emailid, //get from form input field of html file
   subject: 'Sending Email using Node.js',
   text: 'That was easy!'
 }; 
  transporter.sendMail(mailOptions, function(error, info){
    if (error) {
      alert(error);
    } else {
      alert('Email sent: ' + info.response);
    }
  });
  next();
};
app.use("/sendemail", sendemail);
http.createServer(app).listen(8888);
console.log("Server is listening......!!");


    <!-- userservice.ts file -->

    import { Injectable } from '@angular/core';
    import { Http, Headers } from '@angular/http';

    @Injectable()
    export class UserService {

      constructor(private http: Http) { }

      addNew(usercreds) {
        const emailid = 'email=' + usercreds.email;            
        this.http.post('http://localhost:8888/sendemail', emailid).subscribe((data) => {
            if (data.json().sucess) {
              console.log('mail sent');
              }
         });
      }

    }
    <!-- This is app.component.ts file-->
        import { Component } from '@angular/core';
        import { UserService } from './user.service';

        @Component({
          selector: 'app-root',
          templateUrl: './app.component.html',
          styleUrls: ['./app.component.css']
        })
        export class AppComponent {
          title = 'app';
          newUser = {
            email: '',
            password: ''
          };
          constructor (private user: UserService) {}
          addUser() {
            this.user.addNew(this.newUser);
          }
        }
    <form (ngSubmit)="addUser()"> 
          <md-card-content>
           <div>
            <md-icon>email</md-icon>
            <md-input-container>
             <input mdInput type="email" placeholder="Enter Email" name="email" [(ngModel)]="newUser.email">
            </md-input-container>
           </div>
           <div>
            <md-icon>lock</md-icon>
            <md-input-container>
             <input mdInput type="password" placeholder="Enter Password" name="password" [(ngModel)]="newUser.password">
            </md-input-container>  
           </div>
          </md-card-content>
          <md-card-actions>
           <button md-raised-button color="primary" type="submit">Send Email</button>
          </md-card-actions>
         </form>

I have a html component in Angular 4 in their i have form which carry 2 fields one is email and other is password. I am using only email of input field for sending email to client id.

For that i have a user service in that i am sending the data using http post method to my Node JS file.

Now I want to email form user and get it into my server.js file so when user register the email sent their email id which he put into input filed of form.

ns093
  • 31
  • 1
  • 6
  • whic error are you gettin ? – federico scamuzzi Aug 07 '17 at 11:25
  • Possible duplicate of [How to retrieve POST query parameters?](https://stackoverflow.com/questions/5710358/how-to-retrieve-post-query-parameters) – Supamiu Aug 07 '17 at 11:29
  • if i am passing direct email id of sender and receiver in server file then its work but i don't know how to get the value of input filed into the my server file. – ns093 Aug 07 '17 at 11:31
  • "Cannot read property 'emailid' of undefined" this is the error.... – ns093 Aug 07 '17 at 11:54
  • try like this `this.http.post('http://localhost:8888/sendemail', {emailid: emailid}, {headers: headers})` (and I don't think you need to concat `const emailid = 'email=' + usercreds.email;`...use just `const emailid = usercreds.email;` also...make sure to set your header (`content-type`) to send your data as `application/json` – Elmer Dantas Aug 07 '17 at 12:15
  • i have try this one but its not working -> emailid = usercreds.email – ns093 Aug 07 '17 at 12:41
  • I noticed now that you're using `request.body` to get your data. `request.body` is a [Express] (http://expressjs.com/en/api.html#req.body) object...it seems you're not using `Express`... – Elmer Dantas Aug 07 '17 at 13:47
  • I am new in Node JS i don't know how to use Express in this. – ns093 Aug 08 '17 at 06:46
  • So you need to read a little bit more about it. Check this tutorial https://scotch.io/tutorials/use-expressjs-to-get-url-and-post-parameters – Elmer Dantas Aug 08 '17 at 07:18

0 Answers0