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 | } |