只是想用云功能发送电子邮件
const functions = require('firebase-functions')
const express = require('express')
const cors = require('cors')
const mailService = require('@sendgrid/mail')
const bodyParser = require('body-parser')
//Constants
const SENDGRID_API_KEY = 'Myapikey'
//Set up
const app = express()
app.use(bodyParser.json())
app.use(cors({ origin: true }))
app.post('/send', async (req, res) => {
mailService.setApiKey(SENDGRID_API_KEY)
const { name, email, message } = req.body
const emailContent = {
to: '[email protected]',
from: '[email protected]'',
subject: `subject`,
text: `some message`,
}
try{
const response = await mailService.send(emailContent)
res.json({message: 'message sent'})
} catch(error){
const { message, code, response } = error
res.json({message, response})
}
})
exports.api = functions.https.onRequest(app)
看起来像一个DNS问题。 我在其他地方(stackoverflow)读到,您需要付费计划才能对 firebase 函数发出外部请求。 它在您的笔记本电脑上本地工作吗?
@aroach
它在您的笔记本电脑上本地工作吗?
不,不在本地工作。
是 Firebase 问题还是 Sendgrid 问题?
谢谢
PS:我认为对于本地,您的意思是在本地调用该函数而不是已经部署的函数? 如果是,在这种情况下,我的功能是实时的
@aroach事实证明这是真的: https : //firebase.google.com/support
升级我的计划后,我可以发送消息。
谢谢
这是一个基于火力的问题。 如果您使用的是免费(Spark 计划)帐户,则只有在使用必须付费的外部服务时才能使用 Google 服务。
https://firebase.google.com/pricing/
升级您的计划(火焰计划或火焰计划),您可以通过 SendGrid 发送电子邮件。
@kadnan0900我升级到 blaze 计划,但仍然得到 getaddrinfo ENOTFOUND api.github.com api.github。 com:443错误。
也得到错误。 有趣的是,它工作了一次,然后就不再工作了。 在 firebase 中的 Blaze 计划中,验证我使用的是正确的 API 密钥,API 密钥凭证是正确的。 使用 firebase 模拟器/用户界面在本地测试,收到此错误。
使用:
"@sendgrid/mail": "^7.1.1",
"firebase-admin": "^8.12.1",
"firebase-functions": "^3.6.2"
看起来像一个DNS问题。 我在其他地方(stackoverflow)读到,您需要付费计划才能对 firebase 函数发出外部请求。 它在您的笔记本电脑上本地工作吗?
它在本地工作。 但不是在部署
也得到错误。 有趣的是,它工作了一次,然后就不再工作了。 在 firebase 中的 Blaze 计划中,验证我使用的是正确的 API 密钥,API 密钥凭证是正确的。 使用 firebase 模拟器/用户界面在本地测试,收到此错误。
使用:
"@sendgrid/mail": "^7.1.1", "firebase-admin": "^8.12.1", "firebase-functions": "^3.6.2"
运气好的话? 我们自己在 Blaze 计划中遇到了这个问题。
最有用的评论
看起来像一个DNS问题。 我在其他地方(stackoverflow)读到,您需要付费计划才能对 firebase 函数发出外部请求。 它在您的笔记本电脑上本地工作吗?