【问题标题】:Send an email using Firebase Cloud Functions in React Native在 React Native 中使用 Firebase Cloud Functions 发送电子邮件
【发布时间】:2021-09-16 06:19:21
【问题描述】:

我想使用 Firebase Cloud Functions 在我的 React Native 中发送电子邮件。用户应该能够发送电子邮件以在应用程序中报告问题/反馈。我在我的本机应用程序中创建了一个文本输入和一个按钮。用户应该能够在文本输入框中指定他们的问题/反馈,当他们按下按钮时,我将在我的 gmail 或 hotmail 帐户中以电子邮件的形式收到他们的回复。我可以在 Firebase Cloud Functions 中使用 onCreate 来实现这一点吗?如果是这样,我怎样才能做到这一点? onCreate 方法和 react native 中的按钮功能会是什么样子?我对本机和 firebase 云功能反应非常陌生。不幸的是,我没有看到任何关于此的链接。

谢谢。

请看下面:

const nodemailer = require('nodemailer');
const email = functions.config().email_credentials.email;
const password = functions.config().email_credentials.password;
const mailTransport = nodemailer.createTransport(`smtps://${email}:${password}@smtp.gmail.com`);

functions.database.ref('/feedbacks/{currentId}').onCreate((snapshot, context) => {
      const feedback = snapshot.val().feedback;
      const name = snapshot.val().name;
      
      const mailOptions = {
        from: snapshot.val().email,
        replyTo: snapshot.val().email,
        to: functions.config().email_credentials.email,
        subject: `Feedback from `+name,
        html: feedback,
      };
      
          try {
               mailTransport.sendMail(mailOptions);
          } catch(error) {
              console.log(error);
          }
      
      return null;
      
  });

实时数据库:

【问题讨论】:

    标签: firebase react-native google-cloud-functions


    【解决方案1】:

    您的云函数可能如下所示:

    import * as functions from "firebase-functions";
    import admin from "firebase-admin";
    import nodemailer from "nodemailer";
    const { email, password } = functions.config().gmail;
    const mailTransport = nodemailer.createTransport(
      `smtps://${email}:${password}@smtp.gmail.com`
    );
    
    export default functions.database
      .ref("/feedbacks/{uid}")
      .onCreate(async (eventSnapshot, context) => {
        const data = eventSnapshot.val();
        const { feedback } = data;
    
          const mailOptions = {
            from: functions.config().email_credentials.email,
            replyTo: functions.config().email_credentials.email,
            to: snapshot.val().email,
            subject: `Feedback from `+name,
            html: feedback,
          };
    
        await mailTransport.sendMail(mailOptions);
        return null;
      });
    

    确保将您的电子邮件凭据保存在 firebase 云功能配置下,而不是代码中。如果你把它放在代码中的任何地方,它可能会在一段时间内被某人阅读。这很重要。

    确保在您的 Gmail 中启用“Unsercure Apps”。更多相关信息here

    现在,如果有人将一些数据添加到路径 feeds 并发送电子邮件。

    不要忘记使用配置来部署您的功能。

    【讨论】:

    • 谢谢。我还必须启用 DisplayUnlockCaptcha 才能使其正常工作。我注意到的一件事是,如果我尝试在 from 字段中使用来自我的数据库的用户电子邮件,例如snapshot.val().email 它返回我的 gmail 电子邮件地址而不是用户的电子邮件。但是当我使用 snapshot.val().email 时,replyTo 字段会返回用户的电子邮件地址。你知道为什么吗?这是在我的笔记本电脑上使用模拟器时。
    • 能否请您与我们分享一些代码来看看它。也许只是有点错字左右。
    • 请见上文。我在字段 replyTo 中看到来自实时数据库的用户电子邮件,但在收件箱的 from 字段中没有。我的电子邮件在 from 字段中使用。
    • 您不能从您的用户电子邮件中发送电子邮件。您可以将replyTo 设置为任何电子邮件,但您始终发送电子邮件from 您的电子邮件。在from 部分,您可以输入您的姓名和电子邮件。任何其他电子邮件都将被忽略。如果可以的话,我们都可以从其他人那里发送电子邮件;)
    • 我已经用配置更新了答案,它们“应该”是怎样的
    猜你喜欢
    • 2017-11-10
    • 2021-11-20
    • 2020-09-08
    • 2018-01-15
    • 2017-10-30
    • 2018-12-02
    • 2018-09-09
    • 1970-01-01
    • 2020-04-25
    相关资源
    最近更新 更多