首页 > 解决方案 > 在 Firebase 云功能中通过 nodemailer 发送邮件是否需要计费帐户?

问题描述

我部署了一个 Firebase 云功能,用于在用户首次登录时发送欢迎邮件。在 firebase 控制台中,在 firebase 云函数日志消息中,我在调用该函数时看到了此错误消息。

错误信息:

未配置结算帐号。外部网络无法访问,配额受到严格限制。配置结算帐户以删除这些限制

使用firebase云功能不能免费发送电子邮件吗?如果可能,请说明程序。(可能带有示例代码)

编辑 1:
1。我目前正在使用 nodemailer 发送邮件。
2. 我使用 Gmail 作为邮件服务。

标签: google-cloud-functionsnodemailer

解决方案


Does sending mail via nodemailer in firebase cloud functions require billing account?

不,您不需要计费帐户即可使用云功能通过 nodmailer 发送电子邮件。

我在云功能中遇到了与您一样的计费错误。我已经完成了 2 个简单的步骤,它就消失了。

1.在您的 gmail 帐户设置中,将不太安全的应用访问权限启用为ON

2.也转到此链接并单击继续https://accounts.google.com/DisplayUnlockCaptcha

完成以上2个步骤后,计费错误消失,云功能发送邮件成功。

这是我的nodejs代码供您参考:

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');

const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'xyzz@gmail.com',
    pass: '123'
  },
});
exports.sendMail = functions.https.onRequest(async (req, res) => {
  const mailOptions = {
    from: '"Test." <noreply@firebase.com>',
    to: 'xyz@gmail.com'
  };
  // Building Email message.
  mailOptions.subject = 'Thanks and Welcome!'
  mailOptions.text = 'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.'

  try {
    await mailTransport.sendMail(mailOptions);
    console.log('subscription confirmation email sent to');
    return res.send('Sended');
  } catch (error) {
    console.error('There was an error while sending the email:', error);
    return res.send(error.toString());
  }
});

您可以在部署之前在本地进行测试

firebase serve --only functions

你会得到一个链接 http://localhost:5000/project-name/us-central1/sendMail; 将其粘贴到浏览器中,云功能将运行。如果有任何错误,它将显示在浏览器和控制台/powershell 中


推荐阅读