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

Cip*_*ian 9 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);
  });

}
Run Code Online (Sandbox Code Playgroud)

编辑***

上面的代码有效.当我emails正确的名字时,我试图触发该功能email.

Cip*_*ian 4

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

}
Run Code Online (Sandbox Code Playgroud)