1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2020-2025 Iain Cambridge |
7
|
|
|
* |
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by |
10
|
|
|
* the Free Software Foundation, either version 2.1 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Parthenon\Notification\Sender; |
23
|
|
|
|
24
|
|
|
use Mailgun\Mailgun; |
25
|
|
|
use Parthenon\Common\LoggerAwareTrait; |
26
|
|
|
use Parthenon\Notification\Configuration; |
27
|
|
|
use Parthenon\Notification\EmailInterface; |
28
|
|
|
use Parthenon\Notification\EmailSenderInterface; |
29
|
|
|
use Parthenon\Notification\Exception\UnableToSendMessageException; |
30
|
|
|
|
31
|
|
|
final class MailgunEmailSender implements EmailSenderInterface |
32
|
|
|
{ |
33
|
|
|
use LoggerAwareTrait; |
34
|
|
|
|
35
|
|
|
public function __construct(private Mailgun $mailgun, private string $domain, private Configuration $configuration) |
36
|
|
|
{ |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function send(EmailInterface $message) |
40
|
|
|
{ |
41
|
|
|
$messageArray = [ |
42
|
|
|
'to' => $message->getToAddress(), |
43
|
|
|
'from' => $message->getFromAddress(), |
44
|
|
|
'subject' => $message->getSubject(), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
if ($message->isTemplate()) { |
48
|
|
|
$messageArray['template'] = $message->getTemplateName(); |
49
|
|
|
$messageArray['h:X-Mailgun-Variables'] = json_encode($message->getTemplateVariables()); |
50
|
|
|
} else { |
51
|
|
|
$messageArray['html'] = $message->getContent(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$attachments = $message->getAttachments(); |
55
|
|
|
if (!empty($attachments)) { |
56
|
|
|
$messageArray['attachment'] = []; |
57
|
|
|
foreach ($attachments as $attachment) { |
58
|
|
|
$messageArray['attachment'][] = ['fileContent' => $attachment->getContent(), 'filename' => $attachment->getName()]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$response = $this->mailgun->messages()->send($this->domain, $messageArray); |
64
|
|
|
$this->getLogger()->info('Sent email via mail gun'); |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
$this->getLogger()->warning('Unable to send email via mailgun', ['exception_message' => $e->getMessage()]); |
67
|
|
|
throw new UnableToSendMessageException($e->getMessage(), $e->getCode(), $e); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (200 !== $response->getStatusCode()) { |
|
|
|
|
71
|
|
|
$this->getLogger()->warning('Unable to send email via mailgun', ['status_code' => $response->getStatusCode()]); |
72
|
|
|
throw new UnableToSendMessageException(sprintf('Mailgun returned %d status code', $response->getStatusCode())); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.