<a id="module-eod"></a>

<a id="end-of-data"></a>

# End-of-DATA

The end-of-DATA script is executed once, when the message is fully received (but not yet accepted).
To relay the message for all recipients, call [`EODMailMessage.queue()`](#eod.EODMailMessage.queue) for each `$transaction["recipients"]` and then [`Accept()`](#eod.Accept).

<a id="variables"></a>

## Variables

These are the pre-defined variables available.

| Variable                                                    | Type   | Read-only   | Description                                  |
|-------------------------------------------------------------|--------|-------------|----------------------------------------------|
| [$arguments](#v-a6)   | array  | yes         | Context/hook arguments                       |
| [$connection](#v-c6)  | array  | yes         | Connection/session bound                     |
| [$transaction](#v-t6) | array  | yes         | Transaction bound                            |
| $context                                                    | any    | no          | Connection bound user-defined (default none) |

<a id="v-a6"></a>

<a id="arguments"></a>

### Arguments

| Array item                                          | Type                                                            | Example   | Description                                             |
|-----------------------------------------------------|-----------------------------------------------------------------|-----------|---------------------------------------------------------|
| mail                                                | [`EODMailMessage`](#_CPPv414EODMailMessage) |           | An instance of the mail message                         |
| metadata                                            | any                                                             |           | User-defined data (only available through the HTTP API) |
| [http](#v-h) | array                                                           |           | HTTP data (only available through the HTTP API)         |

<a id="v-c6"></a>

<a id="connection"></a>

### Connection

| Array item                                          | Type   | Example                     | Description                                              |
|-----------------------------------------------------|--------|-----------------------------|----------------------------------------------------------|
| id                                                  | string | `"18c190a3-93f-47d7-bd..."` | ID of the connection                                     |
| priority                                            | number | `1`                         | The connection priority                                  |
| remoteip                                            | string | `"192.168.1.11"`            | IP address of the connected client                       |
| remoteport                                          | number | `41666`                     | TCP port of connected client                             |
| remoteptr                                           | string | `"mail.example.org"`        | Reverse DNS (FCrDNS) for remoteip (not always available) |
| remoteuid                                           | number | `106`                       | UNIX socket remote user id (not always available)        |
| localip                                             | string | `"10.0.0.1"`                | IP address of the server                                 |
| localport                                           | number | `25`                        | TCP port of the server                                   |
| serverid                                            | string | `"inbound"`                 | ID of the server                                         |
| [helo](#he6) | array  |                             | HELO information (not always available)                  |
| [tls](#tls6)  | array  |                             | TLS information (if TLS was started)                     |
| [auth](#au6) | array  |                             | AUTH information (not always available)                  |

<a id="he6"></a>

<a id="helo"></a>

#### HELO

| Array item   | Type   | Example              | Description          |
|--------------|--------|----------------------|----------------------|
| verb         | string | `"EHLO"`             | HELO or EHLO command |
| host         | string | `"mail.example.com"` | HELO hostname        |

<a id="tls6"></a>

<a id="tls"></a>

#### TLS

| Array item                                              | Type   | Example                     | Description                                      |
|---------------------------------------------------------|--------|-----------------------------|--------------------------------------------------|
| protocol                                                | string | `"TLSv1.3"`                 | The protocol                                     |
| cipher                                                  | string | `"ECDHE-RSA-AES256-SHA384"` | The cipher                                       |
| keysize                                                 | number | `256`                       | The keysize                                      |
| sni                                                     | string | `"example.com"`             | The SNI hostname (not always available)          |
| [peercert](#p6) | array  |                             | The peer certificate (if provided by the client) |

<a id="p6"></a>

<a id="peercert"></a>

##### Peercert

| Array item   | Type         | Example   | Description                                                                                          |
|--------------|--------------|-----------|------------------------------------------------------------------------------------------------------|
| x509         | X509Resource |           | An X509Resource to be used with the [`X509`](functions.html.md#X509) class |
| chain        | array        |           | An array of X509Resource (the chain)                                                                 |
| error        | number       | `18`      | The peer certificate validation error (see OpenSSLs SSL_get_verify_result(3))                        |

<a id="au6"></a>

<a id="auth"></a>

#### AUTH

| Array item   | Type   | Example      | Description                          |
|--------------|--------|--------------|--------------------------------------|
| mechanism    | string | `"PLAIN"`    | SASL mechanism (always in uppercase) |
| username     | string | `"mailuser"` | SASL username (not always available) |

<a id="v-t6"></a>

<a id="transaction"></a>

### Transaction

| Array item                                                   | Type   | Example                                           | Description                                                                                                     |
|--------------------------------------------------------------|--------|---------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|
| id                                                           | string | `"18c190a3-93f-47d7-bd..."`                       | ID of the transaction                                                                                           |
| ts                                                           | number | `1575558785.1234`                                 | Unix time of transaction                                                                                        |
| sender                                                       | string | `"test@example.org"`                              | Sender address (envelope), lowercase                                                                            |
| [senderaddress](#a6) | array  | `["localpart" => "test"...]`                      | Sender address (envelope)                                                                                       |
| senderparams                                                 | array  | `["SIZE" => "2048", ... ]`                        | Sender parameters to the envelope address (keys will always be in uppercase)                                    |
| recipients                                                   | array  | `[["recipient" => "test@example.com", ...], ...]` | List of all accepted [recipients](#v-t-r6) (envelope), in order of scanning |

<a id="v-t-r6"></a>

<a id="recipient"></a>

#### Recipient

| Array item                                             | Type   | Example                      | Description                                                                     |
|--------------------------------------------------------|--------|------------------------------|---------------------------------------------------------------------------------|
| recipient                                              | string | `"test@example.com"`         | Recipient address (envelope), lowercase                                         |
| [address](#a6) | array  | `["localpart" => "test"...]` | Recipient address (envelope)                                                    |
| params                                                 | array  | `["NOTIFY" => "NEVER", .. ]` | Recipient parameters to the envelope address (keys will always be in uppercase) |
| transportid                                            | string | `"inbound"`                  | Transport ID for recipient                                                      |

<a id="a6"></a>

<a id="address"></a>

#### Address

| Array item   | Type   | Example         | Description            |
|--------------|--------|-----------------|------------------------|
| localpart    | string | `"test"`        | Local part of address  |
| domain       | string | `"example.org"` | Domain part of address |

<a id="v-h"></a>

<a id="http"></a>

#### http

| Array item   | Type   | Example                      | Description                                |
|--------------|--------|------------------------------|--------------------------------------------|
| headers      | array  | `[["user-agent", "..."],...` | HTTP request headers (names are lowercase) |

<a id="functions"></a>

## Functions

* **Actions** [`Accept()`](#eod.Accept) [`Reject()`](#eod.Reject) [`Defer()`](#eod.Defer)
* **DATA, MIME and attachments** [`EODMailMessage`](#_CPPv414EODMailMessage) [`MIMEPart`](functions.html.md#_CPPv48MIMEPart)

<a id="actions"></a>

### Actions

<a id="Accept"></a>

<a id="eod.Accept"></a>

#### Accept([options])

Accept the *DATA* command (mail data).

* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **reason** (string or array) The reason to report. The default is a system generated message.
> * **reply_codes** (array) The associative array may contain the properties *code* (number) and *enhanced* (array of three numbers). To remove the *enhanced* code set it to `[0,0,0]`. The default *code* is `250` and *enhanced* is `[2,0,0]`.
> * **metadata** (any) User-defined data (only available through the HTTP API)
> * **priority** (number) Change the connection priority (0 normal, 1 high).

<a id="Reject"></a>

<a id="eod.Reject"></a>

#### Reject([reason[, options]])

Reject (550) a message.

* **Parameters:**
  * **reason** (*string* *or* *array*) – reject message with reason
  * **options** (*array*) – an options array
* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **disconnect** (boolean) Disconnect the client. The default is `false`.
> * **reply_codes** (array) The associative array may contain the properties *code* (number) and *enhanced* (array of three numbers). To remove the *enhanced* code set it to `[0,0,0]`. The default *code* is `550` and *enhanced* is `[5,7,1]`.
> * **metadata** (any) User-defined data (only available through the HTTP API)
> * **priority** (number) Change the connection priority (0 normal, 1 high).

<a id="Defer"></a>

<a id="eod.Defer"></a>

#### Defer([reason[, options]])

Defer (421) a message.

* **Parameters:**
  * **reason** (*string* *or* *array*) – defer message with reason
  * **options** (*array*) – an options array
* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **disconnect** (boolean) Disconnect the client. The default is `false`.
> * **reply_codes** (array) The associative array may contain the properties *code* (number) and *enhanced* (array of three numbers). To remove the *enhanced* code set it to `[0,0,0]`. The default *code* is `421` and *enhanced* is `[4,3,2]`.
> * **metadata** (any) User-defined data (only available through the HTTP API)
> * **priority** (number) Change the connection priority (0 normal, 1 high).

<a id="data-mime-and-attachments"></a>

### DATA, MIME and attachments

<a id="mailmessage"></a>

<a id="_CPPv414EODMailMessage"></a>

<a id="_CPPv314EODMailMessage"></a>

<a id="_CPPv214EODMailMessage"></a>

<a id="EODMailMessage"></a>

#### class EODMailMessage : [MailMessage](functions.html.md#_CPPv411MailMessage)

In the end-of-DATA context the [`MailMessage`](functions.html.md#_CPPv411MailMessage) class has been extended with one additional function; [`EODMailMessage.queue()`](#eod.EODMailMessage.queue).

<a id="EODMailMessage.queue"></a>

<a id="eod.EODMailMessage.queue"></a>

##### queue(sender, recipient, transportid[, options])

Queue the message.

* **Parameters:**
  * **sender** (*string* *or* *array*) – the sender email address, either as a string or an associative array with a `localpart` and `domain`.  The domain support internationalized domain names (U-labels).
  * **recipient** (*string* *or* *array*) – the recipient email address, either as a string or an associative array with a `localpart` and `domain`. The domain support internationalized domain names (U-labels).
  * **transportid** (*string*) – the transport profile ID
  * **options** (*array*) – an options array
* **Returns:**
  an id object (with `transaction` and `queue`)
* **Return type:**
  array

The following options are available in the options array.

> * **metadata** (array) Add metadata to the queued message, as a key-value pair array of strings.
> * **hold** (boolean) Put the message in the hold (inactive) queue. The default is `false`.
> * **tenantid** (string) Assign a tenantid the message.
> * **jobid** (string) Assign a jobid the message.
> * **priority** (number) The queue priority (0 - normal, 1 - high). The default is `0`.
> * **fsync** (bool) Override default fsync behaviour for this specific message (for multiple recipients, true has precedence).
> * **quotas** (array) An array of quotas to be associated with the message, for use with [`queue_quota()`](functions.html.md#queue_quota).
> * **delay** (number) Delay the first delivery attempt, in seconds. The default is `0`.
> * **dsn** (array) Associative array of `envid` (string), `ret` (string), `notify` (string) and `orcpt` (string).

<a id="on-script-error"></a>

## On script error

On script error [`Defer()`](#eod.Defer) is called.

<a id="on-implicit-termination"></a>

## On implicit termination

If not explicitly terminated then [`Defer()`](#eod.Defer) is called and no recipients will be queued.
