【发布时间】:2019-09-25 14:47:35
【问题描述】:
在我的 Firebase 实时数据库中注册新供应商后,我想通过 Sendgrid 向新供应商发送一封欢迎电子邮件。我已经在我的应用程序的 functions/src/index.ts 文件夹中构建了一个 Firebase 函数 newVendorEmail() 来执行此操作,并根据https://app.sendgrid.com/guide/integrate/langs/nodejs/verify 配置了那里的所有内容。我还可以通过 newVendorEmail() 中的 onCreate() 从 Firebase 检索供应商详细信息,并将它们传递给 msg 对象的 dynamic_template_data 部分,没有任何问题。但是,当在 Firebase Functions 中触发 newVendorEmail() 函数时,未发送电子邮件,而是在 Firebase Functions 控制台中收到了此响应: TypeError: Object.values is not a function at Mail.setDynamicTemplateData (/user_code/node_modules/@sendgrid /mail/node_modules/@sendgrid/helpers/classes/mail.js:342:12)。请帮忙?
我尝试升级到最新的@sendgrid/mail npm 包 v6.4.0,尝试切换到新的 Sendgrid API 密钥,尝试按照 Sendgrid 的 github 示例 https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/kitchen-sink.md 将这个新的 API 密钥存储在 process.env 中,而不是functions.config(),但无济于事。
in node/process.env:
{ SENDGRID_API_KEY:
'SG....E',
...
}
in functions/src/index.ts:
'use strict'
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const sendgrid = require('@sendgrid/mail')
// init function
admin.initializeApp()
//init firebase ref const
const ref = admin.database().ref()
// set sendgrid api from process env
sendgrid.setApiKey(process.env.SENDGRID_API_KEY)
export const newVendorEmail = functions.database
.ref('users/{userId}/profile')
.onCreate((snapshot, context) => {
// call field data using snapshot.val()
let msg
const userData = snapshot.val()
if (userData.type === 'vendor') {
// set email data
msg = {
to: userData.email,
from: {
name: 'Blk. Party',
email: '...@blkparty.com'
},
// custom templates
templateId: '...',
dynamic_template_data: {
subject: 'Welcome to Blk. Party!',
name: userData.name,
regLink: userData.regLink
},
}
}
// send email via sendgrid
return sendgrid.send(msg)
})
in package.json:
...
"dependencies": {
"@sendgrid/mail": "^6.4.0",
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0"
},
"devDependencies": {
"@sendgrid/mail": "^6.4.0",
...
}
...
我希望发送的电子邮件没有任何错误。
【问题讨论】:
-
我已经很久没有使用 send-grid 了,所以虽然这个评论没有回答你的问题,但是看看邮戳。我发现它比任何其他电子邮件服务都友好得多。
-
谢谢菲利普!虽然我现在非常想坚持使用 Sendgrid。将在下一次应用升级时查看邮戳。
标签: firebase google-cloud-functions sendgrid