I am building a Craft plugin which sends emails to users of a subscribed list. I'm trying to get Craft to send the email with a Twig template and pass in variables to that template.

So far I've been able to get the Twig template working properly, except I'm having issues displaying the variables heading and content in my template. They are returned as plain text.

Here's my code for sending the email.

$mailer = Craft::$app->mailer;
$mailer->htmlLayout = false;
$mailer->textLayout = false;

   ->compose('emails/default.twig', [
      'heading' => $list->subject,
      'content' => "Something new has been posted on the Harvest Youth blog. <a href='{$entry->url}'>{$entry->title}</a>",

Here's how I'm trying to use the variables in my Twig template.

<div style="margin: 4px;">
   <h1 style="font-weight: 700; font-size: 30px;">{{ heading }}</h1>
   {{ content }}

And here's how the email template is returned.

enter image description here

I'd appreciate any help as I'm new to Craft plugins and I'm not fantastic with Twig either.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.