Firebase Cloud功能-发送电子邮件时出错

时间:2019-02-16 08:41:27

标签: firebase google-cloud-functions nodemailer sparkpost

我需要通过HTTP请求向Firebase Cloud Function发送电子邮件。我使用Sparkpost作为提供程序,但是当我用Postman调用Function时,出现以下错误:

{
    "message": {
        "errno": "EAI_AGAIN",
        "code": "EAI_AGAIN",
        "syscall": "getaddrinfo",
        "hostname": "api.sparkpost.com",
        "host": "api.sparkpost.com",
        "port": "443"
    }
}

如果我尝试使用firebase serve --only functions命令调用本地提供的相同功能,然后用邮递员对其进行测试,则不会收到任何错误。

我的代码如下:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const SparkPost = require('sparkpost');

// Initialize App
admin.initializeApp();

exports.sendSurveyRecap = functions.https.onRequest((req, res) => {
  try {
    if (req.method !== 'POST') {
      return res.status(405).json({
        message: `Method ${req.method} is not allowed`,
      });
    }

    const client = new SparkPost('<MY API KEY>');

    return client.transmissions.send({
      content: {
        from: '<MY EMAIL DOMAIN>',
        subject: 'Hello from node-sparkpost',
        html: '<p>Hello world</p>',
      },
      recipients: [
        { address: 'foo@example.com' },
      ],
    }).then(() => res.status(200).json({
      message: 'Message sent successful',
    })).catch(error => res.status(500).json({
      message: error,
    }));
  } catch (error) {
    return res.status(500).json({
      message: error.message,
    });
  }
});

那么为什么当我使用“ deployed” URL而不是“ served” URL调用Cloud Function时收到该错误?

0 个答案:

没有答案