# Email

Send transactional email from your site's code with `client.email.send()`. Mail always goes out through your site's own email provider, from your site's own sender address.

## Before you send: set up your own sender

A site sends email only through a provider you connect. Perspect never sends your site's email from its own address.

1. Open **Email Settings** in the console and pick your site.
2. Connect a provider (Resend, SendGrid, Mailgun or Amazon SES) with your own API key. Custom providers need a paid plan.
3. Set the site's **Email From Address** to an address on a domain you've verified with that provider.

Until a site has its own provider, it can email only verified members of its own organization. That limit applies to everything the site sends, not just `email.send()`:

| Email | Without the site's own provider |
|---|---|
| `client.email.send()` | Refused with `sender_not_configured` |
| Site user login codes (`client.siteUsers.requestOtp`) | Refused with `sender_not_configured`, except for members of your organization |
| Order receipts, cancellation and fulfillment notices | Not sent |
| Subscription notices and team invitations | Not sent (the invitation is still created) |
| Newsletter confirmations and campaigns | Not sent |
| Contact form and new order alerts to your team | Sent, to verified members of your organization only |

## Quick Start

```typescript
import { PerspectApiV2Client } from 'perspectapi-ts-sdk';

const client = new PerspectApiV2Client({
  baseUrl: 'https://api.example.com',
  apiKey: env.PERSPECT_API_KEY
});

const result = await client.email.send('my-site', {
  to: 'customer@example.com',
  subject: 'Your report is ready',
  html: '<p>Your report is ready to download.</p>',
  text: 'Your report is ready to download.'
});

console.log(result.id);     // the provider's message id
console.log(result.status); // "sent"
```

## API Reference

### `email.send(siteName, params)`

| Field | Type | Notes |
|---|---|---|
| `to` | `string \| string[]` | Required. Up to 50 addresses. |
| `subject` | `string` | Required unless you send a template. |
| `html`, `text` | `string` | Provide one or both, or use a template instead. |
| `template_type` / `template_id` | `string` | Send a published email template instead of `html`/`text`. |
| `variables` | `object` | Values for the template's placeholders. |
| `from` | `string` | Optional. If set, it must be your site's own sender address. |
| `reply_to` | `string` | Optional. |
| `cc`, `bcc` | `string \| string[]` | Optional. Up to 50 addresses each. |

The sender is always your site's configured address and display name. A different `from` is refused, and a display name in `from` is ignored.

## Error Handling

```typescript
import { PerspectV2Error } from 'perspectapi-ts-sdk';

try {
  await client.email.send('my-site', { to, subject, text });
} catch (err) {
  if (err instanceof PerspectV2Error) {
    switch (err.code) {
      case 'sender_not_configured':
        // The site has no email provider or sender address of its own, or is on
        // a free plan. Configure one in Email Settings; retrying won't help.
        break;
      case 'sender_not_allowed':
        // `from` was not the site's own sender address.
        break;
      case 'provider_error':
        // Your provider refused or failed the send (HTTP 502).
        break;
    }
  }
}
```

| Code | Status | Meaning |
|---|---|---|
| `sender_not_configured` | 422 | No provider or sender address of the site's own, or a free plan |
| `sender_not_allowed` | 403 | `from` isn't the site's sender address |
| `template_not_found` | 404 | No such template, or none published for that type |
| `provider_error` | 502 | The provider returned an error |
