SendGrid Azure问题

时间:2017-07-03 16:07:07

标签: node.js azure sendgrid

我正在尝试使用Azure上的Node JS应用程序发送电子邮件并收到此错误:

TypeError: sendgrid.Email is not a constructor

这是我的代码。我使用了Microsoft的文档(https://docs.microsoft.com/en-us/azure/store-sendgrid-nodejs-how-to-send-email)。

var sendgrid = require('sendgrid')('SendGrid User ID', 'SendGrid password');
function createEmail() {
    console.log('CREATE EMAIL');

    var emailToSend = new sendgrid.Email({
        to: example@example.com,
        from: 'example@example.com',
        subject: 'Subject',
        text: 'some text';
    });

    sendEmail(emailToSend);
}

function sendEmail(email) {
    console.log('SEND EMAIL');
    sendgrid.send(email, function (err, json) {
        if (err) {
            return console.error(err);
        }
    });
}

1 个答案:

答案 0 :(得分:1)

正如@David Tansey所提到的,SendGrid团队添加了一个重大变化,以支持自v3.0.0以来的v3 Web API。 Here是使用最新版本(v5.1.2)的工作代码示例。

var helper = require('sendgrid').mail;
var fromEmail = new helper.Email('test@example.com');
var toEmail = new helper.Email('test@example.com');
var subject = 'Sending with SendGrid is Fun';
var content = new helper.Content('text/plain', 'and easy to do anywhere, even with Node.js');
var mail = new helper.Mail(fromEmail, subject, toEmail, 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) {
  if (error) {
    console.log('Error response received');
  }
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

但是,如果您希望所提供的代码顺利运行,则需要将版本还原为2.0.0。