Passed
Push — master ( 1c618d...c1c6b0 )
by Yannick
10:36 queued 02:52
created

MailHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Exception;
10
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
11
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
12
use Symfony\Component\Mailer\MailerInterface;
13
use Symfony\Component\Mime\Address;
14
use Symfony\Component\Mime\BodyRendererInterface;
15
use Symfony\Component\Mime\Part\DataPart;
16
17
final class MailHelper
18
{
19
    public function __construct(
20
        private readonly MailerInterface $mailer,
21
        private readonly BodyRendererInterface $bodyRenderer,
22
    ) {}
23
24
    public function send(
25
        string $recipientName,
26
        string $recipientEmail,
27
        string $subject,
28
        string $body,
29
        ?string $senderName = null,
30
        ?string $senderEmail = null,
31
        array $extra_headers = [],
32
        array $data_file = [],
33
        bool $embeddedImage = false,
34
        array $additionalParameters = [],
35
        ?string $sendErrorTo = null,
36
    ): bool {
37
        if (!api_valid_email($recipientEmail)) {
38
            return false;
39
        }
40
41
        $templatedEmail = new TemplatedEmail();
42
43
        api_set_noreply_and_from_address_to_mailer(
44
            $templatedEmail,
45
            ['name' => $senderName, 'email' => $senderEmail],
46
            !empty($extra_headers['reply_to']) ? $extra_headers['reply_to'] : []
47
        );
48
49
        if ($sendErrorTo) {
50
            $templatedEmail
51
                ->getHeaders()
52
                ->addIdHeader('Errors-To', $sendErrorTo)
53
            ;
54
        }
55
56
        // Reply to first
57
        $replyToName = '';
58
        $replyToEmail = '';
59
        if (isset($extra_headers['reply_to'])) {
60
            $replyToEmail = $extra_headers['reply_to']['mail'];
61
            $replyToName = $extra_headers['reply_to']['name'];
62
        }
63
64
        try {
65
            $templatedEmail->subject($subject);
66
67
            $list = api_get_setting('announcement.send_all_emails_to', true);
68
69
            if (!empty($list) && isset($list['emails'])) {
70
                foreach ($list['emails'] as $email) {
71
                    $templatedEmail->cc($email);
72
                }
73
            }
74
75
            // Attachment
76
            if (!empty($data_file)) {
77
                foreach ($data_file as $file_attach) {
78
                    if (!empty($file_attach['path']) && !empty($file_attach['filename'])) {
79
                        $templatedEmail->attachFromPath($file_attach['path'], $file_attach['filename']);
80
                    }
81
82
                    if (!empty($file_attach['stream']) && !empty($file_attach['filename'])) {
83
                        $templatedEmail->addPart(new DataPart($file_attach['stream'], $file_attach['filename']));
84
                    }
85
                }
86
            }
87
88
            $noReply = api_get_setting('noreply_email_address');
89
            $automaticEmailText = '';
90
91
            if (!empty($noReply)) {
92
                $automaticEmailText = '<br />'.get_lang('This is an automatic email message. Please do not reply to it.');
93
            }
94
95
            $params = [
96
                'mail_header_style' => api_get_setting('mail.mail_header_style'),
97
                'mail_content_style' => api_get_setting('mail.mail_content_style'),
98
                'link' => $additionalParameters['link'] ?? '',
99
                'automatic_email_text' => $automaticEmailText,
100
                'content' => $body,
101
                'theme' => api_get_visual_theme(),
102
            ];
103
104
            if (!empty($recipientEmail)) {
105
                $templatedEmail->to(new Address($recipientEmail, $recipientName));
106
            }
107
108
            if (!empty($replyToEmail)) {
109
                $templatedEmail->replyTo(new Address($replyToEmail, $replyToName));
110
            }
111
112
            $templatedEmail
113
                ->htmlTemplate('@ChamiloCore/Mailer/Default/default.html.twig')
114
                ->context($params)
115
            ;
116
117
            $this->bodyRenderer->render($templatedEmail);
118
            $this->mailer->send($templatedEmail);
119
120
            return true;
121
        } catch (Exception|TransportExceptionInterface $e) {
122
            error_log($e->getMessage());
123
124
            return false;
125
        }
126
    }
127
}
128