Skip to content

Commit

Permalink
Official Mailgun SDK + EU host support (#2303)
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho authored Sep 25, 2024
1 parent 5a79eca commit d092fef
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { NodeMailgun } from "ts-mailgun";
import Mailgun from 'mailgun.js';
import { getDefaultFromField } from "../helpers.js";
import type { MailgunEmailProvider, EmailSender } from "../types";

// PRIVATE API
export function initMailgunEmailSender(
config: MailgunEmailProvider
): EmailSender {
const mailer = new NodeMailgun(config.apiKey, config.domain);

const defaultFromField = getDefaultFromField();

const mailgun = new Mailgun(FormData);

const mailer = mailgun.client({
username: 'api',
key: config.apiKey,
url: config.apiUrl,
});

return {
async send(email) {
const fromField = email.from || defaultFromField;
mailer.fromEmail = fromField.email;
mailer.fromTitle = fromField.name;
mailer.init();
return mailer.send(email.to, email.subject, email.html);
return mailer.messages.create(config.domain, {
from: `${fromField.name} <${fromField.email}>`,
to: [email.to],
subject: email.subject,
text: email.text,
html: email.html,
})
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type MailgunEmailProvider = {
type: "mailgun";
apiKey: string;
domain: string;
apiUrl?: string;
};

// PRIVATE API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const emailProvider = {
type: "mailgun",
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN,
apiUrl: process.env.MAILGUN_API_URL,
} as const;
{=/ isMailgunProviderUsed =}
{=# isDummyProviderUsed =}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions waspc/examples/todoApp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ mailgun =
}
where
mailgunVersionRange :: SV.Range
mailgunVersionRange = SV.Range [SV.backwardsCompatibleWith (SV.Version 0 5 1)]
mailgunVersionRange = SV.Range [SV.backwardsCompatibleWith (SV.Version 10 2 3)]

mailgunDependency :: AS.Dependency.Dependency
mailgunDependency = AS.Dependency.make ("ts-mailgun", show mailgunVersionRange)
mailgunDependency = AS.Dependency.make ("mailgun.js", show mailgunVersionRange)

dummy :: EmailSenderProvider
dummy =
Expand Down
17 changes: 13 additions & 4 deletions web/docs/advanced/email/email.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,25 @@ Then, get the Mailgun API key and domain and add them to your `.env.server` file
#### Getting the API Key and Domain

1. Go to [Mailgun](https://www.mailgun.com/) and create an account.
2. Go to [API Keys](https://app.mailgun.com/app/account/security/api_keys) and create a new API key.
3. Copy the API key and add it to your `.env.server` file.
4. Go to [Domains](https://app.mailgun.com/mg/sending/domains) and create a new domain.
5. Copy the domain and add it to your `.env.server` file.
1. Go to [Domains](https://app.mailgun.com/mg/sending/new-domain) and create a new domain.
1. Copy the domain and add it to your `.env.server` file.
1. Create a new Sending API key under `Send > Sending > Domain settings` and find `Sending API keys`.
1. Copy the API key and add it to your `.env.server` file.

```properties title=".env.server"
MAILGUN_API_KEY=
MAILGUN_DOMAIN=
```

#### Using the EU Region

If your domain region is in the EU, you need to set the `MAILGUN_API_URL` variable in your `.env.server` file:

```properties title=".env.server"
MAILGUN_API_URL=https://api.eu.mailgun.net
```


### Using the SendGrid Provider

Set the provider field to `SendGrid` in your `main.wasp` file.
Expand Down

0 comments on commit d092fef

Please sign in to comment.