【问题标题】:Cloud Functions for Firebase - Send email onWriteCloud Functions for Firebase - 发送电子邮件 onWrite
【发布时间】:2017-11-10 18:04:50
【问题描述】:

当/emails 中写有任何内容时,我正在尝试向我的电子邮件发送测试电子邮件,但电子邮件从未被发送并且功能日志为空。

exports.sendTestEmail = functions.database.ref('/emails')
  .onWrite(event => {
    return sendTestEmail('myemail@gmail.com');
  })
// [END onWrite]

// Sends a welcome email to the given user.
function sendTestEmail(email) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@example.com>`,
    to: email
  };
  // The user subscribed to the newsletter.
  mailOptions.subject = `Welcome to ${APP_NAME}!`;
  mailOptions.text = `Hey there! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
  return mailTransport.sendMail(mailOptions).then(() => {
    console.log('New welcome email sent to:', email);
  });

}

编辑***

上面的代码有效。我试图在emails 上触发该功能,而正确的名称是email。

【问题讨论】:

  • 您是否使用 gmail 作为您的 smtp 服务?如果是,出于 Google 的安全考虑,您可能需要使用应用密码而不是个人密码。这是一种方法support.google.com/accounts/answer/185833?hl=en
  • 是的,我是。问题是我在用户创建帐户时使用相同的格式发送电子邮件。这行得通。
  • 尝试在发送时捕获错误:mailTransport.sendMail(mailOptions).then(...).catch(error =&gt; { console.error('Error sending:', error); });
  • 你说“函数日志是空的”。您看到的条目是 Function execution started,对吧?
  • 还要确保写入/emails 的值是对先前值的更改。我还没有测试所有客户端 SDK,但在 Android 中,对不更改值的位置执行写入不会导致onWrite() 事件触发。

标签: firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

使用 Firebase Cloud Functions 发送电子邮件的正确方法!

exports.sendTestEmail = functions.database.ref('/emails')
  .onWrite(event => {
    return sendTestEmail('myemail@gmail.com');
  })
// [END onWrite]

// Sends a welcome email to the given user.
function sendTestEmail(email) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@example.com>`,
    to: email
  };
  // The user subscribed to the newsletter.
  mailOptions.subject = `Welcome to ${APP_NAME}!`;
  mailOptions.text = `Hey there! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
  return mailTransport.sendMail(mailOptions).then(() => {
    console.log('New welcome email sent to:', email);
  });

}

【讨论】:

  • 我是firebase云功能的新手,我应该根据您的解决方案导入任何类来发送电子邮件吗?我应该写什么?
猜你喜欢
  • 2017-08-26
  • 2017-08-25
  • 2018-01-15
  • 1970-01-01
  • 2017-09-16
  • 2018-10-11
  • 2017-10-30
  • 2017-09-01
相关资源
最近更新 更多