Mailgun EU 域名在 Node.js 中抛出 401

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

我正在集成 mailgun eu 域,但我不知道为什么 eu 域在我的 node.js 应用程序中不起作用。我的 mailgun 域名也已验证。

当我尝试使用 mailgun api 创建消息时,它总是抛出 401 Forbidden 错误。

我也检查了我的

MAILGUN_SECRET_KEY
,也是正确的。

这是我的代码

import 'dotenv/config'
import formData from 'form-data';
import Mailgun from 'mailgun.js';

class MailgunService {    

    constructor() {
        this.apiKey = process.env.MAILGUN_SECRET_KEY;
        this.domain = process.env.MAILGUN_DOMAIN;
        this.mailgunClient = this.setupClient();
    }

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

    sendMail() {
        const messageData = {
            from: "replace_with_your_email",
            to: "replace_with_your_email",
            subject: "Hello",
            text: "Testing some Mailgun awesomeness!"
        };

        this.mailgunClient.messages.create(this.domain, messageData)
            .then(msg => console.log(msg)) // logs response data
            .catch(err => console.log(err)); // logs any error
    }
}

export default MailgunService;

错误:

我希望我可以使用欧盟域发送电子邮件。

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

这可能是因为您需要通过 IP 允许列表 使用 Mailgun 明确将服务器的 IP 地址列入 白名单

    前往以下页面:
  • https://app.mailgun.com/app/account/security/api_keys
  • 单击 API 安全选项卡
  • 在 IP 白名单部分中,单击添加到白名单按钮。
  • 最后,输入IP / CIDR并单击保存IP地址/CIDR按钮。
© www.soinside.com 2019 - 2024. All rights reserved.