# Sending mail

The v2 sending endpoints send a draft that you have created in the LianaMailer app. In the
request, you choose the draft and who gets it, and you can override parts of the draft, such as
the subject. The response contains the IDs you need to follow the delivery with the v1 endpoints.

## Choose an endpoint

- **Event send** ([`deliveries/event-send`](/api/sending#send-event-or-transactional-mail)) sends a
  personal or transactional mail, such as an order confirmation, to the contacts in the request.
- **Batch send** ([`deliveries/batch-send`](/api/sending#batch-send-draft)) sends a newsletter to
  mailing lists, to contacts in the request, or both.
- **Web page send** ([`deliveries/webpage-send`](/api/sending#send-web-page)) sends a web page as a
  newsletter, in the same way as batch send.

All three can override the draft's `letter.subject`, `letter.sender` and `letter.preheader_text`.
These fields, like `letter.schedule` and `letter.ab`, go inside `letter`. The same fields at the top
level of the request are ignored, so a top-level `schedule` sends the mail right away.

The main differences between event send and batch send are:

<div className="[&_table]:w-full [&_table]:table-fixed [&_th:first-child]:w-24 sm:[&_th:first-child]:w-36 [&_th_code]:whitespace-normal">

|                  | [<code>deliveries/<wbr />event-send</code>](/api/sending#send-event-or-transactional-mail) | [<code>deliveries/<wbr />batch-send</code>](/api/sending#batch-send-draft) |
| ---------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------- |
| Sends to         | Contacts in `recipients`                                                                   | Lists in `lists`, contacts in `recipients`, or both                        |
| When             | Right away                                                                                 | Right away, or at the time in `letter.schedule`                            |
| A/B subject test | Not available                                                                              | With `letter.ab`                                                           |
| Contacts         | [Found or created](#event-send) one by one                                                 | [Imported](#batch-and-web-page-send) through a hidden list                 |
| Creates          | One event delivery for the draft, reused by each send                                      | A copy of the draft, and a delivery for each list                          |
| Resending        | Fails, unless you [allow it](#event-send)                                                  | Each request is a new delivery                                             |
| Response         | `deliveries`, `mails` and `tracking`                                                       | `id` of the copy, `deliveries` and `mails`                                 |

</div>

Batch send delivers the mail to each contact only once, even if the contact is on several of the
lists. An A/B subject test requires the A/B testing feature and open tracking, and cannot be
combined with `letter.schedule`. It creates a delivery for each subject.

Web page send takes the same `lists`, `recipients`, `letter.schedule` and `letter.ab` as batch
send, but gets its content from the web page at `letter.url`. It saves the page as a new draft and
sends a copy of that draft. The subject is the page's `<title>`, unless you give `letter.subject`.

## Before you send

Create and save the draft in the LianaMailer app, and give its ID in `letter.id`. Drafts with an
A/B content test cannot be sent through the API.

Event send and batch send can also send SMS drafts, to the contacts' SMS numbers. The draft needs
an SMS sender, and you can override the SMS text and sender with `letter.sms` and
`letter.sms_sender`. Web page send sends only email.

The sender is `letter.sender` from the request if you give one. Otherwise it is the sender of the
draft or its campaign, and failing that, the default sender in your account's delivery settings.
The sender must be an email address or a property placeholder, such as `$sender_email$`.

The delivery uses the draft's site. Otherwise it uses the mailing list's site, and failing that,
your default site. The site sets the address of the subscription pages that the mail links to.
See [`getCustomerSites`](/api/sites#get-all-site-settings).

For event send, ask your Liana account manager to activate the transactional email feature. For
better deliverability, send transactional mail from a separate LianaMailer account.

## How contacts are handled

Each item in `recipients` needs an `email` or `sms`. Batch send also finds a contact by `id` alone,
but not a contact that has only an SMS number. Any other keys are property values.
They fill in the property placeholders in the draft, and are saved to the contact.

```json title="Request body"
{
  "letter": { "type": "draft", "id": 123 },
  "recipients": [{ "email": "anna@example.com", "order_number": "10045" }],
  "tracking_id": "order10045"
}
```

With this request, `$order_number$` in the draft becomes `10045`, and `order_number` is saved to
the contact `anna@example.com`.

:::warning

All three endpoints replace the contact's current values with the ones you send, and create any
properties that do not exist yet. Send only values that should stay on the contact, and do
not use the [reserved property names](./restrictions#reserved-property-names).

:::

### Event send

Event send finds each contact by `email` or `sms`, and ignores list memberships. It ignores `id`
when `email` is given, and a contact given by `id` alone makes the whole request fail with
`a valid email or sms is required`. What happens to each contact:

| Contact                         | What happens                                                      |
| ------------------------------- | ----------------------------------------------------------------- |
| Not found                       | Created, and confirmed unless your account requires double opt-in |
| Has left its lists              | Gets the mail                                                     |
| Unconfirmed, with double opt-in | Does not get the mail                                             |
| Deleted                         | Does not get the mail, and stays deleted                          |
| Pseudonymized                   | Found by email address, and restored with its old ID              |
| Forgotten                       | Skipped, and not created again                                    |

The request fails if it has the same contact twice, or if none of its contacts can get the mail.
If a contact has already received this draft's event mail, the request fails and no one gets the
mail. To send the mail to a contact again, set `letter.recipients_reset` to `true`.

The contacts that event send creates are not on any mailing list, and an event delivery does not
protect them from the [daily cleanup](./contact-lifecycle#daily-cleanup), so they are deleted after
a week. To keep them, add them to a list.

### Batch and web page send

Batch send and web page send first import the contacts in `recipients` into a new hidden mailing
list, named `adhoc-delivery-draft-` followed by the ID and revision of the draft copy. The import
works like a [list import](./contact-lifecycle#creating-or-importing-a-contact-again) with the
default options.

New contacts are created and confirmed, and existing contacts are updated and get the mail, even
if they have left your other lists. Deleted, bounced and forgotten contacts are
skipped, and pseudonymized contacts are restored. Property values are saved as contact
properties, not list-specific ones, and LianaAutomation events are recorded.

The contacts become regular contacts in your account, but the hidden list does not count as a
membership. It does not appear in `membership` in the [`getRecipient`](/api/contacts#get-contact)
response, and it does not protect the contacts from the
[daily cleanup](./contact-lifecycle#daily-cleanup).

The request returns only after the import is done, so a long `recipients` array makes it slow.
For a large or recurring audience, [import the contacts](./importing) into a mailing list first,
and send to the list.

## After sending

The response has the delivery IDs in `deliveries` and the mail IDs in `mails`. Batch send and web
page send also return the ID of the draft copy in `id`. Event send returns a `tracking` ID, which
is your `tracking_id` or a generated one.

A mail is the copy of the content that a delivery sent, so a mail ID is not a draft ID. `mails`,
`mail_id` in [`getDelivery`](/api/deliveries#get-delivery) and the v1 [Mails](/api/mails)
endpoints use mail IDs. Do not pass a mail ID as `letter.id`.

To follow the progress, call [`getDeliveryStatuses`](/api/deliveries#get-delivery-statuses). It
returns the number of contacts and delivered messages of each delivery, and whether sending is
done. `getDelivery` returns the details of one delivery.

[`getScheduledDeliveries`](/api/deliveries#get-scheduled-deliveries) lists the scheduled
deliveries. To stop one before it is sent, call [`cancelDelivery`](/api/deliveries#cancel-delivery),
or [`cancelDeliveryRelated`](/api/deliveries#cancel-related-deliveries) to cancel all the
deliveries of a send to several lists or of an A/B test. There is no endpoint for changing the
time, so cancel the delivery and send it again.

[`getDeliveryReport`](/api/reports#get-delivery-report) has the result counts of one delivery, and the v3 [delivery reports](/api/delivery-reports) list who opened it or clicked its
links. The v3 [`events`](/api/events#get-events) endpoint returns opens, clicks and bounces across
deliveries, and a `mailsent` event for each contact that an event send mailed.

If a send fails with `sending blocked`,
[`hasTemporaryBlock`](/api/deliveries#check-sending-block) shows whether your account has a
temporary block.

:::warning{title="Retrying a batch send"}

Each batch or web page send request creates a new delivery, so sending the same request twice
sends the mail twice. A request that fails with an A/B timeout error may still have created a
delivery. Before you retry, check whether the delivery exists, for example with
[`getDeliveriesRange`](/api/deliveries#get-deliveries-by-range) or `getScheduledDeliveries`.

:::

## Limits

- v1 and v2 allow at most [four requests per second](./restrictions#request-rate).
- Event send can attach files in `letter.attachments`. This needs the attachments feature, and
  your account limits the size of each file and their total size.
- `letter.ab.wait_period` must be at least `3600` seconds.
