summaryrefslogtreecommitdiff
path: root/src/application/jobs/email.php
blob: 8bb97dc470c5b3a05769e2250de3eaf54a3ae2ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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);
}