如何用nodejs和mailgun发送联系邮件



新增节点js和邮件枪。我正试图从一个网站发送一封"联系我们"的电子邮件,该网站使用以下内容作为我的发送按钮(在我的index.html页面上)。

      <!-- 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 -->

我有Node.js与Express和mailgun安装并在我的本地机器上工作,下面的代码片段在我的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');
});

我如何获得按钮信息,并从我的index.html发送?

谢谢你的帮助。感谢。

您需要创建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!');
});

最新更新