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

# Sending Emails via Mailgun

> Send emails through Mailgun using the SendPost API with IP Pool routing.

Once your Mailgun provider and IP Pool are configured, you can send emails through Mailgun using the standard SendPost API.

<Note>
  **Important**: You must include the `ippool` parameter in your API request to route emails through Mailgun.
</Note>

***

## Basic Email Example

```bash theme={null}
curl -X POST "https://api.sendpost.io/api/v1/subaccount/email/" \
  -H "X-SubAccount-ApiKey: YOUR_SENDPOST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": {
      "email": "sender@yourdomain.com",
      "name": "Your Name"
    },
    "to": [{
      "email": "recipient@example.com",
      "name": "Recipient Name"
    }],
    "subject": "Hello from SendPost via Mailgun",
    "htmlBody": "<h1>Hello!</h1><p>This email is sent through Mailgun.</p>",
    "textBody": "Hello! This email is sent through Mailgun.",
    "ippool": "mailgun-pool",
    "trackOpens": true,
    "trackClicks": true
  }'
```

***

## Email with Personalization

```bash theme={null}
curl -X POST "https://api.sendpost.io/api/v1/subaccount/email/" \
  -H "X-SubAccount-ApiKey: YOUR_SENDPOST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": {
      "email": "sender@yourdomain.com",
      "name": "Your Company"
    },
    "to": [{
      "email": "john@example.com",
      "name": "John Doe",
      "customFields": {
        "firstName": "John",
        "accountType": "Premium"
      }
    }],
    "subject": "Welcome {{firstName}}!",
    "htmlBody": "<h1>Welcome {{firstName}}!</h1><p>Your {{accountType}} account is ready.</p>",
    "ippool": "mailgun-pool",
    "trackOpens": true,
    "trackClicks": true
  }'
```

***

## Using Mailgun IP Pools

If you have IP Pools configured in your Mailgun account, you can specify which Mailgun IP Pool to use via the `tpspIppool` parameter.

<Info>
  The `tpspIppool` field is optional. Use it when you want to route emails through a specific IP Pool on the Mailgun side, separate from the SendPost IP Pool routing.
</Info>

**Parameters**:

* `ippool`: The SendPost IP Pool that routes traffic to Mailgun (required)
* `tpspIppool`: The IP Pool name configured in your Mailgun account (optional)

```bash theme={null}
curl -X POST "https://api.sendpost.io/api/v1/subaccount/email/" \
  -H "X-SubAccount-ApiKey: YOUR_SENDPOST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": {
      "email": "sender@yourdomain.com",
      "name": "Your Company"
    },
    "to": [{
      "email": "recipient@example.com",
      "name": "Recipient Name"
    }],
    "subject": "Hello from SendPost via Mailgun",
    "htmlBody": "<h1>Hello!</h1><p>This email is sent through a specific Mailgun IP Pool.</p>",
    "ippool": "mailgun-pool",
    "tpspIppool": "transactional",
    "trackOpens": true,
    "trackClicks": true
  }'
```

***

## Using a Specific Mailgun IP Address

If you have dedicated IPs in your Mailgun account, you can specify a specific IP address to use for sending via the `tpspIp` parameter.

<Warning>
  The IP address specified must be owned by your Mailgun account. This is different from IP pools which allow Mailgun to select an IP from a pool.
</Warning>

**Parameters**:

* `ippool`: The SendPost IP Pool that routes traffic to Mailgun (required)
* `tpspIp`: The specific IP address owned by your Mailgun account (optional)

```bash theme={null}
curl -X POST "https://api.sendpost.io/api/v1/subaccount/email/" \
  -H "X-SubAccount-ApiKey: YOUR_SENDPOST_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": {
      "email": "sender@yourdomain.com",
      "name": "Your Company"
    },
    "to": [{
      "email": "recipient@example.com",
      "name": "Recipient Name"
    }],
    "subject": "Hello from SendPost via Mailgun",
    "htmlBody": "<h1>Hello!</h1><p>This email is sent from a specific IP.</p>",
    "ippool": "mailgun-pool",
    "tpspIp": "192.0.2.1",
    "trackOpens": true,
    "trackClicks": true
  }'
```

***

## Response

**Success Response** (HTTP 200):

```json theme={null}
[
  {
    "messageId": "abc123-def456-ghi789",
    "errorCode": 0,
    "to": "recipient@example.com"
  }
]
```

***

## Next Steps

* [Understand event types](/guides/mailgun/event-types) for tracking email status
* [View analytics](/guides/mailgun/analytics) for your Mailgun emails
