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

<a id="connect"></a>

# Connect

This script is executed before the SMTP banner is sent.

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

## Variables

These are the pre-defined variables available.

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

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

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

### Arguments

| Array item      | Type   | Example                                    | Description                                                                      |
|-----------------|--------|--------------------------------------------|----------------------------------------------------------------------------------|
| 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)                         |
| remoteptrresult | array  | `["reverse" => [...], "forward" => [...]]` | DNS result for 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                                                                 |
| proxyip         | string | `"192.168.1.1"`                            | IP address of the proxy server (not always available)                            |
| proxytlv        | string | `fffffffffffffffffffffff...`               | Any additional PROXY protocol TLV received (not always available, in hex format) |

<a id="v-u1"></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 |

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

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

### Transaction

| Array item   | Type   | Example                     | Description           |
|--------------|--------|-----------------------------|-----------------------|
| id           | string | `"18c190a3-93f-47d7-bd..."` | ID of the transaction |

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

## Functions

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

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

### Accept([options])

Allow the connection to be established.
Optionally change the `remoteip` and PTR of the accepted client connection, which is written back to the `$connection` variable.

* **Parameters:**
  **options** (*array*) – an options array
* **Returns:**
  doesn’t return, script is terminated

The following options are available in the options array.

> * **remoteip** (string) Change the IP address of the accepted client connection.
> * **remoteptr** (string) Set the reverse DNS pointer (PTR) for the IP address.
> * **serverhost** (string) Set the SMTP servers hostname.
> * **reason** (string or array) The greeting banner response.
> * **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 `220` and *enhanced* is not used.
> * **priority** (number) Change the connection priority (0 normal, 1 high).
> * **maxmessagesize** (number) Change max message size for this connection (in bytes).
> * **tls** (array) The associative array must contain `x509` (*X509Resource*), `privatekey` (`*PrivateKeyResource`) and optionally a `chain` (*array*) of *X509Resources*.

> **Note**
> 
> The `remoteptr` option can be useful for eg. decoding IPv4 addresses embedded in an IPv6 address ([RFC6052](https://tools.ietf.org/html/rfc6052)).
> 
> ```hsl
> $x = unpack("N*", inet_pton($arguments["remoteip"]));
> if (length($x) == 4 and $x[0:3] == [6619035, 0, 0]) // 64:ff9b::[IPv4]
>         $ip = inet_ntop(pack("N", $x[3]));
> ```

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

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

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

Close the connection with a permanent (521) error.

* **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.

> * **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 `521` and *enhanced* is not used.
> * **tls** (array) The associative array must contain `x509` (*X509Resource*), `privatekey` (`*PrivateKeyResource`) and optionally a `chain` (*array*) of *X509Resources*. For `Reject()` calls this is only used for implicit TLS.

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

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

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

Close the connection with a temporary (421) error.

* **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.

> * **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 not used
> * **tls** (array) The associative array must contain `x509` (*X509Resource*), `privatekey` (`*PrivateKeyResource`) and optionally a `chain` (*array*) of *X509Resources*. For `Defer()` calls this is only used for implicit TLS.

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

## On script error

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

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

## On implicit termination

If not explicitly terminated then [`Accept()`](#connect.Accept) is called.
