【发布时间】:2020-02-03 04:03:21
【问题描述】:
好的,所以我设置了一个带有联系表单的 Ionic webapp,并且我的表单与 firebase 交互,这意味着我的所有表单信息都存储在实时数据库中。现在我已经根据本教程设置了 SendGrid:
Firestore - 数据库触发事件; https://fireship.io/lessons/sendgrid-transactional-email-guide/
但是,当输入新数据时,不会触发云功能。我在控制台上没有收到任何错误,并且从 sendgrid 仪表板没有任何请求。我的理解是,当数据库发生变化时,它会自动触发该功能,然后 sendgrid 将发送带有相关数据的电子邮件。
这是我的代码;
// Firebase Config
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
// Sendgrid Config
import * as sgMail from '@sendgrid/mail';
const API_KEY = functions.config().sendgrid.key;
const TEMPLATE_ID = functions.config().sendgrid.template;
sgMail.setApiKey(API_KEY);
// Emails the author when a new messages is added
export const newMessage = functions.firestore.document('messages/{messageId}').onCreate( async (change, context) => {
// Raw Data
// const post = postSnap.data();
const msgData = change.data();
// Email
const msg = {
to: msgData.email,
from: 'Your_email@gmail.com',
templateId: TEMPLATE_ID,
dynamic_template_data: {
subject: 'New Message',
name: msgData.name,
text: `Here is the message: ${msgData.message}`,
phone: msgData.phone
},
};
// Send it
return sgMail.send(msg);
});
函数成功部署到firebase。
感谢任何帮助。
编辑 ////////////////////////////////////// // 编辑
最终改用 Nodemailer。
【问题讨论】:
-
检查 firebase 函数中的更改日志。您是否将您的项目设置为付款项目?
-
只有与 GCP 一起使用的功能是免费计划。要使用 SendGrid,您需要获得付款计划。
-
@Mises 您如何将项目设置为付款项目? SendGrid 说它每天 100 次免费。我在这里错过了什么?
-
是的,你失踪了。这不是关于 SendGrid 而是 Firebase。 Firebase 功能仅在您使用 firebase/GCP 工具时免费!!!如果您想使用其他工具,您需要有支付 Firebase 项目。 !!!
-
在您的 firebase 项目中设置 Blaze 计划。您每月可以免费使用 5gb 带宽,而 Blaze 计划只会在您跨过任何免费计划时才会骚扰您,因此构建网站仍然应该是免费的。
标签: javascript firebase firebase-realtime-database google-cloud-functions sendgrid