【问题标题】:Transnational email with SendGrid and Firebase No Errors but no emails (Used NodeMailer as answer)带有 SendGrid 和 Firebase 的跨国电子邮件没有错误但没有电子邮件(使用 NodeMailer 作为答案)
【发布时间】: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


【解决方案1】:

好的,这就是搜索和搜索后对我有用的方法。感谢@Mises 给了我一个可以遵循的方向。对于其他尝试使用 nodemailer 使用 firebase 发送交易电子邮件的人,我就是这样做的。

  1. 我点击了@Mises 给我的上述链接; https://github.com/firebase/functions-samples/tree/Node-8/email-confirmation

我能够将函数上传到 firebase,但在 firebase 函数日志中仍然出现错误;

-发送电子邮件时出错:{ 错误:缺失 “PLAIN”的凭据

然后从那里我点击了这个链接; Missing credentials for "PLAIN" nodemailer

不幸的是,在谷歌上激活不太安全的应用对我不起作用。

这里还有来自 nodemailer 的官方文档;

https://nodemailer.com/about/

希望这对其他人有所帮助。

【讨论】:

    【解决方案2】:

    这可能是免费的 Firebase Spark 计划 https://firebase.google.com/pricing。云功能:出站网络 = 仅限 Google 服务。如果您更改为 Blaze 计划,如果您不使用太多出站网络,您仍然不会支付任何费用。我有 3 个月的 2x Blaze 计划,无需支付任何费用。

    【讨论】:

    • 我注册了 Blaze 计划,但仍然无法让我的功能正常工作。我仍然没有收到来自 Firebase、SendGrid 或控制台的错误。数据仍在实时数据库中记录,但仍然没有来自 SendGrid 的电子邮件。我想不通。
    • 您是否检查过函数日志中是否有错误?
    • console.log 来自 sgMail.send(msg) 的响应并签入日志。
    • 对不起,我还是 firebase 新手,但我仍然没有真正向我显示任何错误。这是我日志的图像。 imgur.com/a/8pvDpdS谢谢你的帮助!
    • 尝试更改您的 gmail.com 地址中的身份验证策略。默认情况下,身份验证受到限制,您无法通过 3rd 方应用程序登录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2021-01-25
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2016-07-12
    相关资源
    最近更新 更多