PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
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 1 year ago MailgunService.php 1 year ago PHPMailService.php 1 year ago SMTPService.php 1 year ago WpMailService.php 1 year ago
MailgunService.php
90 lines
1 <?php
2 /**
3 * @copyright © TMS-Plugins. All rights reserved.
4 * @licence See LICENCE.md for license details.
5 */
6
7 namespace AmeliaBooking\Infrastructure\Services\Notification;
8
9 use AmeliaBooking\Domain\Services\Notification\AbstractMailService;
10 use AmeliaBooking\Domain\Services\Notification\MailServiceInterface;
11 use Mailgun\Mailgun;
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 $mgClient = $this->endpoint ? Mailgun::create($this->apiKey, $this->endpoint) : Mailgun::create($this->apiKey);
58
59 $mgArgs = [
60 'from' => "{$this->fromName} <{$this->from}>",
61 'to' => $to,
62 'subject' => $subject,
63 'html' => $body,
64 'attachment' => [],
65 'h:Reply-To' => !empty($this->replyTo) ? $this->replyTo : $this->from
66 ];
67
68 if ($bccEmails) {
69 $mgArgs['bcc'] = implode(', ', $bccEmails);
70 }
71
72 foreach ($attachments as $attachment) {
73 if (!empty($attachment['content'])) {
74 $isInvoice = strpos($attachment['type'], 'pdf') !== false;
75 if ($isInvoice) {
76 $tmpFile = tempnam(sys_get_temp_dir(), 'Invoice_');
77 } else {
78 $tmpFile = tempnam(sys_get_temp_dir(), 'cal_');
79 }
80 if ($tmpFile &&
81 file_put_contents($tmpFile, $attachment['content']) !== false &&
82 @rename($tmpFile, $tmpFile .= ($isInvoice ? '.pdf' : '.ics')) !== false) {
83 $mgArgs['attachment'][] = ['filePath' => $tmpFile, 'filename' => $tmpFile];
84 }
85 }
86 }
87
88 $mgClient->messages()->send($this->domain, $mgArgs);
89 }
90 }