Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sendgrid giving error sendgrid.Email is not a constructor

Tags:

email

sendgrid

I am using sendgrid to send email. but when i try to create the email object as follow

let email = new sendgrid.Email();
email.addTo("[email protected]");
email.setFrom("[email protected]");
email.setSubject("New Unit Added");
email.setHtml("New unit addded </br> Unit Id =" + savedUnit._id);
sendgrid.send(email, function(err, json) {
    if (err) {
        console.log("Error: " + err);
    } else {
        console.log(json);
    }
});

But it giving error

enter image description here

https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/nodejs.html

like image 936
Rhushikesh Avatar asked Oct 17 '16 16:10

Rhushikesh


1 Answers

Try this - it worked for me...

First install 'sendgrid-web' using:

npm install sendgrid-web

After that, implement the code like this:

router.get('/email2',function(req,res,next){
  var Sendgrid = require("sendgrid-web");
      var sendgrid = new Sendgrid({
        user: "Your_login_username_for_Sendgrid",//provide the login credentials
        key:"Your_Api_Key_OR_password"
      });

    sendgrid.send({
    to: '[email protected]',
    from: '[email protected]',
    subject: 'Hello world!',
    html: '<h1>Hello world!</h1>'
  }, function (err) {
    if (err) {
      console.log(err);
      res.json({Error:'Error in sending mail'});
    } else {
      console.log("Success.");
      res.json({Success:'sucessful'});
    }
  });
})
like image 199
Dharmendra Prajapati Avatar answered Sep 23 '22 18:09

Dharmendra Prajapati