Firebase的云功能 - 发送电子邮件onWrite

时间:2017-06-08 21:11:21

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

我试图在/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

1 个答案:

答案 0 :(得分:4)

使用Firebase云功能正确发送电子邮件的方式!

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);
  });

}
相关问题