好的,所以我设置了一个带有联系表单的 Ionic 网络应用程序,并且我有与 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 Spark 计划 https://firebase.google.com/pricing .云功能:出站网络 = 仅限 Google 服务。如果您更改为 Blaze 计划,如果您不使用太多出站网络,您仍然不会支付任何费用。我有 2 个 Blaze 计划,为期 3 个月,无需支付任何费用。
关于javascript - 使用 SendGrid 和 Firebase 的跨国电子邮件没有错误但没有电子邮件(使用 NodeMailer 作为答案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58242783/