简体   繁体   English

无法将nodemailer错误响应从node.js发送到angular.js

[英]Unable to send nodemailer error response from node.js to angular.js

Here I'm sending a mail when the form is submitted. 提交表单后,我在这里发送邮件。 Everything is working fine, the only issue is even though the mail is not sent because of wrong authentication or internet problem ,the user is getting the success message. 一切工作正常,唯一的问题是即使由于身份验证错误或互联网问题而未发送邮件,用户也收到了成功消息。 I want that the user should get an error message if the mail is failed to get sent. 我希望如果邮件发送失败,用户应该收到一条错误消息。

client 客户

    this.mail= function() {
    var data = ({
        name :this.name,
        email :this.email

    })

    //Post Request
      $http({
  method: 'POST',
  url: '/contact2', data
}).then(function successCallback(response) {
    $mdToast.show(

                  $mdToast.simple()
                    .textContent('Thanks for your message '+data.name)
                    .position($scope.getToastPosition())
                    .hideDelay(5000)
                );
  }, function errorCallback(response) {
    $mdToast.show(
             $mdToast.simple()
               .textContent('Something went wrong, Please TRY AGAIN '+data.name)
               .position($scope.getToastPosition())
               .hideDelay(5000)
           );
  });

    });

server 服务器

function send(req, res) {
  console.log(req.body);

  var data= req.body
  smtpTransport.sendMail({ 
     from: "<email@gmail.com>", 
     to: data.email, 
     subject: "Website Submission from "+data.name,
     text: 'You have a new submission with the following details...,
  }, function(error, response){  //callback
     if(error){
         console.log(error);
     }if{
         console.log(" Message sent "+data.name);
     }

     smtpTransport.close(); 
  });
  res.json(data);
}

Server : You can create and send error message from server as 服务器:您可以从服务器创建和发送错误消息,如下所示:

if(error){ 
     res.json({ 
      Status : false,
      message : error
     })
}else{ 
     res.json({ Status : true,
     message : 'Success'
     })
}

Client : Here you can capture it by 客户:您可以在此处捕获它

function successCallback(response) {
if(response.Status){ 
     console.log(response.message);
}// for success
else{ 
     console.log(response.message) 
} //for error

} }

You can set and send additional data.emailSent = true or data.emailSent = false as: 您可以设置和发送其他data.emailSent = truedata.emailSent = false如下:

if(error){
       data.emailSent = false;
} else{
       data.emailSent = true;
}

On client you can check this flag and show success or failure accordingly. 在客户端上,您可以检查此标志并相应显示成功或失败。

Thank Zeeshan Hassan and pooja for helping me out, at first I was unable to access the status and message . 感谢Zeeshan Hassan和pooja为我提供帮助,起初我无法访问状态和消息。 I just made these few changes to Pooja's solution and its working 我刚刚对Pooja的解决方案及其工作做了一些更改

function successCallback(response) {
       console.log(response.data.Status);
       if (response.data.Status){console.log(response.data.message);}
`     `else{ console.log(response.data.message) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM