如何在 nodejs 和 SendGrid 中发送电子邮件确认?
how to send email confirmation in nodejs and SendGrid?
我有一个 NodeJs 和 ReactJs 项目,用户可以在其中 注册 并且在用户注册后他们将收到一封电子邮件以确认他们的帐户。
所以现在当我注册时,电子邮件运行良好。但它适用于我这样设置的电子邮件。
function sendMail() {
const msg = {
to: "someoneemail@gmail.com",
from: "myemail@gmail.com",
subject: "a subject",
text: "some text herer",
html: "<strong>and easy to do anywhere, even with Node.js</strong>",
};
sgMail
.send(msg)
.then(() => {
console.log("Email sent");
})
.catch((error) => {
console.error(error);
});
}
module.exports = { sendMail };
我需要删除这个 to: "someoneemail@gmail.com"
a*
nd 而不是设置用户电子邮件,即在此注册的用户
系统
而不是 text:
我必须发送 令牌 .
所以这是注册部分:
router.post("/register", async (req, res) => {
const { fullName, emailAddress, password } = req.body;
const user = await Users.findOne({
where: {
[Op.and]: [{ fullName: fullName }, { emailAddress: emailAddress }],
},
});
if (user) {
res.status(400).send({
error: `some message.`,
});
} else {
bcrypt
.hash(password, 10)
.then((hash) => {
return {
fullName: fullName,
emailAddress: emailAddress,
password: hash,
isVerified: false,
};
})
.then((user) => {
const token = TokenGenerator.generate();
const creator = Verifications.belongsTo(Users, { as: "user" });
return Verifications.create(
{
token,
user: user,
},
{
include: [creator],
}
);
})
.then((verification) => {
console.log("verification", verification);
sendMail();
})
.then(() => res.json("User, Successmessage "));
}
});
但代码不在同一个文件中。
只需将您需要的参数添加到sendMail
函数中即可:
function sendMail(user, token) {
const msg = {
to: user.emailAddress,
from: "myemail@gmail.com",
subject: "Sending with SendGrid is Fun",
text: token,
html: `<strong>${token}</strong>`,
};
sgMail
.send(msg)
.then(() => {
console.log("Email sent");
})
.catch((error) => {
console.error(error);
});
}
同样在promise中注入需要的参数:
.then(async (user) => {
const token = TokenGenerator.generate();
const creator = Verifications.belongsTo(Users, { as: "user" });
await Verifications.create(
{
token,
user: user,
},
{
include: [creator],
}
);
return {user, token};
})
.then(({user, token}) => {
sendMail(user, token);
})
.then(() => res.json("User, Successmessage "));
我有一个 NodeJs 和 ReactJs 项目,用户可以在其中 注册 并且在用户注册后他们将收到一封电子邮件以确认他们的帐户。
所以现在当我注册时,电子邮件运行良好。但它适用于我这样设置的电子邮件。
function sendMail() {
const msg = {
to: "someoneemail@gmail.com",
from: "myemail@gmail.com",
subject: "a subject",
text: "some text herer",
html: "<strong>and easy to do anywhere, even with Node.js</strong>",
};
sgMail
.send(msg)
.then(() => {
console.log("Email sent");
})
.catch((error) => {
console.error(error);
});
}
module.exports = { sendMail };
我需要删除这个 to: "someoneemail@gmail.com"
a*
nd 而不是设置用户电子邮件,即在此注册的用户 系统
而不是
text:
我必须发送 令牌 .
所以这是注册部分:
router.post("/register", async (req, res) => {
const { fullName, emailAddress, password } = req.body;
const user = await Users.findOne({
where: {
[Op.and]: [{ fullName: fullName }, { emailAddress: emailAddress }],
},
});
if (user) {
res.status(400).send({
error: `some message.`,
});
} else {
bcrypt
.hash(password, 10)
.then((hash) => {
return {
fullName: fullName,
emailAddress: emailAddress,
password: hash,
isVerified: false,
};
})
.then((user) => {
const token = TokenGenerator.generate();
const creator = Verifications.belongsTo(Users, { as: "user" });
return Verifications.create(
{
token,
user: user,
},
{
include: [creator],
}
);
})
.then((verification) => {
console.log("verification", verification);
sendMail();
})
.then(() => res.json("User, Successmessage "));
}
});
但代码不在同一个文件中。
只需将您需要的参数添加到sendMail
函数中即可:
function sendMail(user, token) {
const msg = {
to: user.emailAddress,
from: "myemail@gmail.com",
subject: "Sending with SendGrid is Fun",
text: token,
html: `<strong>${token}</strong>`,
};
sgMail
.send(msg)
.then(() => {
console.log("Email sent");
})
.catch((error) => {
console.error(error);
});
}
同样在promise中注入需要的参数:
.then(async (user) => {
const token = TokenGenerator.generate();
const creator = Verifications.belongsTo(Users, { as: "user" });
await Verifications.create(
{
token,
user: user,
},
{
include: [creator],
}
);
return {user, token};
})
.then(({user, token}) => {
sendMail(user, token);
})
.then(() => res.json("User, Successmessage "));