diff options
Diffstat (limited to 'src/application/jobs/email.php')
-rw-r--r-- | src/application/jobs/email.php | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/application/jobs/email.php b/src/application/jobs/email.php new file mode 100644 index 0000000..8bb97dc --- /dev/null +++ b/src/application/jobs/email.php @@ -0,0 +1,33 @@ +<?php + +use mystic\forum\orm\PendingEmail; +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; +use Symfony\Component\Mailer\Transport; +use Symfony\Component\Mime\Email; + +check_sapi(); + +$db = get_db(); + +/** @var PendingEmail[] $pendingEmails */ +$pendingEmails = $db->fetchAll(PendingEmail::class); + +$transport = Transport::fromDsn(getenv("MAILER_DSN")); + +foreach ($pendingEmails as $pendingEmail) { + try { + $mail = (new Email) + ->from($pendingEmail->sender) + ->to($pendingEmail->recipient) + ->subject($pendingEmail->subject) + ; + if (!empty($pendingEmail->htmlBody)) + $mail->html($pendingEmail->htmlBody); + if (!empty($pendingEmail->plaintextBody)) + $mail->text($pendingEmail->plaintextBody); + $transport->send($mail); + } catch (TransportExceptionInterface $ex) { + print_error("Failed to send mail: " . $ex->getMessage() . "\n"); + } + $db->delete($pendingEmail); +} |