|
4 | 4 | namespace App\Service;
|
5 | 5 |
|
6 | 6 | use App\Factory\MailerFactory;
|
| 7 | +use App\Model\Mail\Mail; |
| 8 | +use App\Table\Mail\MailTable; |
7 | 9 | use League\Plates\Engine;
|
8 | 10 | use Monolog\Logger;
|
9 | 11 | use PHPMailer\PHPMailer\Exception;
|
10 | 12 |
|
11 |
| -readonly class MailerService |
| 13 | +class MailerService |
12 | 14 | {
|
13 | 15 |
|
| 16 | + private Mail $mail; |
| 17 | + |
14 | 18 | public function __construct(
|
15 |
| - private MailerFactory $mailer, |
16 |
| - private Engine $template, |
17 |
| - private Logger $logger |
| 19 | + private readonly MailerFactory $mailer, |
| 20 | + private readonly MailTable $mailTable, |
| 21 | + private readonly Engine $template, |
| 22 | + private readonly Logger $logger |
18 | 23 | ) {
|
19 | 24 | }
|
20 | 25 |
|
21 |
| - public function configureMail(string $to, string $subject, string $template, array $content = []): self |
| 26 | + public function configureMail(string $to, string $subject, int $template, array $content = [], int $account = null): self |
22 | 27 | {
|
23 |
| - $this->mailer->getMailer()->addAddress($to); |
24 |
| - |
25 |
| - $this->mailer->getMailer()->isHTML(); |
26 |
| - |
27 |
| - $this->mailer->getMailer()->Subject = $subject; |
28 |
| - $this->mailer->getMailer()->Body = $this->template->render('mail/' . $template, $content); |
| 28 | + $this->mail = new Mail(); |
| 29 | + $this->mail->setEmail($to); |
| 30 | + $this->mail->setAccount($account); |
| 31 | + $this->mail->setType($template); |
| 32 | + $this->mail->setSubject($subject); |
| 33 | + $this->mail->setContent($content); |
29 | 34 |
|
30 | 35 | return $this;
|
31 | 36 | }
|
32 | 37 |
|
33 |
| - public function send(): bool |
| 38 | + public function send(): void |
34 | 39 | {
|
35 |
| - try { |
36 |
| - $this->mailer->getMailer()->send(); |
37 |
| - return true; |
38 |
| - } catch (Exception) { |
39 |
| - $this->logger->error($this->mailer->getMailer()->ErrorInfo); |
40 |
| - return false; |
| 40 | + $this->mailTable->insert($this->mail); |
| 41 | + } |
| 42 | + |
| 43 | + public function fetchMailsAndSend(): void |
| 44 | + { |
| 45 | + $this->logger->info('Starting Mail Batch'); |
| 46 | + $mails = $this->mailTable->findAllNotSentLimit(); |
| 47 | + |
| 48 | + $amount = count($mails); |
| 49 | + $this->logger->info('Collected a batch of ' . $amount . ' Mails.'); |
| 50 | + $success = 0; |
| 51 | + |
| 52 | + foreach ($mails as $mail) { |
| 53 | + |
| 54 | + $time = microtime(true); |
| 55 | + $this->mailer->getMailer()->isHTML(); |
| 56 | + $this->mailer->getMailer()->Subject = $mail['subject']; |
| 57 | + $this->mailer->getMailer()->addAddress($mail['email']); |
| 58 | + $this->mailer->getMailer()->Body = |
| 59 | + $this->template->render('mail/'.$mail['template'], json_decode($mail['content'], true)); |
| 60 | + |
| 61 | + try { |
| 62 | + $this->mailer->getMailer()->send(); |
| 63 | + $this->mailTable->updateMailSentById($mail['id']); |
| 64 | + $success++; |
| 65 | + $this->logger->info('Mail Sending took ' . microtime(true) - $time . 'ms ('.$success.'/'.$amount.')'); |
| 66 | + } catch (Exception) |
| 67 | + { |
| 68 | + $this->logger->error('Mail Sending failed after ' . microtime() - $time . 'ms', [$this->mailer->getMailer()->ErrorInfo]); |
| 69 | + } |
| 70 | + |
41 | 71 | }
|
| 72 | + |
| 73 | + $this->logger->info('Mail Batch complete. Sent a total of ' . $success . ' E-Mails'); |
| 74 | + |
42 | 75 | }
|
43 | 76 |
|
44 | 77 | }
|
0 commit comments