TYPO3 log all mails sent with mailer / swiftmailer

raw

AdditionalConfiguration.php

1
2
3
4
5
6
7
8
9
10
11
<?php
//log all sent mails
$GLOBALS['TYPO3_CONF_VARS']['LOG']['Mogic']['Extname']['MailLogger'] = [
    'writerConfiguration' => [
        \TYPO3\CMS\Core\Log\LogLevel::INFO => [
            \TYPO3\CMS\Core\Log\Writer\PhpErrorLogWriter::class => [],
        ],
    ],
];
?>
 
raw

Classes/MailLogger.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Mogic\Extname;
 
use \TYPO3\CMS\Core\Log\LogLevel;
use \TYPO3\CMS\Core\Utility\GeneralUtility;
 
/**
 * Log all mails sent via the TYPO3 Mailer
 *
 * @author Christian Weiske <weiske@mogic.com>
 */
class MailLogger implements \Swift_Events_SendListener
{
    /**
     * The Mailer class has just been initialized (__construct)
     *
     * @param object $mailer TYPO3 mailer instance
     *
     * @return void
     */
    public function postInitializeMailer(\TYPO3\CMS\Core\Mail\Mailer $mailer)
    {
        $mailer->registerPlugin($this);
    }
 
    /**
     * Invoked immediately before the Message is sent.
     *
     * @param Swift_Events_SendEvent $evt Event to handle
     *
     * @return void
     */
    public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
    {
    }
 
    /**
     * Invoked immediately after the Message is sent.
     *
     * @param Swift_Events_SendEvent $evt Event to handle
     *
     * @return void
     */
    public function sendPerformed(\Swift_Events_SendEvent $evt)
    {
        $level = LogLevel::INFO;
        switch ($evt->getResult()) {
        case \Swift_Events_SendEvent::RESULT_PENDING:
            $status = 'pending';
            break;
        case \Swift_Events_SendEvent::RESULT_SPOOLED:
            $status = 'spooled';
            break;
        case \Swift_Events_SendEvent::RESULT_SUCCESS:
            $status = 'success';
            break;
        case \Swift_Events_SendEvent::RESULT_TENTATIVE:
            $status = 'some failures';
            $level = LogLevel::ERROR;
            break;
        case \Swift_Events_SendEvent::RESULT_FAILED:
            $status = 'failed';
            $level = LogLevel::ERROR;
            break;
        }
 
        $failed = $evt->getFailedRecipients();
 
        $to = $evt->getMessage()->getTo();
 
        $message = 'Mail sent:'
            . ' status=' . $status
            . ' to ' . implode(',', array_keys($to));
        if (count($failed)) {
            $message .= ' failed: ' . implode(',', $failed);
        }
 
        /* @var $logger \TYPO3\CMS\Core\Log\Logger */
        $logger = GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')
            ->getLogger(__CLASS__);
        $logger->log($level, $message);
    }
}
?>
 
raw

ext_localconf.php

1
2
3
4
5
6
7
8
9
10
11
<?php
/** @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher */
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
    \TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class
);
$signalSlotDispatcher->connect(
    'TYPO3\\CMS\\Core\\Mail\\Mailer', 'postInitializeMailer',
    'Mogic\\Extname\\MailLogger', 'postInitializeMailer'
);
?>
 
Christian Weiske Christian Weiske
owner

History