Ecodev /
felix
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Ecodev\Felix\Service; |
||
| 6 | |||
| 7 | use Cake\Chronos\Chronos; |
||
| 8 | use Doctrine\ORM\EntityManager; |
||
| 9 | use Ecodev\Felix\Model\Message; |
||
| 10 | use Ecodev\Felix\Repository\LogRepository; |
||
| 11 | use Ecodev\Felix\Repository\MessageRepository; |
||
| 12 | use Exception; |
||
| 13 | use Symfony\Component\Mailer\Transport\TransportInterface; |
||
| 14 | use Symfony\Component\Mime\Address; |
||
| 15 | use Symfony\Component\Mime\Email; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Service to send a message as an email. |
||
| 19 | */ |
||
| 20 | class Mailer |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * DO NOT REMOVE THIS PROPERTY ! |
||
| 24 | * |
||
| 25 | * When it is garbage collected, the lock will be released. |
||
| 26 | * And the lock must only be released at the end of PHP process, |
||
| 27 | * never at the end of the method. |
||
| 28 | * |
||
| 29 | * @var false|resource |
||
| 30 | */ |
||
| 31 | private $lock; |
||
| 32 | |||
| 33 | 1 | public function __construct( |
|
| 34 | private readonly EntityManager $entityManager, |
||
| 35 | private readonly MessageRepository $messageRepository, |
||
| 36 | private readonly TransportInterface $transport, |
||
| 37 | private readonly string $phpPath, |
||
| 38 | private readonly ?string $toEmailOverride, |
||
| 39 | private readonly string $fromEmail, |
||
| 40 | protected string $fromName, |
||
| 41 | 1 | ) {} |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Send a message asynchronously in a separate process. |
||
| 45 | * |
||
| 46 | * This should be the preferred way to send a message, unless if we are the cron. |
||
| 47 | */ |
||
| 48 | public function sendMessageAsync(Message $message): void |
||
| 49 | { |
||
| 50 | // Be sure we have an ID before "forking" process |
||
| 51 | if ($message->getId() === null) { |
||
| 52 | $this->entityManager->flush(); |
||
| 53 | } |
||
| 54 | |||
| 55 | $args = [ |
||
| 56 | (string) realpath('bin/send-message.php'), |
||
| 57 | (string) $message->getId(), |
||
| 58 | ]; |
||
| 59 | |||
| 60 | $escapedArgs = array_map('escapeshellarg', $args); |
||
| 61 | |||
| 62 | $cmd = escapeshellcmd($this->phpPath) . ' ' . implode(' ', $escapedArgs) . ' > /dev/null 2>&1 &'; |
||
| 63 | exec($cmd); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Send a message. |
||
| 68 | */ |
||
| 69 | 1 | public function sendMessage(Message $message): void |
|
| 70 | { |
||
| 71 | 1 | $mailMessage = $this->messageToEmail($message); |
|
| 72 | |||
| 73 | 1 | $email = $message->getEmail(); |
|
| 74 | 1 | $overriddenBy = ''; |
|
| 75 | 1 | if ($this->toEmailOverride) { |
|
| 76 | $email = $this->toEmailOverride; |
||
| 77 | $overriddenBy = ' overridden by ' . $email; |
||
| 78 | } |
||
| 79 | |||
| 80 | 1 | $recipient = $message->getRecipient(); |
|
| 81 | 1 | $recipientName = $recipient?->getName() ?: ''; |
|
| 82 | 1 | if ($email) { |
|
| 83 | 1 | $mailMessage->addTo(new Address($email, $recipientName)); |
|
| 84 | 1 | $this->transport->send($mailMessage); |
|
| 85 | } |
||
| 86 | |||
| 87 | 1 | $message->setDateSent(new Chronos()); |
|
| 88 | 1 | $this->entityManager->flush(); |
|
| 89 | |||
| 90 | 1 | $addressList = $mailMessage->getFrom(); |
|
| 91 | 1 | if ($addressList) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 92 | 1 | echo 'email from ' . $addressList[0]->getAddress() . ' sent to: ' . $message->getEmail() . "\t" . $overriddenBy . "\t" . $message->getSubject() . PHP_EOL; |
|
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Convert our model message to an email. |
||
| 98 | */ |
||
| 99 | 1 | protected function messageToEmail(Message $message): Email |
|
| 100 | { |
||
| 101 | 1 | $email = new Email(); |
|
| 102 | 1 | $email->subject($message->getSubject()); |
|
| 103 | 1 | $email->html($message->getBody()); |
|
| 104 | |||
| 105 | 1 | $email->from(new Address($this->fromEmail, $this->fromName)); |
|
| 106 | |||
| 107 | 1 | return $email; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Send all messages that are not sent yet. |
||
| 112 | */ |
||
| 113 | public function sendAllMessages(): void |
||
| 114 | { |
||
| 115 | $this->acquireLock(); |
||
| 116 | |||
| 117 | $messages = $this->messageRepository->getAllMessageToSend(); |
||
| 118 | foreach ($messages as $message) { |
||
| 119 | $this->sendMessage($message); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Acquire an exclusive lock. |
||
| 125 | * |
||
| 126 | * This is to ensure only one mailer can run at any given time. This is to prevent sending the same email twice. |
||
| 127 | */ |
||
| 128 | private function acquireLock(): void |
||
| 129 | { |
||
| 130 | $lockFile = 'data/tmp/mailer.lock'; |
||
| 131 | touch($lockFile); |
||
| 132 | $this->lock = fopen($lockFile, 'r+b'); |
||
| 133 | if ($this->lock === false) { |
||
| 134 | throw new Exception('Could not read lock file. This is not normal and might be a permission issue'); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (!flock($this->lock, LOCK_EX | LOCK_NB)) { |
||
| 138 | $message = LogRepository::MAILER_LOCKED; |
||
| 139 | _log()->info($message); |
||
| 140 | |||
| 141 | echo $message . PHP_EOL; |
||
| 142 | echo 'If the problem persist and another mailing is not in progress, try deleting ' . $lockFile . PHP_EOL; |
||
| 143 | |||
| 144 | // Not getting the lock is not considered as error to avoid being spammed |
||
| 145 | exit(); |
||
|
0 ignored issues
–
show
|
|||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 |