邮件未使用平均堆栈应用程序正确发送

时间:2018-06-25 21:39:32

标签: angular

我有这样的问题。我正在使用我的堆栈应用程序创建自动发送电子邮件功能。

这是我后端的index.js文件。

const express = require('express');
const  bodyParser = require('body-parser');
const passport = require('passport');
const cors = require('cors');

const path = require('path');

var app = express();
const config = require('./db.js');
require('./config/passport')(passport);

var userController = require('./controllers/userController');
var reservationController = require('./controllers/reservationController');
var mailController = require('./controllers/mailController');

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(cors({ origin: 'http://localhost:4200'}));



app.listen(3000, function ( ) {
    console.log('Server started at port : 3000')
});

app.use('/user', userController);
app.use('/reservation', reservationController);
app.use('/mail', mailController);

在这里,我提供了我的邮件控制器文件。

const express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'labreservation123@gmail.com',
        pass: 'a@##1234'
    }
});


router.post("/sendmail", function (req, res) {
    const email= req.body.email;
    console.log(email);
    console.log('hii');
    var mailOptions = {
        from: 'labreservation123@gmail.com',
        to: 'tharindusandaruwan40@gmail.com',
        subject: 'Sending Email using Node.js',
        text: 'That was easy!'
    }

    transporter.sendMail(mailOptions, function(error, info){
        if (error) {
            console.log(error);
        } else {
            res.json(info.response);
        }
    });

});

module.exports = router;

当我与邮递员确认时,它工作正常。但是当我在前端尝试使用它时,它什么也没发送,甚至没有控制台登录问候。

这是我的服务文件。

import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders } from '@angular/common/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import {Reservation} from "./reservation";

@Injectable()
export class ReservationService {
  public selectedReservation: Reservation = new Reservation();
  public  reservations: Reservation[];
  public  removedReservations: Reservation[];
  public cCount:any;
  public rCount:any;
  public uCount:any;
  constructor(private http: HttpClient) { }

  getNotConfirmedReservationList(){
    return this.http.get('http://localhost:3000/reservation/notComfirmed');
  }

  confirm(_id: string){
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:3000/reservation/confirm',{_id} , {headers: headers});
  }

  remove(_id: string){
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:3000/reservation/remove',{_id} , {headers: headers});
  }

  getallReservationList(){
    return this.http.get('http://localhost:3000/reservation');
  }

  getallRemoved(){
    return this.http.get('http://localhost:3000/reservation/removed');
  }

  getCCount(){
    return this.http.get('http://localhost:3000/reservation/cCount');
  }

  getRCount(){
    return this.http.get('http://localhost:3000/reservation/rCount');
  }

  getUCount(){
    return this.http.get('http://localhost:3000/reservation/uCount');
  }

  getItem(_id: string){
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:3000/reservation/item',{_id} , {headers: headers});
  }

  sendMail(email:string){
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:3000/mail/sendmail',{email},{headers: headers});
  }


}

在这里,我提供了用于调用此电子邮件功能的特定component.ts文件。

import { Component, OnInit } from '@angular/core';
import { UserService} from '../../shared/user.service';
import {Router} from "@angular/router";
import {ReservationService} from '../../shared/reservation.service';
import { FlashMessagesService } from 'angular2-flash-messages';

//
import { Reservation } from '../../shared/reservation';
// import {AlertService} from "ngx-alerts";


@Component({
  selector: 'app-adminpannel',
  templateUrl: './adminpannel.component.html',
  styleUrls: ['./adminpannel.component.css'],
  providers: [UserService,ReservationService]
})
export class AdminpannelComponent implements OnInit {
  reservation:any;
  constructor(private userService: UserService,private router: Router, private reservationService: ReservationService, private flashMessage:FlashMessagesService) { }

  ngOnInit() {
    if(this.userService.loggedIn()) {
      this.userService.getProfile().subscribe(res => {
        console.log(res)
      })
    }

    this.getNotConfirmedReservations();
    this.getCCount();
    this.getRCount();
    this.getUCount();
  }



  getNotConfirmedReservations(){
    this.reservationService.getNotConfirmedReservationList().subscribe( (res) => {
      this.reservationService.reservations = res as Reservation[];
    });
  }

  confirm(id){
    this.reservationService.confirm(id).subscribe( (res) => {
      this.reservationService.getItem(id).subscribe((res)=>{
        this.reservationService.selectedReservation=res as Reservation;
        this.reservation=this.reservationService.selectedReservation;
        this.reservationService.sendMail(this.reservation.email);
      });
      this.flashMessage.show("Succesfully confirmed" , {cssClass: 'alert-succes', delay: 1000});
      this.getNotConfirmedReservations();
      // this.router.navigate(['/admin']);
    });
  }

  remove(id){
    this.reservationService.remove(id).subscribe( (res) => {
      this.flashMessage.show("Succesfully removed" , {cssClass: 'alert-danger', delay: 1000});
      this.getNotConfirmedReservations();

    });
  }

  getCCount(){
    this.reservationService.getCCount().subscribe((res)=>{
      this.reservationService.cCount=res;
    })
  }

  getRCount(){
    this.reservationService.getRCount().subscribe((res)=>{
      this.reservationService.rCount=res;
    })
  }

  getUCount(){
    this.reservationService.getUCount().subscribe((res)=>{
      this.reservationService.uCount=res;
    })
  }


}

有人可以帮助我解决此问题并正确发送邮件吗?谢谢

0 个答案:

没有答案