【问题标题】:sendgrid giving error sendgrid.Email is not a constructorsendgrid 给出错误 sendgrid.Email 不是构造函数
【发布时间】:2016-10-17 16:16:54
【问题描述】:

我正在使用 sendgrid 发送电子邮件。但是当我尝试如下创建电子邮件对象时

let email = new sendgrid.Email();
email.addTo("rhushikeshl@test.com");
email.setFrom("customercare@test.com");
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);
    }
});

但它给出了错误

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

【问题讨论】:

    标签: email sendgrid


    【解决方案1】:

    试试这个 - 它对我有用...

    首先安装'sendgrid-web',使用:

    npm install sendgrid-web

    之后,实现如下代码:

    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: 'test@gmail.com',
        from: 'test@gmail.com',
        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'});
        }
      });
    })
    

    【讨论】:

    • 很棒,也为我工作。您能否编辑您的答案以包含指向您找到它的文档的链接?因为我在文档中找不到这个解决方案
    【解决方案2】:

    这应该适合你

    var helper = require('sendgrid').mail;
    
    from_email = new helper.Email("test@example.com");
    to_email = new helper.Email("test@example.com");
    subject = "Sending with SendGrid is Fun";
    content = new helper.Content("text/plain", "and easy to do anywhere, even with Node.js");
    mail = new helper.Mail(from_email, subject, to_email, content);
    
    var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
    var request = sg.emptyRequest({
      method: 'POST',
      path: '/v3/mail/send',
      body: mail.toJSON()
    });
    
    sg.API(request, function(error, response) {
      console.log(response.statusCode);
      console.log(response.body);
      console.log(response.headers);
    })
    

    来源:https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/nodejs.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 2019-08-11
      • 2013-04-23
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多