基本的 mailgun.js 节点设置在欧盟不起作用

问题描述 投票:0回答:1

我尝试从他们的 npmjs 文档运行此代码。

const mailgun = new Mailgun(FormData);
const mg = mailgun.client({ username: 'api', key: MY_MAILGUN_API, url: 'https://api.eu.mailgun.net'})
mg.messages.create('sandbox-123.mailgun.org', {
      from: "Excited User <[email protected]>",
      to: ["[email protected]"],
      subject: "Hello",
      text: "Testing some Mailgun awesomness!",
      html: "<h1>Testing some Mailgun awesomness!</h1>"
    })
 .then(msg => console.log(msg)) // logs response data
 .catch(err => console.error(err)); // logs any error

它会抛出错误

[Error: Unauthorized] {
  status: 401,
  details: 'Forbidden',
  type: 'MailgunAPIError'
}

我不知道问题出在哪里。用谷歌搜索,没有找到与我相关的信息。

MY_MAILGUN_API - 从我的帐户复制

[电子邮件受保护] - 在代码中我有可以访问的电子邮件

node.js mailgun mailgun-api
1个回答
0
投票

401错误表示发送API密钥无效

在 Mailgun 主页中,您必须转到“发送部分”并选择您要发送电子邮件的域。然后单击“域设置”并单击“发送 API 密钥”。您必须创建一个新的发送密钥。你应该没有问题。

创建新的发送 api 密钥的简单步骤

  • 仪表板>>域名>>选择您的域名>>域名设置>>发送>>添加发送密钥

示例代码:

const SENDING_KEY = 'YOUR_SENDING_KEY';
const DOMAIN = 'YOUR_DOMAIN_NAME';

const mailgun = new Mailgun(formData);
const client = mailgun.client({
    username: 'api',
    key: SENDING_KEY,
    url: 'https://api.eu.mailgun.net'
});

const messageData = {
  from: 'Excited User <[email protected]>',
  to: '[email protected], [email protected]',
  subject: 'Hello',
  text: 'Testing some Mailgun awesomeness!'
};

client.messages.create(DOMAIN, messageData)
 .then((res) => {
   console.log(res);
 })
 .catch((err) => {
   console.error(err);
 });
© www.soinside.com 2019 - 2024. All rights reserved.