# MailHostsEvent events

> 3 FeatherPanel plugin events on MailHostsEvent (3 emitted by the core): the event names, the arguments each listener gets, where they fire and a PHP listener for each.

Source: https://featherpanel.com/docs/plugins/events/mail-hosts/

Part of the [plugin events reference](https://featherpanel.com/docs/plugins/events/). Listeners get the payload values as positional arguments in the order shown, cannot cancel the action, and must catch their own exceptions.

## Setup

Import `MailHostsEvent` in your entry class and register listeners in `processEvents()`:

```php
<?php

namespace App\Addons\myplugin;

use App\App;
use App\Plugins\AppPlugin;
use App\Plugins\PluginEvents;
use App\Plugins\Events\Events\MailHostsEvent;

class MyPlugin implements AppPlugin
{
    public static function processEvents(PluginEvents $event): void
    {
        $event->on(MailHostsEvent::onMailHostDeleted(), function ($hostId, $host): void {
            App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:mail_hosts:host:deleted ' . json_encode(['host_id' => $hostId, 'host' => $host]));
        });
    }

    public static function pluginInstall(): void
    {
    }

    public static function pluginUninstall(): void
    {
    }
}
```

## `MailHostsEvent::onMailHostCreated()`

- Name: `featherpanel:admin:mail_hosts:host:created`
- emitted at 1 site
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/MailHostsEvent.php#L27)

Docblock: array host data.

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `host` | `$host` | `array`from docblock |

When it fires (1 site)

-   [`MailHostsController::create`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/Admin/MailHostsController.php#L78) via `emitPluginEvent()`[`PUT /api/admin/mail-hosts`](https://featherpanel.com/docs/api/reference/admin/mail-hosts/#put-admin-mail-hosts)

```php
$event->on(MailHostsEvent::onMailHostCreated(), function ($host): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:mail_hosts:host:created ' . json_encode(['host' => $host]));
});
```

## `MailHostsEvent::onMailHostDeleted()`

- Name: `featherpanel:admin:mail_hosts:host:deleted`
- emitted at 1 site
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/MailHostsEvent.php#L43)

Docblock: int host id, array host data.

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `host_id` | `$hostId` | `int` |
| 2 | `host` | `$host` | `array`from docblock |

When it fires (1 site)

-   [`MailHostsController::delete`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/Admin/MailHostsController.php#L155) via `emitPluginEvent()`[`DELETE /api/admin/mail-hosts/{id}`](https://featherpanel.com/docs/api/reference/admin/mail-hosts/#delete-admin-mail-hosts-id)

```php
$event->on(MailHostsEvent::onMailHostDeleted(), function ($hostId, $host): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:mail_hosts:host:deleted ' . json_encode(['host_id' => $hostId, 'host' => $host]));
});
```

## `MailHostsEvent::onMailHostUpdated()`

- Name: `featherpanel:admin:mail_hosts:host:updated`
- emitted at 1 site
- Cancel or modify: no
- [definition](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Plugins/Events/Events/MailHostsEvent.php#L35)

Docblock: int host id, array host data.

| # | Payload key | Listener parameter | Type |
| --- | --- | --- | --- |
| 1 | `host_id` | `$hostId` | `int` |
| 2 | `host` | `$host` | `array`from docblock |

When it fires (1 site)

-   [`MailHostsController::update`](https://github.com/MythicalLTD/FeatherPanel/blob/15b94e9fc9df233123706b47c42728b81e48cb3d/backend/app/Controllers/Admin/MailHostsController.php#L110) via `emitPluginEvent()`[`PATCH /api/admin/mail-hosts/{id}`](https://featherpanel.com/docs/api/reference/admin/mail-hosts/#patch-admin-mail-hosts-id)

```php
$event->on(MailHostsEvent::onMailHostUpdated(), function ($hostId, $host): void {
    App::getInstance(true)->getLogger()->info('[myplugin] featherpanel:admin:mail_hosts:host:updated ' . json_encode(['host_id' => $hostId, 'host' => $host]));
});
```
