PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / Services / Notification / MailgunService.php
ameliabooking / src / Infrastructure / Services / Notification Last commit date
MailerFactory.php 4 months ago MailgunService.php 3 months ago OutlookMiddlewareService.php 4 months ago OutlookService.php 6 months ago PHPMailService.php 6 months ago SMTPService.php 6 months ago WpMailService.php 6 months ago
MailgunService.php
107 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Infrastructure\Services\Notification;
9
10 use AmeliaBooking\Domain\Services\Notification\AbstractMailService;
11 use AmeliaBooking\Domain\Services\Notification\MailServiceInterface;
12
13 /**
14 * Class MailgunService
15 */
16 class MailgunService extends AbstractMailService implements MailServiceInterface
17 {
18 /** @var string */
19 private $apiKey;
20
21 /** @var string */
22 private $domain;
23
24 /** @var string */
25 private $endpoint;
26
27 /**
28 * MailgunService constructor.
29 *
30 * @param string $from
31 * @param string $fromName
32 * @param string $apiKey
33 * @param string $domain
34 * @param string $endpoint
35 */
36 public function __construct($from, $fromName, $apiKey, $domain, $endpoint, $replyTo)
37 {
38 parent::__construct($from, $fromName, $replyTo);
39 $this->apiKey = $apiKey;
40 $this->domain = $domain;
41 $this->endpoint = $endpoint;
42 }
43
44 /** @noinspection MoreThanThreeArgumentsInspection */
45 /**
46 * @param $to
47 * @param $subject
48 * @param $body
49 * @param array $bccEmails
50 * @param array $attachments
51 *
52 * @return mixed|void
53 * @SuppressWarnings(PHPMD)
54 */
55 public function send($to, $subject, $body, $bccEmails = [], $attachments = [])
56 {
57 $mgArgs = [
58 'from' => "{$this->fromName} <{$this->from}>",
59 'to' => $to,
60 'subject' => $subject,
61 'html' => $body,
62 'h:Reply-To' => !empty($this->replyTo) ? $this->replyTo : $this->from
63 ];
64
65 if ($bccEmails) {
66 $mgArgs['bcc'] = implode(', ', $bccEmails);
67 }
68
69 $attachmentIndex = 0;
70 foreach ($attachments as $attachment) {
71 if (!empty($attachment['content'])) {
72 $extension = pathinfo($attachment['name'], PATHINFO_EXTENSION);
73 $fileName = pathinfo($attachment['name'], PATHINFO_FILENAME);
74 $tmpFile = tempnam(sys_get_temp_dir(), $fileName . '_');
75 if (
76 $tmpFile &&
77 file_put_contents($tmpFile, $attachment['content']) !== false &&
78 @rename($tmpFile, $tmpFile .= '.' . $extension) !== false
79 ) {
80 $mgArgs["attachment[$attachmentIndex]"] = new \CURLFile($tmpFile, mime_content_type($tmpFile), $attachment['name']);
81 $attachmentIndex++;
82 }
83 }
84 }
85
86 $endpoint = $this->endpoint ?: 'https://api.mailgun.net/';
87
88 $url = $endpoint . "v3/{$this->domain}/messages";
89
90 $ch = curl_init();
91
92 curl_setopt($ch, CURLOPT_URL, $url);
93 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
94 curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $this->apiKey);
95 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
96 curl_setopt($ch, CURLOPT_POST, true);
97
98 curl_setopt($ch, CURLOPT_POSTFIELDS, $mgArgs);
99
100 $response = curl_exec($ch);
101
102 if ($response === false) {
103 throw new \RuntimeException('Mailgun error: ' . curl_error($ch));
104 }
105 }
106 }
107