I want to send an email using crafts mailer in Craft 4. I've seen this post: How to send email from Craft 3 custom plugin?

But now I need this adapted for craft 4. Unfortunately I did not succeed so much.

I think something like this:

$mail = Craft::$app->mailer;

$mail->template = "My email template. How to use placeholders?";
$mail->send($message); // how do I set up that message?
// where can I set the recipient address?

Links: https://docs.craftcms.com/api/v4/craft-mail-mailer.html#public-methods

I want to use the settings I have set in the CP:

enter image description here

I would be very thankful for an example, as the Craft 4 documentation is a bit to sparse for somebody like me 😅.

1 Answer 1


You can do something like this

    $view               = Craft::$app->getView();
    $email              = '[email protected]'
    $subject            = $view->renderString($messageSubject, $variables, View::TEMPLATE_MODE_SITE);
    $html               = $view->renderTemplate($template,$variables , View::TEMPLATE_MODE_SITE);

  • Sorry to ask again... This looks promising, but I have still some noob questions: - what is the $template for example? Where can I define those, where can I get that? - What should the $variables array contain? The link here: docs.craftcms.com/api/v4/craft-mail-mailer.html#public-methods lists some but I still don't get how it works... :s - $messageSubject is just a string, right? - What is the View::TEMPLATE_MODE_SITE part?
    – Merc
    Commented Feb 27, 2023 at 13:50
  • 1
    @Merc, $template is your twig template path. $variables yes it is contain array. This contain variable you want to pass to your twig template, The View::TEMPLATE_MODE_SITE is option to identify Craft where they should search your template, so View::TEMPLATE_MODE_SITE is means is using the template folder you use normally. if you no need to render from twig template you can just put your html code inside the $html variable.
    – aodihis
    Commented Feb 28, 2023 at 3:21
  • Ah perfect. No I don't need a template. And thanks for all the other answers. I will try that!
    – Merc
    Commented Feb 28, 2023 at 8:59
  • I tried, for the sake of completeness, to render a twig template in my $html. But this line fails already: $view = Craft::$app->getView(); with this error: \"modules\\commentmodule\\controllers\\View\" not found", Is this related to: verbb.io/blog/… > so I need to register views in my controller first? (I will try and report back here, if I find a solution :)) Sorry for all the beginners questions :S
    – Merc
    Commented Feb 28, 2023 at 9:17
  • I think I need to register the views, but I fail... I register the view like here (verbb.io/blog/…) in my /modules/commentmodule/CommentModule.php. I have /modules/commentmodule/templates/emails/mail.twig and I try to render it $html = Craft::$app->getView()->renderTemplate('commentmodule/emails/mail.twig', $variables); – but then it will fail with Template could not be found. :( And I try to render that template within modules/commentmodule/controllers/MailController.php
    – Merc
    Commented Feb 28, 2023 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.