mirror of
https://github.com/resend/react-email.git
synced 2026-09-14 14:18:02 +08:00
9ba3de9811
Co-authored-by: gabriel miranda <gabriel@resend.com>
84 lines
1.8 KiB
Plaintext
84 lines
1.8 KiB
Plaintext
---
|
|
title: 'Send email using Mailgun'
|
|
sidebarTitle: 'Mailgun'
|
|
description: 'Learn how to send an email using React Email and the Mailgun JavaScript SDK.'
|
|
'og:image': 'https://react.email/static/covers/react-email.png'
|
|
---
|
|
|
|
## 1. Install dependencies
|
|
|
|
Get the [react-email](https://www.npmjs.com/package/react-email), [mailgun.js](https://www.npmjs.com/package/mailgun.js), and [form-data](https://www.npmjs.com/package/form-data) packages.
|
|
|
|
<CodeGroup>
|
|
|
|
```sh npm
|
|
npm install mailgun.js form-data react-email
|
|
```
|
|
|
|
```sh yarn
|
|
yarn add mailgun.js form-data react-email
|
|
```
|
|
|
|
```sh pnpm
|
|
pnpm add mailgun.js form-data react-email
|
|
```
|
|
|
|
</CodeGroup>
|
|
|
|
## 2. Create an email using React
|
|
|
|
Start by building your email template in a `.jsx` or `.tsx` file.
|
|
|
|
```tsx email.tsx
|
|
import { Button, Html } from 'react-email';
|
|
|
|
interface EmailProps {
|
|
url: string;
|
|
}
|
|
|
|
export function Email({ url }: EmailProps) {
|
|
return (
|
|
<Html lang="en">
|
|
<Button href={url}>Click me</Button>
|
|
</Html>
|
|
);
|
|
}
|
|
```
|
|
|
|
## 3. Convert to HTML and send email
|
|
|
|
Import the email template you just built, convert it into an HTML string, and use the Mailgun SDK to send it.
|
|
|
|
```tsx
|
|
import { render } from 'react-email';
|
|
import FormData from 'form-data';
|
|
import Mailgun from 'mailgun.js';
|
|
import { Email } from './email';
|
|
|
|
const mailgun = new Mailgun(FormData);
|
|
const client = mailgun.client({
|
|
username: 'api',
|
|
key: process.env.MAILGUN_API_KEY,
|
|
});
|
|
|
|
const emailHtml = await render(<Email url="https://example.com" />);
|
|
|
|
await client.messages.create(process.env.MAILGUN_DOMAIN, {
|
|
from: 'you@example.com',
|
|
to: ['user@gmail.com'],
|
|
subject: 'hello world',
|
|
html: emailHtml,
|
|
});
|
|
```
|
|
|
|
## Try it yourself
|
|
|
|
<Card
|
|
title="Mailgun example"
|
|
icon="arrow-up-right-from-square"
|
|
iconType="duotone"
|
|
href="https://github.com/resend/react-email/tree/main/examples/mailgun"
|
|
>
|
|
See the full source code.
|
|
</Card>
|