1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tylercd100\Monolog\Handler; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
use Monolog\Handler\SocketHandler; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Mailgun - Monolog Handler |
11
|
|
|
*/ |
12
|
|
|
class MailgunHandler extends SocketHandler |
13
|
|
|
{ |
14
|
|
|
private $host; |
15
|
|
|
private $version; |
16
|
|
|
private $domain; |
17
|
|
|
private $token; |
18
|
|
|
private $to; |
19
|
|
|
private $subject; |
20
|
|
|
private $from; |
21
|
|
|
|
22
|
3 |
|
public function __construct($to, $subject, $from, $token, $domain, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'api.mailgun.net', $version = 'v3') |
23
|
|
|
{ |
24
|
3 |
|
if($version !== 'v3'){ |
25
|
|
|
throw new Exception("Version '{$version}' is not supported"); |
26
|
|
|
} |
27
|
|
|
|
28
|
3 |
|
$connectionString = $useSSL ? 'ssl://'.$host.':443' : $host.':80'; |
29
|
3 |
|
parent::__construct($connectionString, $level, $bubble); |
30
|
|
|
|
31
|
3 |
|
$this->to = $to; |
32
|
3 |
|
$this->subject = $subject; |
33
|
3 |
|
$this->from = $from; |
34
|
3 |
|
$this->host = $host; |
35
|
3 |
|
$this->version = $version; |
36
|
3 |
|
$this->domain = $domain; |
37
|
3 |
|
$this->token = $token; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
* |
43
|
|
|
* @param array $record |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
protected function generateDataStream($record) |
47
|
|
|
{ |
48
|
|
|
$content = $this->buildContent($record); |
49
|
|
|
return $this->buildHeader($content) . $content; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
* |
55
|
|
|
* @param array $record |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
protected function buildContent($record) |
59
|
|
|
{ |
60
|
|
|
$dataArray = array( |
61
|
|
|
'from' => $this->from, |
62
|
|
|
'to' => $this->to, |
63
|
|
|
'subject' => $this->subject, |
64
|
|
|
'text' => $record['formatted'] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return http_build_query($dataArray); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Builds the URL for the API call |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function buildRequestUrl() |
76
|
|
|
{ |
77
|
|
|
return "POST /{$this->version}/{$this->domain}/messages HTTP/1.1\r\n"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Builds the header of the API call |
82
|
|
|
* |
83
|
|
|
* @param string $content |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
private function buildHeader($content) |
87
|
|
|
{ |
88
|
|
|
$auth = base64_encode("api:".$this->token); |
89
|
|
|
|
90
|
|
|
$header = $this->buildRequestUrl(); |
91
|
|
|
|
92
|
|
|
$header .= "Host: {$this->host}\r\n"; |
93
|
|
|
$header .= "Authorization: Basic ".$auth."\r\n"; |
94
|
|
|
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
95
|
|
|
$header .= "Cache-Control: no-cache\r\n"; |
96
|
|
|
$header .= "Content-Length: " . strlen($content) . "\r\n"; |
97
|
|
|
$header .= "\r\n"; |
98
|
|
|
|
99
|
|
|
return $header; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|