Skip to content

[ENHANCEMENT] Remove bundled PHPMailer and migrate fully to GLPI's native notification system #51

@giovanny07

Description

@giovanny07

Plugin version: 3.2.6
GLPI version: 11.x
Priority: High
Files: src/NotificationMail.php, src/NotificationTargetHoliday.php, vendor/phpmailer/

Summary

The plugin maintains a custom NotificationMail class that extends a bundled PHPMailer instance. GLPI 11 provides a complete, transport-agnostic notification system. Using it eliminates the bundled dependency, reduces plugin size by ~500KB, and ensures notifications behave consistently with the rest of GLPI (queue, templates, admin configuration, etc.).

The infrastructure is already partially in place — src/NotificationTargetHoliday.php exists. The goal is to complete it and remove the custom mail layer.

Step 1 — Complete NotificationTargetHoliday

// src/NotificationTargetHoliday.php
namespace GlpiPlugin\Activity;

use NotificationTarget;
use Session;
use User;

class NotificationTargetHoliday extends NotificationTarget
{
/**
* Declare the events this target handles.
*/
public function getEvents(): array
{
return [
'newvalidation' => __('New holiday validation request', 'activity'),
'answervalidation' => __('Holiday validation answer', 'activity'),
];
}

/**
 * Define who receives notifications for each event.
 */
public function addAdditionalTargets(string $event = ''): void
{
    switch ($event) {
        case 'newvalidation':
            // Notify validators
            $this->addTarget(
                Notification::USER,
                __('Validator', 'activity')
            );
            break;

        case 'answervalidation':
            // Notify the applicant
            $this->addTarget(
                Notification::ITEM_TECH_IN_CHARGE,
                __('Applicant', 'activity')
            );
            break;
    }
}

/**
 * Populate template variables for the notification.
 */
public function addDataForTemplate(string $event, array $options = []): void
{
    /** @var Holiday $holiday */
    $holiday = $this->obj;

    $this->data['##activity.url##']              = $this->formatURL(
        $options['additionalTargets'] ?? '',
        Holiday::getFormURLWithID($holiday->getID())
    );
    $this->data['##holiday.status##']            = Holiday::getStatusName($holiday->fields['status']);
    $this->data['##holiday.applicant.name##']    = getUserName($holiday->fields['users_id']);
    $this->data['##holiday.name##']              = $holiday->fields['name'];
    $this->data['##holiday.begin.date##']        = Html::convDate($holiday->fields['begin']);
    $this->data['##holiday.end.date##']          = Html::convDate($holiday->fields['end']);
    $this->data['##holiday.nbdays##']            = $holiday->fields['actiontime'];
    $this->data['##holiday.date.submission##']   = Html::convDateTime($holiday->fields['date']);
    $this->data['##holiday.holidaytype##']       = Dropdown::getDropdownName(
        'glpi_plugin_activity_holidaytypes',
        $holiday->fields['plugin_activity_holidaytypes_id']
    );
}

public function getTags(): void
{
    $tags = [
        'activity.url'            => __('URL', 'activity'),
        'holiday.status'          => __('Status', 'activity'),
        'holiday.applicant.name'  => __('Applicant', 'activity'),
        'holiday.name'            => __('Name'),
        'holiday.begin.date'      => __('Begin date', 'activity'),
        'holiday.end.date'        => __('End date', 'activity'),
        'holiday.nbdays'          => __('Number of days', 'activity'),
        'holiday.date.submission' => __('Submission date', 'activity'),
        'holiday.holidaytype'     => __('Holiday type', 'activity'),
    ];

    foreach ($tags as $tag => $label) {
        $this->addTagToList(['tag' => $tag, 'label' => $label, 'value' => true]);
    }
}

}

Step 2 — Raise events from Holiday and HolidayValidation

// src/Holiday.php — after add()
public function post_addItem(): void
{
    NotificationEvent::raiseEvent('newvalidation', $this);
}

// src/HolidayValidation.php — after status change
public function post_updateItem(array $history = []): void
{
$holiday = new Holiday();
$holiday->getFromDB($this->fields['plugin_activity_holidays_id']);
NotificationEvent::raiseEvent('answervalidation', $holiday);
}

Step 3 — Update composer.json

{
    "require": {
        "fpdf/fpdf": "^1.86"
    }
}

Then run:

composer update --no-dev

Step 4 — Delete custom mail files

src/NotificationMail.php    ← delete
vendor/phpmailer/           ← deleted by composer update

Benefits

Before After
Custom SMTP config in plugin Uses GLPI's configured mail transport
Notifications bypass GLPI queue Queued, logged, and retried by GLPI
No admin visibility Visible in GLPI notification log
Bundled PHPMailer 6.12.0 GLPI's PHPMailer (always up to date)
~500KB vendor overhead 0KB added

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions