# Mailchimp Transactional

> For the complete documentation index, see [llms.txt](https://effectkit.dev/llms.txt).

Send email through Mailchimp Transactional with the @effect-kit/emails/mailchimp-transactional Layer.

Source: https://effectkit.dev/docs/emails/providers/mailchimp-transactional

The `@effect-kit/emails/mailchimp-transactional` subpath sends messages through [Mailchimp Transactional](https://mailchimp.com/developer/transactional/) and provides the `Email` service.

## Installation [#installation]

```package-install
@effect-kit/emails effect @mailchimp/mailchimp_transactional@1.4.1
```

The SDK is a pinned optional peer. It is needed only when you import this subpath.

## Setup [#setup]

`MailchimpTransactional.layerConfig()` reads these variables:

| Variable                          | Required | Value   |
| --------------------------------- | -------- | ------- |
| `MAILCHIMP_TRANSACTIONAL_API_KEY` | yes      | API key |

**Example** (Sending a Message With Mailchimp Transactional)

```ts twoslash
import { ConfigProvider, Effect, Layer } from "effect";
import { Email } from "@effect-kit/emails";
import * as MailchimpTransactional from "@effect-kit/emails/mailchimp-transactional";

const EmailLive = MailchimpTransactional.layerConfig().pipe(
  Layer.provide(ConfigProvider.layer(ConfigProvider.fromEnv())),
);

const program = Effect.gen(function* () {
  const email = yield* Email;
  return yield* email.send({
    from: "hello@example.com",
    to: "ada@example.com",
    subject: "Welcome",
    text: "Your account is ready.",
  });
});

Effect.runPromise(program.pipe(Effect.provide(EmailLive)));
```

Use a `from` address your Mailchimp Transactional account is allowed to send from.

## Options [#options]

`MailchimpTransactional.layer` takes these options. `layerConfig` builds the same object from a `Config`.

<TypeTable
  type={{
    apiKey: {
      type: "Redacted<string>",
      description: "The API key. Must be printable ASCII with no spaces.",
      required: true,
    },
    baseUrl: {
      type: "URL",
      description: "API base URL. Must be http or https with no credentials, query or fragment.",
      default: "https://mandrillapp.com/api/1.4",
      required: false,
    },
    timeout: {
      type: "Duration",
      description:
        "Time allowed for one send. Must be finite and positive. When it elapses the send fails with TimeoutError.",
      default: "30 seconds",
      required: false,
    },
  }}
/>

## Capabilities [#capabilities]

Mailchimp Transactional reports this in `email.capabilities`:

| Text | HTML | Both | Sender name | Cc  | Bcc | Reply-to | Headers | Attachments | Inline images | Idempotency |
| ---- | ---- | ---- | ----------- | --- | --- | -------- | ------- | ----------- | ------------- | ----------- |
| yes  | yes  | yes  | yes         | yes | yes | multiple | yes     | yes         | no            | no          |

These fail with `UnsupportedFeatureError` and `acceptance: "notSubmitted"` before any request:

- An attachment with a `contentId` (feature `inlineAttachments`).
- An `idempotencyKey` (feature `idempotency`).

## Idempotency [#idempotency]

Not supported. A key fails with `UnsupportedFeatureError` before sending.

## Provider notes [#provider-notes]

- Recipients are sent with `preserve_recipients: true`, so every `to`, `cc` and `bcc` recipient is listed in the message headers each recipient sees.
- Mailchimp answers per recipient. The statuses `sent`, `queued` and `scheduled` count as accepted, and `rejected` and `invalid` as rejected. Any other status fails with `UnexpectedResponseError`.
- When some recipients are rejected the receipt is `partiallyAccepted` with one outcome per recipient. When all are rejected the send fails with `MessageRejectedError`. A `hard_bounce` reject reason is kept as `rejectionCode`.

## Using your own client [#using-your-own-client]

`layerClient` takes any object with `messages.send(body)`, which the client returned by the SDK has. `options` is optional.

**Example** (Borrowing a Mailchimp Transactional Client)

```ts
import mailchimp from "@mailchimp/mailchimp_transactional";
import * as MailchimpTransactional from "@effect-kit/emails/mailchimp-transactional";

const client = mailchimp("fake-key");

const EmailLive = MailchimpTransactional.layerClient(client);
```

You own a borrowed client. The Layer never closes it, and the client keeps its own retry, timeout and logging policy.

## Errors [#errors]

`ProviderConfigurationError` carries `provider: "mailchimp-transactional"` and a `field` naming the bad option. Layer construction fails with it, and `layerClient` fails with it for the client options.

| `field`   | Raised when                                                              |
| --------- | ------------------------------------------------------------------------ |
| `timeout` | The timeout is not finite and positive.                                  |
| `token`   | The API key is not printable ASCII without spaces.                       |
| `baseUrl` | The URL is not http or https, or has credentials, a query or a fragment. |

| Mailchimp response                            | Reason                 |
| --------------------------------------------- | ---------------------- |
| `status: "error"` with name `Invalid_Key`     | `AuthenticationError`  |
| `status: "error"` with name `PaymentRequired` | `QuotaExceededError`   |
| Every recipient `rejected` or `invalid`       | `MessageRejectedError` |

Other `status: "error"` responses fail with `ProviderError` and `acceptance: "rejected"`. The error name is kept as `providerCode` only for the two names above.

Any other HTTP status uses the shared mapping: 401 is `AuthenticationError`, 403 `PermissionDeniedError`, 429 `RateLimitedError`, 400 and 422 `InvalidMessageError`, 5xx `ProviderUnavailableError`, and anything else `ProviderError`. `acceptance` is `rejected` for a 4xx status other than 409 and `unknown` otherwise. See [Errors](/docs/emails/errors). A network failure with no status is `TransportError`, and `ETIMEDOUT` or `ECONNABORTED` is `TimeoutError`, both with `acceptance: "unknown"`.
