# sendEmail mutation


`sendEmail` is a GraphQL mutation of the Chameleoon MAX API. Send it as `POST https://max-api.chameleoon.sk/api/v1/graphql` with your API key, see [Chameleoon MAX](/chameleoon-max).

Renders an e-mail and queues it for delivery. Call it to send a message about a record, with a
template or with your own subject and body.

The call either fails with one of the errors below before anything is stored, or returns the
message with the status `QUEUED`. MAX tries delivery up to 8 times, and the message then turns
`SENT` or `FAILED`. Read it with [`emailMessage`](/chameleoon-max/query/email-message).

#### Permissions

- Requires the `email.send` permission.
- The `resource` requires its read permission: `invoice.read` for `INVOICE`, `order.read` for
  `SALES_ORDER`, `shipment.read` for `SHIPMENT`.
- An `invoicePdf` attachment requires the `invoice.read` permission, and also `price.read` when
  MAX renders the PDF itself.
- An `orderInvoice` attachment requires the `order.read` and `invoice.read` permissions, and
  also `price.read` when MAX renders the PDF itself.
- A `document` attachment requires the permission that reading its record's documents
  requires: `order.read` for an order's, `invoice.read` for an invoice's. An invoice's PDF that
  is a stored document requires the same.
- A `file` attachment must be your API key's own upload (`POST /api/v1/files`, which
  `email.send` also allows).
- Without `price.read`, the template data carries no amounts. Without `company.read`, it
  carries no issuing company, as on an order print.

#### Repeated calls

`requestId` makes a retry after a lost response safe. A repeat with the same `requestId` and
the same input returns `applied: false` and the first call's message as it is now. A repeat
with other input fails with `CONFLICT` and reason `REQUEST_ID_REUSED`.
[`sendTestEmail`](/chameleoon-max/mutation/send-test-email) shares the same `requestId`s.

The response always carries the message's `id` and `status` (and `applied`), also on a repeat.
Its content is returned only when your API key may read the message (see
[`SendEmailResult`](/chameleoon-max/type/send-email-result)).

#### Errors

| Code | Reason | When |
| --- | --- | --- |
| `VALIDATION` | `NO_RECIPIENT` | There is nobody to send to (field `to`). |
| `VALIDATION` | `INVALID_ADDRESS` | An address is not valid (field `to`, `cc` or `bcc`). |
| `VALIDATION` | `TOO_MANY_RECIPIENTS` | `to`, `cc` and `bcc` hold more than 50 addresses in all. |
| `VALIDATION` | `TEMPLATE_ERROR` | A part does not parse or render, for example because of an unclosed `{{` or `{%`. `extensions.part` names the part and, when known, `extensions.line` the line. |
| `VALIDATION` | `EMPTY_BODY` | The HTML body renders to nothing (field `html`). |
| `VALIDATION` | `ATTACHMENTS_TOO_LARGE` | The attachments are over 15 MiB in all. |
| `NOT_FOUND` | `TEMPLATE_NOT_FOUND` | The template does not exist (field `template`). |
| `NOT_FOUND` | | `extensions.field` names what does not exist: `resource`, `sender`, `orderInvoice`, `invoicePdf`, `document` or `file`. |
| `FORBIDDEN_ROLE` | `DOCUMENT_READ_REQUIRED` | An invoice's PDF is a stored document your API key may not read (the invoice's requires `invoice.read`; an order's requires `order.read` and the order in your API key's scope), or the order's governing invoice is outside your API key's scope. Field `attachments`; `extensions.permission` names the permission. MAX never sends its own render or another file in its place. |
| `CONFLICT` | `NO_SENDER` | There is no sender to send through, or the named sender is disabled. |
| `CONFLICT` | `INVOICE_NOT_READY` | The order has no invoice yet. Try again later. |
| `CONFLICT` | `INVOICE_PDF_MISSING` | The accounting system has not sent the invoice's PDF yet, and the invoice's external source does not allow MAX to render it (`ExternalSource.invoicePrintAllowed`). Try again later. |
| `CONFLICT` | `INVOICE_NOT_PRINTABLE` | The invoice is pending, failed or cancelled. |
| `CONFLICT` | `INVOICE_RENDER_BUSY` | MAX is busy rendering PDFs. Try again in a moment. |
| `CONFLICT` | `REQUEST_ID_REUSED` | The `requestId` was used before with other input. |

> An error whose path runs below this field does not mean the call failed: the message was
> queued, so do not retry. See [`Mutation`](/chameleoon-max/mutations).

It may render an invoice PDF.

Costs 11 points plus its selection.

## Arguments

- `input`: [`SendEmailInput!`](/chameleoon-max/type/send-email-input) · required

## Returns

[`SendEmailResult!`](/chameleoon-max/type/send-email-result)

## Example

**Mutation**

```graphql
mutation sendEmail($input: SendEmailInput!) {
  sendEmail(input: $input) {
    applied
    message {
      id
      status
      senderId
      fromAddress
      fromName
      replyTo
    }
  }
}
```

**Variables**

```json
{
  "input": {
    "requestId": "example"
  }
}
```

**Response**

```json
{
  "data": {
    "sendEmail": {
      "applied": true,
      "message": {
        "id": "00000000-0000-4000-8000-000000000001",
        "status": "QUEUED",
        "senderId": "00000000-0000-4000-8000-000000000001",
        "fromAddress": "example",
        "fromName": "example",
        "replyTo": "example"
      }
    }
  }
}
```

Values are illustrative: replace IDs and codes with your own before you send the call.
