google-cloud-functions - 在 Firebase 云功能中通过 nodemailer 发送邮件是否需要计费帐户?
问题描述
我部署了一个 Firebase 云功能,用于在用户首次登录时发送欢迎邮件。在 firebase 控制台中,在 firebase 云函数日志消息中,我在调用该函数时看到了此错误消息。
错误信息:
未配置结算帐号。外部网络无法访问,配额受到严格限制。配置结算帐户以删除这些限制
使用firebase云功能不能免费发送电子邮件吗?如果可能,请说明程序。(可能带有示例代码)
编辑 1:
1。我目前正在使用 nodemailer 发送邮件。
2. 我使用 Gmail 作为邮件服务。
解决方案
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 中
推荐阅读
- java - 使用 Apache Batik 对 SVG 文件进行正确缩进(漂亮的打印)
- c# - 未找到“EntityQueryable < ReportCompetency >”。确保实体类型已添加到模型中
- vba - 表格的vba选择范围
- sql - 重构 Oracle SQL 查询
- html - 内部 A Href 链接在网站上不起作用,而使用同一类的外部链接可以正常工作
- node.js - node V12 的 node-sspi 安装错误
- node.js - Mongoose 获取当前数据,即使它对模型无效
- java - 如何在android中添加带边界的彩色折线
- ios - 任何类似于 Libgdx 的 Swift 2D 框架/引擎?
- r - 仪表板中的串扰过滤器可能性