Skip to content

Mailgun Provider needs option for EU server (domain is different) #12925

@HexaCubist

Description

@HexaCubist

Provider type

Email

Environment

  System:
    OS: macOS 15.4.1
    CPU: (8) arm64 Apple M1
    Memory: 108.25 MB / 16.00 GB
    Shell: 4.0.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 20.15.1 - /usr/local/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 10.7.0 - /usr/local/bin/npm
    bun: 1.2.4 - ~/.bun/bin/bun
  Browsers:
    Chrome: 135.0.7049.115
    Safari: 18.4
  npmPackages:
    @auth/drizzle-adapter: ^1.9.0 => 1.9.0 
    @auth/sveltekit: ^1.9.1 => 1.9.1

Reproduction URL

https://github.com/HexaCubist/next-auth-mailgun-eu

Describe the issue

Mailgun has two regions to send from since 2018: https://www.mailgun.com/about/regions/

API keys can be created for one region only, and the US region is hard-coded into the provider:

  • US Domain: api.mailgun.net
  • EU Domain: api.eu.mailgun.net

mailgun.ts:

...
      const res = await fetch(`https://api.mailgun.net/v3/${domain}/messages`, {
...

How to reproduce

  1. Clone repo
  2. Provide EU API keys for Mailgun
  3. Error:
[auth][error] Error: Mailgun error: Forbidden

Expected behavior

Mailgun provider should have configurable option to select region (EU or US), defaulting to the US.

Activity

added
bugSomething isn't working
triageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
on Apr 29, 2025
HexaCubist

HexaCubist commented on Apr 29, 2025

@HexaCubist
Author

Related: #11923

benhovinga

benhovinga commented on Jun 3, 2025

@benhovinga
Contributor

Possible solution to this issue. This follows a similar setup to the Microsoft Entra ID profilePhotoSize.

In packages/core/src/providers/mailgun.ts

export default function MailGun(
  config: EmailUserConfig & {
    /**
     * https://documentation.mailgun.com/docs/mailgun/api-reference/#base-url
     *
     * @default "US"
     */
    region?: "US" | "EU"
  }
): EmailConfig {
  const { region = "US" } = config
  const servers = {
    US: "api.mailgun.net",
    EU: "api.eu.mailgun.net",
  }
  const apiServer = servers[region]

// ...

const res = await fetch(`https://${apiServer}/v3/${domain}/messages`

// ...

When setting up the provider you would just need to include the region in the config

export const { handlers, auth, signIn, signOut } = NextAuth({
  providers: [
    Mailgun({
      region: "EU",  //  <-- HERE
      apiKey: process.env.AUTH_MAILGUN_KEY,
      from: "no-reply@company.com"
    }),
  ],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingproviderstriageUnseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @HexaCubist@benhovinga

      Issue actions

        Mailgun Provider needs option for EU server (domain is different) · Issue #12925 · nextauthjs/next-auth