【发布时间】:2017-11-10 18:04:50
【问题描述】:
当/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。
【问题讨论】:
-
您是否使用 gmail 作为您的 smtp 服务?如果是,出于 Google 的安全考虑,您可能需要使用应用密码而不是个人密码。这是一种方法support.google.com/accounts/answer/185833?hl=en
-
是的,我是。问题是我在用户创建帐户时使用相同的格式发送电子邮件。这行得通。
-
尝试在发送时捕获错误:
mailTransport.sendMail(mailOptions).then(...).catch(error => { console.error('Error sending:', error); }); -
你说“函数日志是空的”。您看到的条目是 Function execution started,对吧?
-
还要确保写入
/emails的值是对先前值的更改。我还没有测试所有客户端 SDK,但在 Android 中,对不更改值的位置执行写入不会导致onWrite()事件触发。
标签: firebase firebase-realtime-database google-cloud-functions