如何使用nodemailer包使用Angular 4和Node Js发送电子邮件

时间:2017-08-07 11:16:20

标签: node.js angular angular2-forms

<!-- 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>

我在Angular 4中有一个html组件,我的表单有2个字段,一个是电子邮件,另一个是密码。我只使用输入字段的电子邮件向客户端ID发送电子邮件。

为此,我有一个用户服务,我使用http post方法将数据发送到我的Node JS文件。

现在我想通过电子邮件发送表单用户并将其放入我的server.js文件中,这样当用户注册电子邮件时,就会发送他们的电子邮件ID,并将其放入表单的输入文件中。

0 个答案:

没有答案