英文:
Not able to set the mailgun key and user name setup
问题
我在我的数据库中有Mailgun设置配置。我想要的是在邮件发送给客户之前,应该从数据库中获取Mailgun客户端密钥、用户名和电子邮件。但我无法设置这些数据。from: mailgun_email,
这部分也是从数据库中获取的。请帮助我如何设置这些数据。还有一件事,如果我在邮件文件之外调用配置文件,它会给我一个Mongoose错误,我已经了解到这是因为配置文件在数据库设置之前被调用,导致了一个缓冲超时错误。
错误 MongooseError: 在10000毫秒后操作configurations.findOne()缓冲超时
邮件文件
let mg;
const configuration = async () => {
const res = await Configuration.findById({ _id: process.env.CONFIGURATION_ID });
let key = res.mailgun_api_key;
let username = res.mailgun_username;
return mailgun.client({
username,
key,
});
};
const successOnRegisterMail = (user_email, user_name) => {
configuration();
try {
mg.messages
.create("sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org", {
from: mailgun_email,
to: [user_email],
subject: "",
text: "恭喜您成功注册。",
html: `<p>Hello ${user_name}</p> <br /><br /> <p>恭喜您成功注册。</p><br /> Best Regards, <br /> Team Developers`,
})
.then((msg) => console.log(msg)) // log response data
.catch((error) => console.log(error)); // log any error
} catch (err) {
console.log("Mail错误:", err);
}
};
请注意,我只翻译了您提供的代码部分,不包括其他内容。
英文:
I have Mailgun settings configuration in my database. All I want to the Mailgun Client key, username, and email should be fetched from the database before any mail goes to the client. But I cannot set the data. from: mailgun_email,
this part also we fetched from the database. Please help how can I set the data. One more thing if I call the configuration file outside the email file it gives me an error mongoose error which is what I have understand that is the configuration file is called before the database has been set a buffering timeout error.
Error MongooseError: Operation configurations.findOne() buffering timed out after 10000ms
Email file
let mg;
const configuration = async () => {
const res = await Configuration.findById({ _id: process.env.CONFIGURATION_ID });
let key = res.mailgun_api_key;
let username = res.mailgun_username;
return mailgun.client({
username,
key,
});
};
const successOnRegisterMail = (user_email, user_name) => {
configuration();
try {
mg.messages
.create("sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org", {
from: mailgun_email,
to: [user_email],
subject: "",
text: "Congratulation you've been successfully registered.",
html: `<p>Hello ${user_name}</p> <br /><br /> <p>Congratulation you've been successfully registered.</p><br /> Best Regards, <br /> Team Developers`,
})
.then((msg) => console.log(msg)) // log response data
.catch((error) => console.log(error)); // log any error
} catch (err) {
console.log("Error from Mail: ", err);
}
};
答案1
得分: 0
好的,以下是您要翻译的代码部分:
const configuration = async () => {
const res = await Configuration.findById({ _id: process.env.CONFIGURATION_ID });
const mg = mailgun.client({
username: res.mailgun_username,
key: res.mailgun_api_key,
});
if (res) {
return {
mg,
email_id: res.email_id_for_mail_sent,
};
} else {
return console.log("Error Unable To Fetched Data");
}
};
Using this inside the mail
const mailgunData = await configuration();
mailgunData.mg.messages
.create("sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org", {
from: mailgunData.email_id,
to: [user_email],
subject: "",
请注意,我已将HTML实体编码(如>
和"
)还原为原始文本。如果您需要进一步的翻译,请提供更多信息。
英文:
Okay after multiple try I figured out the answer.
const configuration = async () => {
const res = await Configuration.findById({ _id: process.env.CONFIGURATION_ID });
const mg = mailgun.client({
username: res.mailgun_username,
key: res.mailgun_api_key,
});
if (res) {
return {
mg,
email_id: res.email_id_for_mail_sent,
};
} else {
return console.log("Error Unable To Fetched Data");
}
};
Using this inside the mail
const mailgunData = await configuration();
mailgunData.mg.messages
.create("sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org", {
from: mailgunData.email_id,
to: [user_email],
subject: "",
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论