gpt4 book ai didi

node.js - 如何使用 Firebase 云功能发送自定义电子邮件

转载 作者:搜寻专家 更新时间:2023-11-01 00:16:08 24 4
gpt4 key购买 nike

我想在使用 nodemail 和邮戳创建具有 firebase 云功能的用户后发送邮件。

我遵循了本教程:Tutorial link来自戴夫·马丁

但不断收到此错误:

There was an error while sending the welcome email: { status: 422, message: 'Zero recipients specified', 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'
}

最佳答案

决定只遵循邮戳文档(顺便说一下,这真的很好)从头开始。

下面是从 firebase 云函数中的事件发送邮件的非常简单的步骤:

1-下载包:

运行:npm install postmark

2- 注册到邮戳

注册到 PostMark- 然后找到您的 API key 。

3-设置firebase环境配置:

运行:firebase functions:config:set postmark.key="API-KEY-HERE"

需要添加的4个index.js代码:

//Mail 
const postmark = require('postmark')
const postmarkKey = functions.config().postmark.key;
const mailerClient = new postmark.ServerClient(postmarkKey);

exports.OnUserCreation = functions.auth.user().onCreate((user) => {
console.log("user created: " + user.data.uid);
console.log("user email: " + user.data.email);
return sendEmail(user);
})

// Send welcome email to new users
function sendEmail(user) {
const mailOptions = {
"From": "XYZ@YOURDOMAIN.com",
"To": user.data.email,
"Subject": "Test",
"TextBody": "Hello from Postmark!"
}
return mailerClient.sendEmail(mailOptions)
.then(() => console.log('Welcome confirmation email sent'))
.catch((error) => console.error('There was an error while sending the welcome email:', error))
}

就是这样。

无需下载 nodemailer 或使用传输器。

关于node.js - 如何使用 Firebase 云功能发送自定义电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53354930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com