EXT: sf_event_mgt erweitern: Fluidemails

Damit die E-Mails an den Benutzer und an den Administrator der Erweiterung sf_event_mgt in ansprechenden Fluidemail-Templates verschickt werden, erweitere ich die Klasse EmalService per XClass.

Die Klasse EmailService, die sich unter ‘sf_event_mgt/Classes/Service/EmailService.php’ findet wird per XClass überschrieben.

In diesem Beispiel ist der Vendor ‘Machwert’ und die Extension ‘theme’.

1. Bei Composer Installation in der composer.json darauf achten, dass die Extension Klassen geladen werden.

EXT:theme/composer.json

"autoload": {
        "psr-4": {
            "Machwert\\Theme\\": "Classes"
        }
    },

2. In der ext_localconf.php die zu ersetzende Klasse registrieren und die Fluidemail-Template-Pfade definieren:

EXT:theme/ext_localconf.php

use Machwert\Theme\Xclass\NewEmailService as NewEmailServiceXclass;
use DERHANSEN\SfEventMgt\Service\EmailService;
$GLOBALS['TYPO3_CONF_VARS']['MAIL']['layoutRootPaths'][100] = 'EXT:theme/Resources/Private/Layouts/';
$GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'][100] = 'EXT:theme/Resources/Private/Templates/Email/';
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][EmailService::class] = [
  'className' => NewEmailServiceXclass::class
];

3. In der eigenen Extension unter ‘Classes/Xclass’ die Datei ‘NewEmailService.php’ anlegen:

EXT:theme/Classes/Xclass/NewEmailService.php

<?php
declare(strict_types=1);
/*
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 */
namespace Machwert\Theme\Xclass;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Mail\FluidEmail;
use TYPO3\CMS\Core\Mail\Mailer;
class NewEmailService extends \DERHANSEN\SfEventMgt\Service\EmailService
{
    /**
     * Sends an email, if sender and recipient is an valid email address
     *
     * @param string $sender The sender
     * @param string $recipient The recipient
     * @param string $subject The subject
     * @param string $body E-Mail body
     * @param string|null $name Optional sendername
     * @param array $attachments Array of files (e.g. ['/absolute/path/doc.pdf'])
     * @param string|null $replyTo The reply-to mail
     *
     * @return bool true/false if message is sent
     */
    public function sendEmailMessage(
        string $sender,
        string $recipient,
        string $subject,
        string $body,
        ?string $name = null,
        array $attachments = [],
        ?string $replyTo = null
    ): bool {
        if (!GeneralUtility::validEmail($sender) || !GeneralUtility::validEmail($recipient)) {
            return false;
        }
        // NEW: FLUIDEMAIL
        $email = GeneralUtility::makeInstance(FluidEmail::class)
            ->from($sender)
            ->to($recipient)
            ->subject($subject)
            ->setTemplate('Email')
            ->assign('headline', $subject)
            ->assign('content', $body);
        if ($replyTo !== null && $replyTo !== '') {
            $email->replyTo($replyTo);
        }
        foreach ($attachments as $attachment) {
            if (file_exists($attachment)) {
                $email->attachFromPath($attachment);
            }
        }
        if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface) {
            $email->setRequest($GLOBALS['TYPO3_REQUEST']);
        }
        GeneralUtility::makeInstance(Mailer::class)->send($email);
        return true;
    }
}

4. Extension per Composer deinstallieren und danach wieder installieren, damit die autoload Klassen registriert werden. Folgende Shell-Befehle ausführen:

composer remove Machwert/theme
composer req Machwert/theme

5. In der Datei EXT:theme/Resources/Private/Templates/Email/Email.html und Email.txt anlegen:

EXT:theme/Resources/Private/Templates/Email/Email.html

<f:layout name="SystemEmail" />
<f:section name="Title">{headline}</f:section>
<f:section name="Main">
    <f:if condition="{introduction}">
        

{introduction}

</f:if> <f:format.raw>{content}</f:format.raw> </f:section>

EXT:theme/Resources/Private/Templates/Email/Email.txt

<f:layout name="SystemEmail" />
<f:section name="Title">{headline}</f:section>
<f:section name="Main">
<f:if condition="{introduction}">{introduction}</f:if>
{content}
</f:section>

Erstellt am: (aktualisiert am: )