-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailer.php
More file actions
99 lines (89 loc) · 3.2 KB
/
Mailer.php
File metadata and controls
99 lines (89 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace MatchBot\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use MatchBot\Application\Email\EmailMessage;
use MatchBot\Application\Environment;
/**
* Originally created as a copy of the similar class BigGive\Identity\Client in Identity repo & adapted to fit in
* Matchbot.
*/
class Mailer extends Common
{
/**
* @deprecated for public use - use {@see self::send()} instead.
*
* @param array{templateKey: string, recipientEmailAddress: string, params: array<string, mixed>, ...} $requestBody
*/
public function sendEmail(array $requestBody): void
{
try {
$uri = $this->baseUri() . '/v1/send';
$response = $this->getHttpClient()->post(
$uri,
[
'json' => $requestBody,
'headers' => [
'x-send-verify-hash' => $this->hash(json_encode($requestBody, \JSON_THROW_ON_ERROR)),
'User-Agent' => 'matchbot',
],
]
);
if ($response->getStatusCode() === 200) {
return;
} else {
$this->logger->warning(sprintf(
'%s email callout didn\'t return 200. It returned code: %s. Request body: %s. Response body: %s.',
$requestBody['templateKey'],
$response->getStatusCode(),
json_encode($requestBody, \JSON_THROW_ON_ERROR),
$response->getBody()->getContents(),
));
return;
}
} catch (RequestException $ex) {
$response = $ex->getResponse();
$this->logger->error(sprintf(
'%s email exception %s with error code %s: %s. Body: %s',
$requestBody['templateKey'],
get_class($ex),
$ex->getCode(),
$ex->getMessage(),
$response ? $response->getBody()->getContents() : 'N/A',
));
if (!Environment::current()->isProduction()) {
$this->logger->error("Message request data: " . json_encode($requestBody, \JSON_THROW_ON_ERROR));
}
return;
} catch (GuzzleException $ex) {
$this->logger->error(sprintf(
'%s email exception %s with error code %s: %s. Body: %s',
$requestBody['templateKey'],
get_class($ex),
$ex->getCode(),
$ex->getMessage(),
'N/A',
));
return;
}
}
private function hash(string $body): string
{
return hash_hmac('sha256', trim($body), $this->getMailerSetting('sendSecret'));
}
private function baseUri(): string
{
return $this->getMailerSetting('baseUri');
}
public function send(EmailMessage $command): void
{
/** @psalm-suppress DeprecatedMethod */
$this->sendEmail(
[
'templateKey' => $command->templateKey,
'recipientEmailAddress' => $command->emailAddress->email,
'params' => $command->params,
]
);
}
}