Unable to send mails using a form with nodejs nodemailer












0














So I'm trying to send mails with nodejs using the Nodemailer module. When I use this code it does work:



const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user:"my_emailadres_",
clientId:"my_client_id",
clientSecret:"my_client_secret",
refreshToken:"refresh_token",
accessToken: "my_access_token",
},
});

var mailOpties = {
from: "my_email",
to: "another_email",
subject: "Hello",
generateTextFromHTML: true,
html: "<b>Hello world!</b>"
};

transporter.sendMail(mailOpties, function(error) {
if (error) {
console.log(error);
} else {
console.log('Email is verzonden');
}
transporter.close();
});


But now I'm trying to send an email when I fill in a form on a contact page but its not sending the email and I'm getting an error.



This is my new POST method using nodemailer route:(FYS/javascript/mail.js):



const express = require('express');
const app = express();
//gebruik body-parser om data van de contact form op te nemen
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));

const nodemailer = require('nodemailer');

app.post('/Contact', function (req, res) {
let mailOpts, transporter;
transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: "my_email",
clientId: "my_client_id",
clientSecret: "my_client_secret",
refreshToken: "refresh_token",
accessToken: "acess_token",
},
});

mailOpts = {
from: req.body.name + ' &lt;' + req.body.email + '&gt;',
to: "my_email_adress",
subject: 'Nieuw bericht van Corendon klant',
text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
};

transporter.sendMail(mailOpts, function(error) {
if (error) {
res.render('contact-failure')
} else {
console.log('contact-success');
}
transporter.close();
});
});


This is the form I'm using in html route:(FYS/html/Contact.html)



 <form action="/Contact.html" id="contact-form" method="post" role="form">
<fieldset>
<label for="name">Name *</label>
<input id="name" name="name" type="text" placeholder="Your name" required="required">
<label for="email">Email *</label>
<input id="email" name="email" type="text" placeholder="Your email" required="required">
<label for="message">Message *</label>
<textarea id="message" name="message" placeholder="Enter your message here" rows="3" required="required"></textarea>
<button type="submit">Submit</button>
</fieldset>
</form>


So when I submit the form no email is being send and I'm getting an error: 404 not found.
I think the form action in the html form is not being linked well?:



 <form action="/Contact.html"


And what route should i do in the mail.js app.post?:



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


Or is there something else I'm doing wrong?
Any kind of help is appreciated
(I'm new using Nodejs and javascript)










share|improve this question



























    0














    So I'm trying to send mails with nodejs using the Nodemailer module. When I use this code it does work:



    const nodemailer = require('nodemailer');
    const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
    type: 'OAuth2',
    user:"my_emailadres_",
    clientId:"my_client_id",
    clientSecret:"my_client_secret",
    refreshToken:"refresh_token",
    accessToken: "my_access_token",
    },
    });

    var mailOpties = {
    from: "my_email",
    to: "another_email",
    subject: "Hello",
    generateTextFromHTML: true,
    html: "<b>Hello world!</b>"
    };

    transporter.sendMail(mailOpties, function(error) {
    if (error) {
    console.log(error);
    } else {
    console.log('Email is verzonden');
    }
    transporter.close();
    });


    But now I'm trying to send an email when I fill in a form on a contact page but its not sending the email and I'm getting an error.



    This is my new POST method using nodemailer route:(FYS/javascript/mail.js):



    const express = require('express');
    const app = express();
    //gebruik body-parser om data van de contact form op te nemen
    const bodyParser = require('body-parser');
    app.use(bodyParser.urlencoded({extended: true}));

    const nodemailer = require('nodemailer');

    app.post('/Contact', function (req, res) {
    let mailOpts, transporter;
    transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
    type: 'OAuth2',
    user: "my_email",
    clientId: "my_client_id",
    clientSecret: "my_client_secret",
    refreshToken: "refresh_token",
    accessToken: "acess_token",
    },
    });

    mailOpts = {
    from: req.body.name + ' &lt;' + req.body.email + '&gt;',
    to: "my_email_adress",
    subject: 'Nieuw bericht van Corendon klant',
    text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
    };

    transporter.sendMail(mailOpts, function(error) {
    if (error) {
    res.render('contact-failure')
    } else {
    console.log('contact-success');
    }
    transporter.close();
    });
    });


    This is the form I'm using in html route:(FYS/html/Contact.html)



     <form action="/Contact.html" id="contact-form" method="post" role="form">
    <fieldset>
    <label for="name">Name *</label>
    <input id="name" name="name" type="text" placeholder="Your name" required="required">
    <label for="email">Email *</label>
    <input id="email" name="email" type="text" placeholder="Your email" required="required">
    <label for="message">Message *</label>
    <textarea id="message" name="message" placeholder="Enter your message here" rows="3" required="required"></textarea>
    <button type="submit">Submit</button>
    </fieldset>
    </form>


    So when I submit the form no email is being send and I'm getting an error: 404 not found.
    I think the form action in the html form is not being linked well?:



     <form action="/Contact.html"


    And what route should i do in the mail.js app.post?:



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


    Or is there something else I'm doing wrong?
    Any kind of help is appreciated
    (I'm new using Nodejs and javascript)










    share|improve this question

























      0












      0








      0







      So I'm trying to send mails with nodejs using the Nodemailer module. When I use this code it does work:



      const nodemailer = require('nodemailer');
      const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
      type: 'OAuth2',
      user:"my_emailadres_",
      clientId:"my_client_id",
      clientSecret:"my_client_secret",
      refreshToken:"refresh_token",
      accessToken: "my_access_token",
      },
      });

      var mailOpties = {
      from: "my_email",
      to: "another_email",
      subject: "Hello",
      generateTextFromHTML: true,
      html: "<b>Hello world!</b>"
      };

      transporter.sendMail(mailOpties, function(error) {
      if (error) {
      console.log(error);
      } else {
      console.log('Email is verzonden');
      }
      transporter.close();
      });


      But now I'm trying to send an email when I fill in a form on a contact page but its not sending the email and I'm getting an error.



      This is my new POST method using nodemailer route:(FYS/javascript/mail.js):



      const express = require('express');
      const app = express();
      //gebruik body-parser om data van de contact form op te nemen
      const bodyParser = require('body-parser');
      app.use(bodyParser.urlencoded({extended: true}));

      const nodemailer = require('nodemailer');

      app.post('/Contact', function (req, res) {
      let mailOpts, transporter;
      transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
      type: 'OAuth2',
      user: "my_email",
      clientId: "my_client_id",
      clientSecret: "my_client_secret",
      refreshToken: "refresh_token",
      accessToken: "acess_token",
      },
      });

      mailOpts = {
      from: req.body.name + ' &lt;' + req.body.email + '&gt;',
      to: "my_email_adress",
      subject: 'Nieuw bericht van Corendon klant',
      text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
      };

      transporter.sendMail(mailOpts, function(error) {
      if (error) {
      res.render('contact-failure')
      } else {
      console.log('contact-success');
      }
      transporter.close();
      });
      });


      This is the form I'm using in html route:(FYS/html/Contact.html)



       <form action="/Contact.html" id="contact-form" method="post" role="form">
      <fieldset>
      <label for="name">Name *</label>
      <input id="name" name="name" type="text" placeholder="Your name" required="required">
      <label for="email">Email *</label>
      <input id="email" name="email" type="text" placeholder="Your email" required="required">
      <label for="message">Message *</label>
      <textarea id="message" name="message" placeholder="Enter your message here" rows="3" required="required"></textarea>
      <button type="submit">Submit</button>
      </fieldset>
      </form>


      So when I submit the form no email is being send and I'm getting an error: 404 not found.
      I think the form action in the html form is not being linked well?:



       <form action="/Contact.html"


      And what route should i do in the mail.js app.post?:



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


      Or is there something else I'm doing wrong?
      Any kind of help is appreciated
      (I'm new using Nodejs and javascript)










      share|improve this question













      So I'm trying to send mails with nodejs using the Nodemailer module. When I use this code it does work:



      const nodemailer = require('nodemailer');
      const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
      type: 'OAuth2',
      user:"my_emailadres_",
      clientId:"my_client_id",
      clientSecret:"my_client_secret",
      refreshToken:"refresh_token",
      accessToken: "my_access_token",
      },
      });

      var mailOpties = {
      from: "my_email",
      to: "another_email",
      subject: "Hello",
      generateTextFromHTML: true,
      html: "<b>Hello world!</b>"
      };

      transporter.sendMail(mailOpties, function(error) {
      if (error) {
      console.log(error);
      } else {
      console.log('Email is verzonden');
      }
      transporter.close();
      });


      But now I'm trying to send an email when I fill in a form on a contact page but its not sending the email and I'm getting an error.



      This is my new POST method using nodemailer route:(FYS/javascript/mail.js):



      const express = require('express');
      const app = express();
      //gebruik body-parser om data van de contact form op te nemen
      const bodyParser = require('body-parser');
      app.use(bodyParser.urlencoded({extended: true}));

      const nodemailer = require('nodemailer');

      app.post('/Contact', function (req, res) {
      let mailOpts, transporter;
      transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
      type: 'OAuth2',
      user: "my_email",
      clientId: "my_client_id",
      clientSecret: "my_client_secret",
      refreshToken: "refresh_token",
      accessToken: "acess_token",
      },
      });

      mailOpts = {
      from: req.body.name + ' &lt;' + req.body.email + '&gt;',
      to: "my_email_adress",
      subject: 'Nieuw bericht van Corendon klant',
      text: `${req.body.name} (${req.body.email}) says: ${req.body.message}`
      };

      transporter.sendMail(mailOpts, function(error) {
      if (error) {
      res.render('contact-failure')
      } else {
      console.log('contact-success');
      }
      transporter.close();
      });
      });


      This is the form I'm using in html route:(FYS/html/Contact.html)



       <form action="/Contact.html" id="contact-form" method="post" role="form">
      <fieldset>
      <label for="name">Name *</label>
      <input id="name" name="name" type="text" placeholder="Your name" required="required">
      <label for="email">Email *</label>
      <input id="email" name="email" type="text" placeholder="Your email" required="required">
      <label for="message">Message *</label>
      <textarea id="message" name="message" placeholder="Enter your message here" rows="3" required="required"></textarea>
      <button type="submit">Submit</button>
      </fieldset>
      </form>


      So when I submit the form no email is being send and I'm getting an error: 404 not found.
      I think the form action in the html form is not being linked well?:



       <form action="/Contact.html"


      And what route should i do in the mail.js app.post?:



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


      Or is there something else I'm doing wrong?
      Any kind of help is appreciated
      (I'm new using Nodejs and javascript)







      javascript node.js forms oauth-2.0 nodemailer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 11:12









      johnnyjohnny

      105




      105
























          0






          active

          oldest

          votes











          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%2f53445621%2funable-to-send-mails-using-a-form-with-nodejs-nodemailer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f53445621%2funable-to-send-mails-using-a-form-with-nodejs-nodemailer%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

          Trompette piccolo

          Slow SSRS Report in dynamic grouping and multiple parameters

          Simon Yates (cyclisme)