How to send contact email with node js and mailgun?

时间:2016-10-20 19:50:24

标签: javascript node.js email mailgun

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.

1 个答案:

答案 0 :(得分:0)

您需要创建Web服务器以接收来自您的表单或ajax调用的提交。看看这个:http://expressjs.com/

示例:

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!');
});