|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\Mail\Wrapper; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\Mail\Envelope; |
|
6
|
|
|
use ByJG\Mail\Exception\InvalidEMailException; |
|
7
|
|
|
use ByJG\Mail\Exception\MailApiException; |
|
8
|
|
|
use ByJG\Util\CurlException; |
|
|
|
|
|
|
9
|
|
|
use ByJG\Util\Helper\RequestMultiPart; |
|
10
|
|
|
use ByJG\Util\HttpClient; |
|
11
|
|
|
use ByJG\Util\MultiPartItem; |
|
12
|
|
|
use ByJG\Util\Psr7\Request; |
|
13
|
|
|
use ByJG\Util\Uri; |
|
14
|
|
|
|
|
15
|
|
|
class MailgunApiWrapper extends PHPMailerWrapper |
|
16
|
|
|
{ |
|
17
|
|
|
private $client; |
|
18
|
|
|
|
|
19
|
|
|
private $regions = [ |
|
20
|
|
|
'us' => 'api.mailgun.net', |
|
21
|
|
|
'eu' => 'api.eu.mailgun.net', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
5 |
|
public function __construct(Uri $uri, HttpClient $client = null) |
|
25
|
|
|
{ |
|
26
|
5 |
|
parent::__construct($uri); |
|
27
|
|
|
|
|
28
|
5 |
|
$this->client = $client; |
|
29
|
5 |
|
if (is_null($client)) { |
|
30
|
1 |
|
$this->client = new HttpClient(); |
|
31
|
|
|
} |
|
32
|
5 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return Request |
|
36
|
|
|
* @throws \ByJG\Util\Psr7\MessageException |
|
37
|
|
|
*/ |
|
38
|
5 |
|
public function getRequestObject() |
|
39
|
|
|
{ |
|
40
|
5 |
|
$domainName = $this->uri->getHost(); |
|
41
|
5 |
|
$apiUri = $this->getApiUri(); |
|
42
|
|
|
|
|
43
|
5 |
|
$uri = Uri::getInstanceFromString("https://$apiUri/v3/$domainName/messages") |
|
44
|
5 |
|
->withUserInfo('api', $this->uri->getUsername()); |
|
45
|
|
|
|
|
46
|
5 |
|
return Request::getInstance($uri)->withMethod("POST"); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* malgun://api:APIKEY@DOMAINNAME |
|
51
|
|
|
* |
|
52
|
|
|
* @param Envelope $envelope |
|
53
|
|
|
* @return bool |
|
54
|
|
|
* @throws MailApiException |
|
55
|
|
|
* @throws InvalidEMailException |
|
56
|
|
|
* @throws CurlException |
|
57
|
|
|
* @throws \ByJG\Util\Psr7\MessageException |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function send(Envelope $envelope) |
|
60
|
|
|
{ |
|
61
|
4 |
|
$this->validate($envelope); |
|
62
|
|
|
|
|
63
|
|
|
$message = [ |
|
64
|
4 |
|
new MultiPartItem('from', $envelope->getFrom()), |
|
65
|
4 |
|
new MultiPartItem('subject', $envelope->getSubject()), |
|
66
|
4 |
|
new MultiPartItem('html', $envelope->getBody()), |
|
67
|
4 |
|
new MultiPartItem('text', $envelope->getBodyText()), |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
4 |
|
foreach ((array)$envelope->getTo() as $to) { |
|
72
|
4 |
|
$message[] = new MultiPartItem('to', $to); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
foreach ((array)$envelope->getBCC() as $bcc) { |
|
76
|
3 |
|
$message[] = new MultiPartItem('bcc', $bcc); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
if (!empty($envelope->getReplyTo())) { |
|
80
|
4 |
|
$message[] = new MultiPartItem('h:Reply-To', $envelope->getReplyTo()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
foreach ((array)$envelope->getCC() as $cc) { |
|
84
|
3 |
|
$message[] = new MultiPartItem('cc', $cc); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
foreach ((array)$envelope->getAttachments() as $name => $attachment) { |
|
88
|
2 |
|
$message[] = new MultiPartItem( |
|
89
|
2 |
|
$attachment['disposition'], |
|
90
|
2 |
|
file_get_contents($attachment['content']), |
|
91
|
|
|
$name, |
|
92
|
2 |
|
$attachment['content-type'] |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
$request = RequestMultiPart::buildMultiPart($message, $this->getRequestObject()); |
|
97
|
|
|
|
|
98
|
4 |
|
$result = $this->client->sendRequest($request); |
|
|
|
|
|
|
99
|
4 |
|
if ($result->getStatusCode() != 200) { |
|
100
|
|
|
throw new MailApiException("Mailgun result code is " . $result->getStatusCode()); |
|
101
|
|
|
} |
|
102
|
4 |
|
$resultJson = json_decode($result->getBody()->getContents(), true); |
|
103
|
4 |
|
if (!isset($resultJson['id'])) { |
|
104
|
|
|
throw new MailApiException('Mailgun: ' . $resultJson['message']); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
5 |
|
private function getApiUri() |
|
111
|
|
|
{ |
|
112
|
5 |
|
$query = $this->uri->getQueryPart('region'); |
|
113
|
5 |
|
if (isset($this->regions[$query])) { |
|
114
|
|
|
return $this->regions[$query]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
5 |
|
return $this->regions['us']; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths