【问题标题】:SendGrid Azure IssueSendGrid Azure 问题
【发布时间】:2017-07-03 16:07:07
【问题描述】:

我正在尝试使用我在 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);
        }
    });
}

【问题讨论】:

标签: node.js azure sendgrid


【解决方案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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多