0

new to node js and mail gun. I am trying to send a 'contact us' email from a website that is using the following for my send button (on my index.html page).

      <!-- START CONTACT SECTION -->
    <section class="section-contact" id="contacts">
        <div class="container">

            <!-- START SECTION HEADER -->
            <h2 class="reveal reveal-top">Contact Us</h2>
            <div class="subheading reveal reveal-top">
                Hit us up.  Let us know how we can make things better ( <b>Support@mail.com </b>).  
            </div>
            <!-- END SECTION HEADER -->

            <div class="contact-form-wrapper reveal reveal-bottom">

                <!-- START CONTACT FORM -->
            <!--    <form method="POST" action="./scripts/writeus.php" accept-charset="UTF-8" class="contact-form" id="jvalidate" novalidate="novalidate"> --> 
               <form method="POST" action="./scripts/mailgun.php" accept-charset="UTF-8" class="contact-form" id="jvalidate" novalidate="novalidate"> 
                 <!-- <form method="POST" action="send_mailgun($email)" accept-charset="UTF-8" class="contact-form" id="jvalidate" novalidate="novalidate"> -->


                    <div class="row">
                        <div class="col-sm-12 col-md-4">
                            <div class="field-holder">
                                <input class="form-control input-lg field" placeholder="John Snow" name="name" type="text">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-4">
                            <div class="field-holder">
                                <input class="form-control input-lg field" placeholder="myReplyEmail@Address.com" name="email" type="email">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-4">
                            <div class="field-holder">
                                <input class="form-control input-lg field" placeholder="Subject" name="website" type="text">
                            </div>
                        </div>
                        <div class="col-md-12 col-xs-12">
                            <div class="field-holder">
                                <textarea class="form-control" placeholder="Some text" name="message" cols="50" rows="10"></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-12">  
                                <div class="alert alert-success" role="alert" style="display: none;">
                                    Message has been successfully sent.
                                </div>
                                <div class="alert alert-danger" role="alert" style="display: none;">
                                    <div class="alert-text">Server error</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <button class="btn btn-primary btn-contact" type="submit" value="SEND MESSAGE">SEND MESSAGE</button>
                        </div>
                    </div>
                </form>
                <!-- END CONTACT FORM -->

            </div>

        </div>
    </section>
    <!-- END CONTACT SECTION -->

I have Node.js with Express and mailgun installed and working on my local machine with the snippet bellow hard coded as a test in my server.js

var Mailgun = require('mailgun').Mailgun;

var mg = new Mailgun('some-api-key');
mg.sendText('example@example.com', 'Recipient 1 <rec1@example.com>',
  'This is the subject',
  'This is the text',
  'noreply@example.com', {},
  function(err) {
    if (err) console.log('Oh noes: ' + err);
    else     console.log('Success');
});

How do I get the button info and send from my index.html ?

Thanks for the help. Much appreciated.

AhabLives
  • 1,308
  • 6
  • 22
  • 34
  • Your html button need to POST to the server address that Jacopo Brovida built for you – yeya Oct 20 '16 at 22:17

1 Answers1

0

You need create web server to receive submit from your form or ajax call. Look this: http://expressjs.com/

example:

var express = require('express');
var Mailgun = require('mailgun').Mailgun;
var app = express();

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

   /*use body-parser (https://github.com/expressjs/body-parser) for parameters */  
   var mg = new Mailgun('some-api-key');
   mg.sendText('example@example.com', 'Recipient 1 <rec1@example.com>',
         'This is the subject',
         'This is the text',
         'noreply@example.com', {},
      function(err) {
         if (err) {
           console.log('Oh noes: ' + err);
           res.send('Error occurred');
         }
         res.send('Email sent');
       });

app.listen(3000, function () {
   console.log('Example app listening on port 3000!');
});
Jacopo Brovida
  • 465
  • 4
  • 10
  • Not sure but, I think there is a misunderstanding. I have Node.js with express and mailgun working on my local machine. I'm just not sure how to send and email using mailgun and html button. I can and have sent hardcoded test email with 'npm start'. – AhabLives Oct 20 '16 at 20:50
  • You can't call directly from html button – Jacopo Brovida Oct 20 '16 at 21:06
  • @Jacopa Brovida what exactly is "sendMail" a .js file ? If so should the file be placed in 'public' folder or root ? thanks. – AhabLives Oct 20 '16 at 22:21
  • To anyone who is in the same boat. This link is what really helped http://stackoverflow.com/questions/35931135/cannot-post-error-using-express The big leap for me was the "body-parser". Make sure to run -npm install body-parser - to pass parameters. – AhabLives Oct 21 '16 at 01:37