Send mail with nodemailer in NodeJS with Angular5 front-end












-1














I want to send an email from my nodejs server whenever i register a user. I'm using Angular5 as front-end. It doesn't return any error, but it doesn't work either. It's like my function is not being called at all. This is what I tried(email not real in this example):



server.js



app.post('/sendMails', function(req,res) {

console.log("WENT INTO SEND MAILS!!!")


let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'someemail@gmail.com',
pass: process.env.MAIL_PASSWORD
},

tls: {
rejectUnauthorized: false
},

});

let HelperOptions = {

from: '"Babababa" <someemail@gmail.com>',
to: 'someemail@gmail.com',
subject: 'Hello world',
text: 'hey dude'
};

transporter.sendMail(HelperOptions, (error, response) => {
if(error) {
return console.log(error);
res.json({error: "API Error"});
}

else{
console.log("The message is sent: " + response.message);
res.json({response: "sent"})
}




})


});


from Angular



UserService.ts (relevant parts)



constructor(private http: HttpClient) { }

sendMail(user: User){
console.log("sendMail() was called.")
return this.http.post("http://localhost:3000/sendMails", user.email);
}


This is called in my registration component on signup



registration.component.ts



     public signUp(){

//marks all fields as touched.
this.fs.markFormGroupTouched(this.registerForm);


if(this.registerForm.valid) {

let user: User = this.registerForm.value;
console.log(user)

this.userService.createUser(user);
this.userService.sendMail(user); //<--- only this is relevant for this case.

}
else {
//shows the error messages, for the not valid input fields.
this.formErrors = this.fs.validateForm(this.registerForm,
this.formErrors, false)
}



}


When i run this code there's no errors. In the inspector/ client-side console window i see the message "sendMail() was called.", which means it's definetly calling my service. However, I recieve no mail nor any error










share|improve this question



























    -1














    I want to send an email from my nodejs server whenever i register a user. I'm using Angular5 as front-end. It doesn't return any error, but it doesn't work either. It's like my function is not being called at all. This is what I tried(email not real in this example):



    server.js



    app.post('/sendMails', function(req,res) {

    console.log("WENT INTO SEND MAILS!!!")


    let transporter = nodemailer.createTransport({
    service: 'gmail',
    secure: false,
    port: 25,
    auth: {
    user: 'someemail@gmail.com',
    pass: process.env.MAIL_PASSWORD
    },

    tls: {
    rejectUnauthorized: false
    },

    });

    let HelperOptions = {

    from: '"Babababa" <someemail@gmail.com>',
    to: 'someemail@gmail.com',
    subject: 'Hello world',
    text: 'hey dude'
    };

    transporter.sendMail(HelperOptions, (error, response) => {
    if(error) {
    return console.log(error);
    res.json({error: "API Error"});
    }

    else{
    console.log("The message is sent: " + response.message);
    res.json({response: "sent"})
    }




    })


    });


    from Angular



    UserService.ts (relevant parts)



    constructor(private http: HttpClient) { }

    sendMail(user: User){
    console.log("sendMail() was called.")
    return this.http.post("http://localhost:3000/sendMails", user.email);
    }


    This is called in my registration component on signup



    registration.component.ts



         public signUp(){

    //marks all fields as touched.
    this.fs.markFormGroupTouched(this.registerForm);


    if(this.registerForm.valid) {

    let user: User = this.registerForm.value;
    console.log(user)

    this.userService.createUser(user);
    this.userService.sendMail(user); //<--- only this is relevant for this case.

    }
    else {
    //shows the error messages, for the not valid input fields.
    this.formErrors = this.fs.validateForm(this.registerForm,
    this.formErrors, false)
    }



    }


    When i run this code there's no errors. In the inspector/ client-side console window i see the message "sendMail() was called.", which means it's definetly calling my service. However, I recieve no mail nor any error










    share|improve this question

























      -1












      -1








      -1







      I want to send an email from my nodejs server whenever i register a user. I'm using Angular5 as front-end. It doesn't return any error, but it doesn't work either. It's like my function is not being called at all. This is what I tried(email not real in this example):



      server.js



      app.post('/sendMails', function(req,res) {

      console.log("WENT INTO SEND MAILS!!!")


      let transporter = nodemailer.createTransport({
      service: 'gmail',
      secure: false,
      port: 25,
      auth: {
      user: 'someemail@gmail.com',
      pass: process.env.MAIL_PASSWORD
      },

      tls: {
      rejectUnauthorized: false
      },

      });

      let HelperOptions = {

      from: '"Babababa" <someemail@gmail.com>',
      to: 'someemail@gmail.com',
      subject: 'Hello world',
      text: 'hey dude'
      };

      transporter.sendMail(HelperOptions, (error, response) => {
      if(error) {
      return console.log(error);
      res.json({error: "API Error"});
      }

      else{
      console.log("The message is sent: " + response.message);
      res.json({response: "sent"})
      }




      })


      });


      from Angular



      UserService.ts (relevant parts)



      constructor(private http: HttpClient) { }

      sendMail(user: User){
      console.log("sendMail() was called.")
      return this.http.post("http://localhost:3000/sendMails", user.email);
      }


      This is called in my registration component on signup



      registration.component.ts



           public signUp(){

      //marks all fields as touched.
      this.fs.markFormGroupTouched(this.registerForm);


      if(this.registerForm.valid) {

      let user: User = this.registerForm.value;
      console.log(user)

      this.userService.createUser(user);
      this.userService.sendMail(user); //<--- only this is relevant for this case.

      }
      else {
      //shows the error messages, for the not valid input fields.
      this.formErrors = this.fs.validateForm(this.registerForm,
      this.formErrors, false)
      }



      }


      When i run this code there's no errors. In the inspector/ client-side console window i see the message "sendMail() was called.", which means it's definetly calling my service. However, I recieve no mail nor any error










      share|improve this question













      I want to send an email from my nodejs server whenever i register a user. I'm using Angular5 as front-end. It doesn't return any error, but it doesn't work either. It's like my function is not being called at all. This is what I tried(email not real in this example):



      server.js



      app.post('/sendMails', function(req,res) {

      console.log("WENT INTO SEND MAILS!!!")


      let transporter = nodemailer.createTransport({
      service: 'gmail',
      secure: false,
      port: 25,
      auth: {
      user: 'someemail@gmail.com',
      pass: process.env.MAIL_PASSWORD
      },

      tls: {
      rejectUnauthorized: false
      },

      });

      let HelperOptions = {

      from: '"Babababa" <someemail@gmail.com>',
      to: 'someemail@gmail.com',
      subject: 'Hello world',
      text: 'hey dude'
      };

      transporter.sendMail(HelperOptions, (error, response) => {
      if(error) {
      return console.log(error);
      res.json({error: "API Error"});
      }

      else{
      console.log("The message is sent: " + response.message);
      res.json({response: "sent"})
      }




      })


      });


      from Angular



      UserService.ts (relevant parts)



      constructor(private http: HttpClient) { }

      sendMail(user: User){
      console.log("sendMail() was called.")
      return this.http.post("http://localhost:3000/sendMails", user.email);
      }


      This is called in my registration component on signup



      registration.component.ts



           public signUp(){

      //marks all fields as touched.
      this.fs.markFormGroupTouched(this.registerForm);


      if(this.registerForm.valid) {

      let user: User = this.registerForm.value;
      console.log(user)

      this.userService.createUser(user);
      this.userService.sendMail(user); //<--- only this is relevant for this case.

      }
      else {
      //shows the error messages, for the not valid input fields.
      this.formErrors = this.fs.validateForm(this.registerForm,
      this.formErrors, false)
      }



      }


      When i run this code there's no errors. In the inspector/ client-side console window i see the message "sendMail() was called.", which means it's definetly calling my service. However, I recieve no mail nor any error







      node.js email angular5 nodemailer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 22:25









      Thomas

      516




      516
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The http.post() inside sendMail() returns an observable. You must subscribe to this observable, otherwise it will not be executed.



          So in registration.component.ts you need something like:



          this.userService.sendMail(user).subscribe(
          response => {
          console.log(response);
          }
          );





          share|improve this answer





















          • Thank you! This worked and solved the issue
            – Thomas
            Nov 22 at 23:27



















          0














          You also need to have a return statements in your sendMail function call. In the error block, the res.send is unreachable code. The edit code below:






          app.post('/sendMails', function(req,res) {

          console.log("WENT INTO SEND MAILS!!!")


          let transporter = nodemailer.createTransport({
          service: 'gmail',
          secure: false,
          port: 25,
          auth: {
          user: 'someemail@gmail.com',
          pass: process.env.MAIL_PASSWORD
          },

          tls: {
          rejectUnauthorized: false
          },

          });

          let HelperOptions = {

          from: '"Babababa" <someemail@gmail.com>',
          to: 'someemail@gmail.com',
          subject: 'Hello world',
          text: 'hey dude'
          };

          transporter.sendMail(HelperOptions, (error, response) => {
          if(error) {
          console.log(error);
          return res.json({error: "API Error"}); // return should be here instead of at console.log
          }

          else{
          console.log("The message is sent: " + response.message);
          return res.json({response: "sent"})
          }




          })


          });








          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438632%2fsend-mail-with-nodemailer-in-nodejs-with-angular5-front-end%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The http.post() inside sendMail() returns an observable. You must subscribe to this observable, otherwise it will not be executed.



            So in registration.component.ts you need something like:



            this.userService.sendMail(user).subscribe(
            response => {
            console.log(response);
            }
            );





            share|improve this answer





















            • Thank you! This worked and solved the issue
              – Thomas
              Nov 22 at 23:27
















            1














            The http.post() inside sendMail() returns an observable. You must subscribe to this observable, otherwise it will not be executed.



            So in registration.component.ts you need something like:



            this.userService.sendMail(user).subscribe(
            response => {
            console.log(response);
            }
            );





            share|improve this answer





















            • Thank you! This worked and solved the issue
              – Thomas
              Nov 22 at 23:27














            1












            1








            1






            The http.post() inside sendMail() returns an observable. You must subscribe to this observable, otherwise it will not be executed.



            So in registration.component.ts you need something like:



            this.userService.sendMail(user).subscribe(
            response => {
            console.log(response);
            }
            );





            share|improve this answer












            The http.post() inside sendMail() returns an observable. You must subscribe to this observable, otherwise it will not be executed.



            So in registration.component.ts you need something like:



            this.userService.sendMail(user).subscribe(
            response => {
            console.log(response);
            }
            );






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 23:06









            Boji

            807




            807












            • Thank you! This worked and solved the issue
              – Thomas
              Nov 22 at 23:27


















            • Thank you! This worked and solved the issue
              – Thomas
              Nov 22 at 23:27
















            Thank you! This worked and solved the issue
            – Thomas
            Nov 22 at 23:27




            Thank you! This worked and solved the issue
            – Thomas
            Nov 22 at 23:27













            0














            You also need to have a return statements in your sendMail function call. In the error block, the res.send is unreachable code. The edit code below:






            app.post('/sendMails', function(req,res) {

            console.log("WENT INTO SEND MAILS!!!")


            let transporter = nodemailer.createTransport({
            service: 'gmail',
            secure: false,
            port: 25,
            auth: {
            user: 'someemail@gmail.com',
            pass: process.env.MAIL_PASSWORD
            },

            tls: {
            rejectUnauthorized: false
            },

            });

            let HelperOptions = {

            from: '"Babababa" <someemail@gmail.com>',
            to: 'someemail@gmail.com',
            subject: 'Hello world',
            text: 'hey dude'
            };

            transporter.sendMail(HelperOptions, (error, response) => {
            if(error) {
            console.log(error);
            return res.json({error: "API Error"}); // return should be here instead of at console.log
            }

            else{
            console.log("The message is sent: " + response.message);
            return res.json({response: "sent"})
            }




            })


            });








            share|improve this answer


























              0














              You also need to have a return statements in your sendMail function call. In the error block, the res.send is unreachable code. The edit code below:






              app.post('/sendMails', function(req,res) {

              console.log("WENT INTO SEND MAILS!!!")


              let transporter = nodemailer.createTransport({
              service: 'gmail',
              secure: false,
              port: 25,
              auth: {
              user: 'someemail@gmail.com',
              pass: process.env.MAIL_PASSWORD
              },

              tls: {
              rejectUnauthorized: false
              },

              });

              let HelperOptions = {

              from: '"Babababa" <someemail@gmail.com>',
              to: 'someemail@gmail.com',
              subject: 'Hello world',
              text: 'hey dude'
              };

              transporter.sendMail(HelperOptions, (error, response) => {
              if(error) {
              console.log(error);
              return res.json({error: "API Error"}); // return should be here instead of at console.log
              }

              else{
              console.log("The message is sent: " + response.message);
              return res.json({response: "sent"})
              }




              })


              });








              share|improve this answer
























                0












                0








                0






                You also need to have a return statements in your sendMail function call. In the error block, the res.send is unreachable code. The edit code below:






                app.post('/sendMails', function(req,res) {

                console.log("WENT INTO SEND MAILS!!!")


                let transporter = nodemailer.createTransport({
                service: 'gmail',
                secure: false,
                port: 25,
                auth: {
                user: 'someemail@gmail.com',
                pass: process.env.MAIL_PASSWORD
                },

                tls: {
                rejectUnauthorized: false
                },

                });

                let HelperOptions = {

                from: '"Babababa" <someemail@gmail.com>',
                to: 'someemail@gmail.com',
                subject: 'Hello world',
                text: 'hey dude'
                };

                transporter.sendMail(HelperOptions, (error, response) => {
                if(error) {
                console.log(error);
                return res.json({error: "API Error"}); // return should be here instead of at console.log
                }

                else{
                console.log("The message is sent: " + response.message);
                return res.json({response: "sent"})
                }




                })


                });








                share|improve this answer












                You also need to have a return statements in your sendMail function call. In the error block, the res.send is unreachable code. The edit code below:






                app.post('/sendMails', function(req,res) {

                console.log("WENT INTO SEND MAILS!!!")


                let transporter = nodemailer.createTransport({
                service: 'gmail',
                secure: false,
                port: 25,
                auth: {
                user: 'someemail@gmail.com',
                pass: process.env.MAIL_PASSWORD
                },

                tls: {
                rejectUnauthorized: false
                },

                });

                let HelperOptions = {

                from: '"Babababa" <someemail@gmail.com>',
                to: 'someemail@gmail.com',
                subject: 'Hello world',
                text: 'hey dude'
                };

                transporter.sendMail(HelperOptions, (error, response) => {
                if(error) {
                console.log(error);
                return res.json({error: "API Error"}); // return should be here instead of at console.log
                }

                else{
                console.log("The message is sent: " + response.message);
                return res.json({response: "sent"})
                }




                })


                });








                app.post('/sendMails', function(req,res) {

                console.log("WENT INTO SEND MAILS!!!")


                let transporter = nodemailer.createTransport({
                service: 'gmail',
                secure: false,
                port: 25,
                auth: {
                user: 'someemail@gmail.com',
                pass: process.env.MAIL_PASSWORD
                },

                tls: {
                rejectUnauthorized: false
                },

                });

                let HelperOptions = {

                from: '"Babababa" <someemail@gmail.com>',
                to: 'someemail@gmail.com',
                subject: 'Hello world',
                text: 'hey dude'
                };

                transporter.sendMail(HelperOptions, (error, response) => {
                if(error) {
                console.log(error);
                return res.json({error: "API Error"}); // return should be here instead of at console.log
                }

                else{
                console.log("The message is sent: " + response.message);
                return res.json({response: "sent"})
                }




                })


                });





                app.post('/sendMails', function(req,res) {

                console.log("WENT INTO SEND MAILS!!!")


                let transporter = nodemailer.createTransport({
                service: 'gmail',
                secure: false,
                port: 25,
                auth: {
                user: 'someemail@gmail.com',
                pass: process.env.MAIL_PASSWORD
                },

                tls: {
                rejectUnauthorized: false
                },

                });

                let HelperOptions = {

                from: '"Babababa" <someemail@gmail.com>',
                to: 'someemail@gmail.com',
                subject: 'Hello world',
                text: 'hey dude'
                };

                transporter.sendMail(HelperOptions, (error, response) => {
                if(error) {
                console.log(error);
                return res.json({error: "API Error"}); // return should be here instead of at console.log
                }

                else{
                console.log("The message is sent: " + response.message);
                return res.json({response: "sent"})
                }




                })


                });






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 23:25









                shmit

                44239




                44239






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438632%2fsend-mail-with-nodemailer-in-nodejs-with-angular5-front-end%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks