1

For my website, I am trying to send email to the users. I used sendgrid provider for sending the mail with nodeJS. I am new to nodeJS, I have sent the mail via the command prompt as per their instructions on their page. But I don't how to implement it on the real server. For sending emails via sendgrid I installed their package. And I want to send an email to a particular event.

function goMail()
{
  const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(MY_API_KEY);
const msg = {
  to: '********1234@gmail.com',
  from: '12345***@gmail.com',
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

}

HTML :

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   <script src="index.js"></script>
</head>
<body>

<div class="container">
  <h2>Vertical (basic) form</h2>
  <form>
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
    </div>
    <div class="form-group">
      <label for="pwd">Password:</label>
      <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">
    </div>
    <div class="checkbox">
      <label><input type="checkbox" name="remember"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default" onclick="goMail();">Submit</button>
  </form>
</div>

</body>
</html>

Please explain to brief how do they work on the real server.

A.D.
  • 2,352
  • 2
  • 15
  • 25
Badhusha
  • 195
  • 2
  • 20

2 Answers2

0

You cannot directly include index.js file in HTML and invoke the Nodejs function. instead you should be using Express framework at Nodejs and use jQuery to post the form data. Please follow the instructions provided in the below link: https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm

Hope this answer points you to the right direction and resolve your issue.

Nagaraja Malla
  • 144
  • 1
  • 10
0

If you have implemented your server code then just add the following route in app.js file for creating an API for sending an email to the users

app.get('/sendEmail', (req, res) => {
    let sendgrid = require('sendgrid');
    let SG = sendgrid('your sendgridKey');
    let request = SG.emptyRequest();
    request.method = 'POST';
    request.path = '/v3/mail/send';
    request.body = (new sendgrid.mail.Mail(
    new sendgrid.mail.Email(req.body.from),
    req.body.subject, // subject
    new sendgrid.mail.Email(req.body.to), 
    new sendgrid.mail.Content('text/html', 'your html') 
    )).toJSON();

    SG.API(request, (err, response)=> {



  if (response.statusCode >= 200 && response.statusCode < 300) {
          res.send(response);
        });
});

or you can define this function outside of the router so that you can call this function as middleware in any route.

Rohan Das
  • 79
  • 5