# Send emails in a batch

`opensend emails batch`

Sends up to a hundred emails in one request. Every email is checked first and one that is refused refuses the whole batch, unless `validation` is `permissive`: then the rest are sent and the refused one is listed with its reason. After that each email is accepted or refused by itself, and the answer says which.

Needs the scope: `emails:send`

## Flags

- `--emails` (object[], required): The emails to send, up to 100. Each takes the same fields as one email, without attachments or a schedule.
- `emails[].from` (string): The sender, on a verified domain. An address, or a name and an address as `Acme <hello@mail.example.com>`. Needed unless a template gives one. Up to 500 characters.
- `emails[].to` (string | string[], required): Who the email is for: one address, or up to 50. A test address, such as `delivered@opensend.test` or `bounced@opensend.test`, reaches nobody and ends the way its name says.
- `emails[].cc` (string | string[]): Who gets a copy: one address, or up to 50.
- `emails[].bcc` (string | string[]): Who gets a copy without the others seeing: one address, or up to 50.
- `emails[].replyTo` (string): The address replies go to, when it is not the sender. Without it, the one of the template when it has one, then the default of the workspace.
- `emails[].subject` (string): The subject line. Needed unless a template gives one. Up to 500 characters.
- `emails[].html` (string): The message as HTML. Up to 200,000 characters.
- `emails[].text` (string): The message as plain text. Send it with `html`, or alone. With a template it replaces the plain text of the template. Up to 200,000 characters.
- `emails[].headers` (Record<string, string>): Extra headers for the message, by name. Up to 20, and not the ones the platform writes itself.
- `emails[].tags` (Record<string, string>): Labels of your own for the email, by name, such as `{ "campaign": "welcome" }`. Up to 10. They come back on the email and in webhooks.
- `emails[].template` (object): A published template to build the email from, in place of `subject`, `html` and `text`. Its sender and reply address are used when you give none.
- `emails[].template.id` (string, required): The id or the alias of a published template. Up to 500 characters.
- `emails[].template.variables` (Record<string, string | number>): A value for each `{{NAME}}` in the template, by name. A variable with a default in the template can be left out.
- `--validation` (string): What one refused email does to the others: `strict` refuses the whole batch and sends nothing, `permissive` sends the rest and lists the refused one with its reason. Without it you get `strict`. One of: `strict`, `permissive`.
- `--idempotency-key` (string): A key of your own that makes a repeat of this request answer with the first result and send nothing. The `Idempotency-Key` header does the same.

## Returns

The command prints the answer: as JSON when its output goes to a file or another program, and as a table or a list of fields in a terminal. When the API refuses, the error goes to the error stream and the command ends with a code that names the failure. [Output and scripts](https://opensend.sh/docs/scripting) lists the codes.

- `object` ("list"): Always `list`.
- `data` (object[]): One entry for each email you sent, in the same order.
- `data[].index` (integer): The position of this email in the list you sent, from 0.
- `data[].id` (string | null): The id of the email, or `null` when it was refused.
- `data[].status` (string | null): The status of the email, or `null` when it was refused. One of: `queued`, `sent`, `delivered`, `delayed`, `bounced`, `opened`, `clicked`, `complained`, `failed`, `scheduled`, `canceled`.
- `data[].mode` (string | null): `live` for a real email, `test` for an email of the sandbox, or `null` when it was refused. One of: `live`, `test`.
- `data[].error` (object | null): Why it was refused, or `null`.
- `data[].error.code` (string): The code of the refusal.
- `data[].error.message` (string): The refusal in words.
- `sent` (integer): How many were accepted.
- `failed` (integer): How many were refused.

## Example

CLI:

```bash
opensend emails batch \
  --emails '[{"from":"Acme <hello@mail.example.com>","to":"ada@example.com","subject":"Your receipt for November","html":"<p>Thanks for your order.</p>"}]'
```

Answer:

```json
{
  "object": "list",
  "data": [
    {
      "index": 0,
      "id": "em_10",
      "status": "queued",
      "mode": "live",
      "error": null
    }
  ],
  "sent": 1,
  "failed": 0
}
```
