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
OutlookMiddlewareService.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Services\Notification; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Services\Notification\AbstractMailService; |
| 6 | use AmeliaBooking\Domain\Services\Notification\MailServiceInterface; |
| 7 | use AmeliaBooking\Infrastructure\Services\Outlook\AbstractOutlookCalendarMiddlewareService; |
| 8 | use Exception; |
| 9 | |
| 10 | /** |
| 11 | * Class OutlookMiddlewareService |
| 12 | */ |
| 13 | class OutlookMiddlewareService extends AbstractMailService implements MailServiceInterface |
| 14 | { |
| 15 | /** @var AbstractOutlookCalendarMiddlewareService */ |
| 16 | private $outlookCalendarMiddlewareService; |
| 17 | |
| 18 | /** |
| 19 | * OutlookMiddlewareService constructor. |
| 20 | * |
| 21 | * @param AbstractOutlookCalendarMiddlewareService $outlookCalendarMiddlewareService |
| 22 | * @param string $from |
| 23 | * @param string $fromName |
| 24 | * @param string $replyTo |
| 25 | */ |
| 26 | public function __construct($outlookCalendarMiddlewareService, $from, $fromName, $replyTo) |
| 27 | { |
| 28 | $this->outlookCalendarMiddlewareService = $outlookCalendarMiddlewareService; |
| 29 | |
| 30 | parent::__construct($from, $fromName, $replyTo); |
| 31 | } |
| 32 | |
| 33 | /** @noinspection MoreThanThreeArgumentsInspection */ |
| 34 | /** |
| 35 | * @param string $to |
| 36 | * @param string $subject |
| 37 | * @param string $body |
| 38 | * @param array $bcc |
| 39 | * @param array $attachments |
| 40 | * |
| 41 | * @return void |
| 42 | * @throws Exception |
| 43 | * @SuppressWarnings(PHPMD) |
| 44 | */ |
| 45 | public function send($to, $subject, $body, $bcc = [], $attachments = []) |
| 46 | { |
| 47 | $attachmentsList = []; |
| 48 | |
| 49 | foreach ($attachments as $attachment) { |
| 50 | if (!empty($attachment['content'])) { |
| 51 | $extension = pathinfo($attachment['name'], PATHINFO_EXTENSION); |
| 52 | $fileName = pathinfo($attachment['name'], PATHINFO_FILENAME); |
| 53 | $tmpFile = tempnam(sys_get_temp_dir(), $fileName . '_'); |
| 54 | if ( |
| 55 | $tmpFile && |
| 56 | file_put_contents($tmpFile, $attachment['content']) !== false && |
| 57 | @rename($tmpFile, $tmpFile .= '.' . $extension) !== false |
| 58 | ) { |
| 59 | $attachmentsList[] = ['filePath' => $tmpFile, 'fileName' => $attachment['name']]; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $this->outlookCalendarMiddlewareService->sendEmail( |
| 65 | $this->from, |
| 66 | $this->fromName, |
| 67 | $this->replyTo, |
| 68 | $to, |
| 69 | $subject, |
| 70 | $body, |
| 71 | $bcc, |
| 72 | $attachmentsList |
| 73 | ); |
| 74 | |
| 75 | // Clean up temporary attachment files |
| 76 | foreach ($attachmentsList as $attachment) { |
| 77 | if (!empty($attachment['filePath']) && file_exists($attachment['filePath'])) { |
| 78 | @unlink($attachment['filePath']); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 |