Firebase的云功能-onWrite发送电子邮件 [英] Cloud Functions for Firebase - Send email onWrite

查看:68
本文介绍了Firebase的云功能-onWrite发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在/emails 中写入任何内容时,我试图将测试电子邮件发送到我的电子邮件中,但是该电子邮件从未发送过且功能日志为空.

I m trying to send a test email to my email when anything is written in /emails but the email never gets sent and the function logs are empty.

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 .

The above code works. I was trying to trigger the function on emails while the correct name was email.

推荐答案

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

Correct way of sending an email using 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的云功能-onWrite发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆