Completed
Push — master ( 70dfe1...a97a1c )
by Torben
04:18
created

EmailService::sendEmailMessage()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

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