EmailService   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B sendEmailMessage() 0 28 7
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use TYPO3\CMS\Core\Mail\MailMessage;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
class EmailService
18
{
19
    /**
20
     * Sends an email, if sender and recipient is an valid email address
21
     *
22
     * @param string $sender The sender
23
     * @param string $recipient The recipient
24
     * @param string $subject The subject
25
     * @param string $body E-Mail body
26
     * @param string|null $name Optional sendername
27
     * @param array $attachments Array of files (e.g. ['/absolute/path/doc.pdf'])
28
     * @param string|null $replyTo The reply-to mail
29
     *
30
     * @return bool true/false if message is sent
31
     */
32
    public function sendEmailMessage(
33
        string $sender,
34
        string $recipient,
35
        string $subject,
36
        string $body,
37
        ?string $name = null,
38
        array $attachments = [],
39
        ?string $replyTo = null
40
    ): bool {
41
        if (!GeneralUtility::validEmail($sender) || !GeneralUtility::validEmail($recipient)) {
42
            return false;
43
        }
44
45
        $email = GeneralUtility::makeInstance(MailMessage::class);
46
        $email->setFrom($sender, $name);
47
        $email->setTo($recipient);
48
        $email->setSubject($subject);
49
        $email->html($body);
50
        if ($replyTo !== null && $replyTo !== '') {
51
            $email->setReplyTo($replyTo);
52
        }
53
        foreach ($attachments as $attachment) {
54
            if (file_exists($attachment)) {
55
                $email->attachFromPath($attachment);
56
            }
57
        }
58
59
        return $email->send();
60
    }
61
}
62