-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailer.php
73 lines (67 loc) · 2.37 KB
/
Mailer.php
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
<?php
namespace MatchBot\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
/**
* Originally created as a copy of the similar class BigGive\Identity\Client in Identity repo & adapted to fit in
* Matchbot.
*/
class Mailer extends Common
{
/**
* @psalm-param array{templateKey: string, recipientEmailAddress: string, params: array, ...} $requestBody
*/
public function sendEmail(array $requestBody): void
{
try {
$baseUri = $this->getSetting('mailer', 'baseUri');
$uri = $baseUri . '/v1/send';
$response = $this->getHttpClient()->post(
$uri,
[
'json' => $requestBody,
'headers' => [
'x-send-verify-hash' => $this->hash(json_encode($requestBody)),
],
]
);
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),
$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',
));
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->getSetting('mailer', 'sendSecret'));
}
}