node.js - Mailgun-401禁止

标签 node.js nestjs mailgun httpforbiddenhandler

我尝试使用mailgun发送电子邮件。我使用node.js(nest.js),这是我的邮件服务。我应该改变什么?当我尝试发送第一封电子邮件(mailgun官方网站中的描述)时,我收到了相同的错误消息。

import { Injectable } from '@nestjs/common';
import * as Mailgun from 'mailgun-js';
import { IMailGunData } from './interfaces/mail.interface';
import { ConfigService } from '../config/config.service';

@Injectable()
export class MailService {
  private mg: Mailgun.Mailgun;

  constructor(private readonly configService: ConfigService) {
    this.mg = Mailgun({
      apiKey: this.configService.get('MAILGUN_API_KEY'),
      domain: this.configService.get('MAILGUN_API_DOMAIN'),
    });
  }

  send(data: IMailGunData): Promise<Mailgun.messages.SendResponse> {
    console.log(data);
    console.log(this.mg);
    return new Promise((res, rej) => {
      this.mg.messages().send(data, function (error, body) {
        if (error) {
          console.log(error);
          rej(error);
        }
        res(body);
      });
    });
  }
}

当我尝试发送消息时,出现禁止说明的401错误。
我的毫克(console.log(this.mg))
Mailgun {
  username: 'api',
  apiKey: '920d6161ca860e7b84d9de75e14exxx-xxx-xxx',
  publicApiKey: undefined,
  domain: 'lokalne-dobrodziejstwa.pl',
  auth: 'api:920d6161ca860e7b84d9de75e14exxx-xxx-xxx',
  mute: false,
  timeout: undefined,
  host: 'api.mailgun.net',
  endpoint: '/v3',
  protocol: 'https:',
  port: 443,
  retry: 1,
  testMode: undefined,
  testModeLogger: undefined,
  options: {
    host: 'api.mailgun.net',
    endpoint: '/v3',
    protocol: 'https:',
    port: 443,
    auth: 'api:920d6161ca860e7b84d9de75e14exxx-xxx-xxx',
    proxy: undefined,
    timeout: undefined,
    retry: 1,
    testMode: undefined,
    testModeLogger: undefined
  },
  mailgunTokens: {}
}

我的邮件正文
{
  from: 'rejestracja@lokalne-dobrodziejstwa.pl',
  to: 'me@gmail.com',
  subject: 'Verify User',
  html: '\n' +
    '                <h3>Hello me@gmail.com!</h3>\n' +
    '            '
}

最佳答案

当我的域名位于欧盟区域时,我遇到了这个问题。使用EU区域时,必须在配置中指定它-Mailgun对此没有明确说明。
所以会是这样的:

var mailgun = require("mailgun-js")({
  apiKey: API_KEY,
  domain: DOMAIN,
  host: "api.eu.mailgun.net",
});

关于node.js - Mailgun-401禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63489555/

相关文章:

java - 当我尝试发送包含内联图像的电子邮件时(使用 Jersey 2 REST 客户端和 Mailgun 网络服务发送电子邮件),为什么会出现此错误?

javascript - 使用NodeJS(公共(public)应用程序)时如何在shopify中获取当前商店?

node.js - 在 keystone.js 中添加自定义字段类型

azure - CORS:Azure WebApp Docker 容器内的 NestJs,CORS 被拒绝

node.js - 如何从 firebase 存储发送附件并使用 node.js 中的 mailgun 服务将其发送到电子邮件

runtime-error - 邮件枪错误”:"exposed account credentials

node.js - prisma 容器中的 AWS ECS 错误 - 环境变量 PRISMA_CONFIG

javascript - 检测 Mongoose 事件的语法是什么?

reactjs - Ramda 或者不是 Ramda

typescript - 如何在 NestJS 的响应中设置 header 字段?