Skip to content

Commit e0168ed

Browse files
feat(providers): Mailgun region selection (#13027)
Fix(provider): Mailgun region server Co-authored-by: Thang Vu <[email protected]>
1 parent 07b7d93 commit e0168ed

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

docs/pages/getting-started/providers/mailgun.mdx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ AUTH_MAILGUN_KEY=abc
4141

4242
If you name your environment variable `AUTH_MAILGUN_KEY`, the provider will pick it up automatically and your Auth.js configuration object can be simpler. If you'd like to rename it to something else, however, you'll have to manually pass it into the provider in your Auth.js configuration.
4343

44+
3. If you are using the EU Mailgun server, you will need to include `region: "EU"` in the provider options. If you are using the US Mailgun server you can remove this option.
45+
4446
<Code>
4547
<Code.Next>
4648

@@ -53,8 +55,9 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
5355
providers: [
5456
Mailgun({
5557
// If your environment variable is named differently than default
56-
apiKey: import.meta.env.AUTH_MAILGUN_KEY,
57-
58+
apiKey: process.env.AUTH_MAILGUN_KEY,
59+
60+
region: "EU", // Optional
5861
}),
5962
],
6063
})
@@ -74,6 +77,7 @@ export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
7477
// If your environment variable is named differently than default
7578
apiKey: import.meta.env.AUTH_MAILGUN_KEY,
7679
80+
region: "EU", // Optional
7781
}),
7882
],
7983
})
@@ -95,6 +99,7 @@ export const { handle, signIn, signOut } = SvelteKitAuth({
9599
// If your environment variable is named differently than default
96100
apiKey: env.AUTH_MAILGUN_KEY,
97101
102+
region: "EU", // Optional
98103
}),
99104
],
100105
})

packages/core/src/providers/mailgun.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import { html, text } from "../lib/utils/email.js"
2424
* const request = new Request(origin)
2525
* const response = await Auth(request, {
2626
* providers: [
27-
* Mailgun({ from: MAILGUN_DOMAIN }),
27+
* Mailgun({
28+
* from: MAILGUN_DOMAIN,
29+
* region: "EU", // Optional
30+
* }),
2831
* ],
2932
* })
3033
* ```
@@ -43,7 +46,23 @@ import { html, text } from "../lib/utils/email.js"
4346
*
4447
* :::
4548
*/
46-
export default function MailGun(config: EmailUserConfig): EmailConfig {
49+
export default function MailGun(
50+
config: EmailUserConfig & {
51+
/**
52+
* https://documentation.mailgun.com/docs/mailgun/api-reference/#base-url
53+
*
54+
* @default "US"
55+
*/
56+
region?: "US" | "EU"
57+
}
58+
): EmailConfig {
59+
const { region = "US" } = config
60+
const servers = {
61+
US: "api.mailgun.net",
62+
EU: "api.eu.mailgun.net",
63+
}
64+
const apiServer = servers[region]
65+
4766
return {
4867
id: "mailgun",
4968
type: "email",
@@ -64,7 +83,7 @@ export default function MailGun(config: EmailUserConfig): EmailConfig {
6483
form.append("html", html({ host, url, theme }))
6584
form.append("text", text({ host, url }))
6685

67-
const res = await fetch(`https://api.mailgun.net/v3/${domain}/messages`, {
86+
const res = await fetch(`https://${apiServer}/v3/${domain}/messages`, {
6887
method: "POST",
6988
headers: {
7089
Authorization: `Basic ${btoa(`api:${provider.apiKey}`)}`,

0 commit comments

Comments
 (0)