Completed
Push — typo3-10-compatibility ( d960ff...46cd8f )
by Torben
86:53 queued 41:50
created

EmailService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

1 Method

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