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
SMTPService.php
110 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 | use Exception; |
| 13 | use AmeliaVendor\PHPMailer\PHPMailer\PHPMailer; |
| 14 | |
| 15 | /** |
| 16 | * Class SMTPService |
| 17 | */ |
| 18 | class SMTPService extends AbstractMailService implements MailServiceInterface |
| 19 | { |
| 20 | /** @var string */ |
| 21 | private $host; |
| 22 | |
| 23 | /** @var int */ |
| 24 | private $port; |
| 25 | |
| 26 | /** @var string */ |
| 27 | private $username; |
| 28 | |
| 29 | /** @var string */ |
| 30 | private $password; |
| 31 | |
| 32 | /** @var string */ |
| 33 | private $secure; |
| 34 | |
| 35 | /** |
| 36 | * SMTPService constructor. |
| 37 | * |
| 38 | * @param $from |
| 39 | * @param $fromName |
| 40 | * @param string $host |
| 41 | * @param int $port |
| 42 | * @param string $secure |
| 43 | * @param string $username |
| 44 | * @param string $password |
| 45 | * @param $replyTo |
| 46 | */ |
| 47 | public function __construct($from, $fromName, $host, $port, $secure, $username, $password, $replyTo) |
| 48 | { |
| 49 | parent::__construct($from, $fromName, $replyTo); |
| 50 | $this->host = $host; |
| 51 | $this->port = $port; |
| 52 | $this->secure = $secure; |
| 53 | $this->username = $username; |
| 54 | $this->password = $password; |
| 55 | } |
| 56 | |
| 57 | /** @noinspection MoreThanThreeArgumentsInspection */ |
| 58 | /** |
| 59 | * @param $to |
| 60 | * @param $subject |
| 61 | * @param $body |
| 62 | * @param array $bccEmails |
| 63 | * @param array $attachments |
| 64 | * |
| 65 | * @return mixed|void |
| 66 | * @throws Exception |
| 67 | * @SuppressWarnings(PHPMD) |
| 68 | */ |
| 69 | public function send($to, $subject, $body, $bccEmails = [], $attachments = []) |
| 70 | { |
| 71 | $mail = new PHPMailer(true); |
| 72 | |
| 73 | try { |
| 74 | //Server settings |
| 75 | $mail->isSMTP(); |
| 76 | $mail->SMTPAuth = true; |
| 77 | $mail->SMTPSecure = $this->secure; |
| 78 | $mail->Host = $this->host; |
| 79 | $mail->Port = $this->port; |
| 80 | $mail->Username = $this->username; |
| 81 | $mail->Password = $this->password; |
| 82 | |
| 83 | //Recipients |
| 84 | $mail->setFrom($this->from, $this->fromName); |
| 85 | $mail->addAddress($to); |
| 86 | $mail->addReplyTo(!empty($this->replyTo) ? $this->replyTo : $this->from); |
| 87 | |
| 88 | foreach ($bccEmails as $bccEmail) { |
| 89 | $mail->addBCC($bccEmail); |
| 90 | } |
| 91 | |
| 92 | foreach ($attachments as $attachment) { |
| 93 | if (!empty($attachment['content'])) { |
| 94 | $mail->addStringAttachment($attachment['content'], $attachment['name'], 'base64', $attachment['type']); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | //Content |
| 99 | $mail->CharSet = 'UTF-8'; |
| 100 | $mail->isHTML(); |
| 101 | $mail->Subject = $subject; |
| 102 | $mail->Body = $body; |
| 103 | |
| 104 | $mail->send(); |
| 105 | } catch (Exception $e) { |
| 106 | throw $e; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 |