> ## Documentation Index
> Fetch the complete documentation index at: https://react.email/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Send email using Mailgun

> Learn how to send an email using React Email and the Mailgun JavaScript SDK.

## 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 theme={"theme":{"light":"github-light","dark":"vesper"}}
  npm install mailgun.js form-data react-email
  ```

  ```sh yarn theme={"theme":{"light":"github-light","dark":"vesper"}}
  yarn add mailgun.js form-data react-email
  ```

  ```sh pnpm theme={"theme":{"light":"github-light","dark":"vesper"}}
  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 theme={"theme":{"light":"github-light","dark":"vesper"}}
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 theme={"theme":{"light":"github-light","dark":"vesper"}}
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>
