Skip to main content

Trigger an Email from a module

How to trigger a twig rendered email from a custom module

Das Template wird zuerst gerendert und dann dem htmlBody übergeben

public function sendOfflineAlert(string $mail, string $subject, string $fallback, $order)
    {
        Craft::$app->getView()->setTemplateMode(View::TEMPLATE_MODE_SITE);
        $html = Craft::$app->getView()->renderTemplate('_emails/offlinePaymentAlert.twig', [ 'order' => $order]);
        $mailer = Craft::$app->getMailer();
        $mailer->htmlLayout = false;
        $mailer->textLayout = false;
        $mailer->compose()
            ->setTo($mail)
            ->setSubject($subject)
            ->setHtmlBody($html)
            ->send();
    }