无法设置邮件发送密钥和用户名设置

huangapple go评论57阅读模式
英文:

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 () =&gt; {
	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) =&gt; {
	configuration();
	try {
		mg.messages
			.create(&quot;sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org&quot;, {
				from: mailgun_email,
				to: [user_email],
				subject: &quot;&quot;,
				text: &quot;Congratulation you&#39;ve been successfully registered.&quot;,
				html: `&lt;p&gt;Hello ${user_name}&lt;/p&gt; &lt;br /&gt;&lt;br /&gt; &lt;p&gt;Congratulation you&#39;ve been successfully registered.&lt;/p&gt;&lt;br /&gt; Best Regards, &lt;br /&gt; Team Developers`,
			})
			.then((msg) =&gt; console.log(msg)) // log response data
			.catch((error) =&gt; console.log(error)); // log any error
	} catch (err) {
		console.log(&quot;Error from Mail: &quot;, 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实体编码(如&gt;&quot;)还原为原始文本。如果您需要进一步的翻译,请提供更多信息。

英文:

Okay after multiple try I figured out the answer.

const configuration = async () =&gt; {
	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(&quot;Error Unable To Fetched Data&quot;);
	}
};

Using this inside the mail

const mailgunData = await configuration();

mailgunData.mg.messages
			.create(&quot;sandbox1f0bc2b687fc416cbd9bdf7a7d1bc24c.mailgun.org&quot;, {
				from: mailgunData.email_id,
				to: [user_email],
				subject: &quot;&quot;,

huangapple
  • 本文由 发表于 2023年6月19日 12:44:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定