【发布时间】:2019-04-20 15:35:51
【问题描述】:
我想在使用 firebase 云功能创建用户后使用 nodemail 和邮戳发送邮件。
我遵循了这个教程:来自 Dave Martin 的 Tutorial link
但不断收到此错误:
发送欢迎电子邮件时出错:{ status: 422, message: '零收件人指定', code: 300 }
这是我从云函数发送邮件的代码:
//Mail
const nodemailer = require('nodemailer')
const postmarkTransport = require('nodemailer-postmark-transport')
// Google Cloud environment variable used:
// firebase functions:config:set postmark.key="API-KEY-HERE"
const postmarkKey = functions.config().postmark.key
const mailTransport = nodemailer.createTransport(postmarkTransport({
auth: {
apiKey: postmarkKey
}
}))
exports.OnUserCreation = functions.auth.user().onCreate((user) =>
{
console.log("user created: " + user.data.uid);
console.log("user email: " + user.data.email);
sendEmail(user);
})
function sendEmail(user)
{
// Send welcome email to new users
const mailOptions =
{
from: '"test" <test@test.com>',
to: user.email,
subject: 'Welcome!',
html: 'hello'
}
// Process the sending of this email via nodemailer
return mailTransport.sendMail(mailOptions)
.then(() => console.log('Welcome confirmation email sent'))
.catch((error) => console.error('There was an error while sending the welcome email:', error))
}
我的 postmark.key 已经在 firebase 配置中设置... API 告诉我问题是我用来发送邮件信息的格式。我该如何解决它?
更新
我也尝试修改mailOptions如下,但仍然是同样的错误:
const mailOptions = {
from: 'test@test.com',
to: user.email,
subject: 'Welcome!',
textBody: 'hello'
}
【问题讨论】:
-
如果你只使用 test@test.com 的 From 值呢?
-
更新了,你的建议我得到了同样的结果
-
您可能想检查
user.email是否为空,因为文档表明可以。 firebase.google.com/docs/reference/functions/… -
@DougStevenson 我根据您的建议更新了问题中的代码。我向您确认,此时 user.email 不为空,它确实包含 user.email 信息并且格式正确
标签: node.js firebase google-cloud-functions nodemailer postmark