AhabLives AhabLives - 1 month ago 14
Node.js Question

How to send contact email with node js and 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.

Answer

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