Answers.Body: <p>You can do something like this</p>
  <pre><code>    $view               = Craft::$app-&gt;getView();
      $email              = 'example@example.com'
      $subject            = $view-&gt;renderString($messageSubject, $variables, View::TEMPLATE_MODE_SITE);
      $html               = $view-&gt;renderTemplate($template,$variables , View::TEMPLATE_MODE_SITE);
  
      Craft::$app
          -&gt;getMailer()
          -&gt;compose()
          -&gt;setTo($email)
          -&gt;setSubject($subject)
          -&gt;setHtmlBody($html)
          -&gt;send();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-27T13:50:27.233Z
Answers.Text: 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: https://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?
Answers.UserDisplayName: Merc
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-28T03:21:45.323Z
Answers.Text: @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.
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-28T08:59:35.220Z
Answers.Text: Ah perfect. No I don't need a template. 
  And thanks for all the other answers. I will try that!
Answers.UserDisplayName: Merc
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-28T09:17:30.627Z
Answers.Text: 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: https://verbb.io/blog/everything-you-need-to-know-about-modules#templates > 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
Answers.UserDisplayName: Merc
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-28T09:37:33.753Z
Answers.Text: I think I need to register the views, but I fail...
  I register the view like here (https://verbb.io/blog/everything-you-need-to-know-about-modules#templates) 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`
Answers.UserDisplayName: Merc
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-17T07:10:56.053Z
Answers.LastActivityDate: 2023-02-17T07:10:56.053Z
Answers.UserDisplayName: aodihis
Body: <p>I want to send an email using crafts mailer in Craft 4.
  I've seen this post:
  <a href="https://craftcms.stackexchange.com/questions/23804/how-to-send-email-from-craft-3-custom-plugin">How to send email from Craft 3 custom plugin?</a></p>
  <p>But now I need this adapted for craft 4.
  Unfortunately I did not succeed so much.</p>
  <p>I think something like this:</p>
  <pre class="lang-php prettyprint-override"><code>$mail = Craft::$app-&gt;mailer;
  
  $mail-&gt;template = &quot;My email template. How to use placeholders?&quot;;
  $mail-&gt;send($message); // how do I set up that message?
  // where can I set the recipient address?
  </code></pre>
  <p>Links: <a href="https://docs.craftcms.com/api/v4/craft-mail-mailer.html#public-methods" rel="nofollow noreferrer">https://docs.craftcms.com/api/v4/craft-mail-mailer.html#public-methods</a></p>
  <p>I want to use the settings I have set in the CP:</p>
  <p><a href="https://i.sstatic.net/um56a.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/um56a.png" alt="enter image description here" /></a></p>
  <p>I would be very thankful for an example, as the Craft 4 documentation is a bit to sparse for somebody like me .</p>
  
Tags: <emails><craft4><smtp>
Title: How to send email from Craft 4 custom plugin/module?
ThreadText: # How to send email from Craft 4 custom plugin/module?
  
  (Asked by: username_0 on 2023-02-16)
  
  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?](https://craftcms.stackexchange.com/questions/23804/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:
  
  ```php
  $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](https://i.sstatic.net/um56a.png)](https://i.sstatic.net/um56a.png)
  
  I would be very thankful for an example, as the Craft 4 documentation is a bit to sparse for somebody like me .
  
  **Tags:** emails, craft4, smtp
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-02-17. Score: 3
  
  You can do something like this
  
  ```
      $view               = Craft::$app->getView();
      $email              = 'example@example.com'
      $subject            = $view->renderString($messageSubject, $variables, View::TEMPLATE_MODE_SITE);
      $html               = $view->renderTemplate($template,$variables , View::TEMPLATE_MODE_SITE);
  
      Craft::$app
          ->getMailer()
          ->compose()
          ->setTo($email)
          ->setSubject($subject)
          ->setHtmlBody($html)
          ->send();
  
  ```
  
  > **Comments:**
  > **username_0**: 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: https://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?  
  > **username_1**: @username_0, $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.  
  > **username_0**: Ah perfect. No I don't need a template.  And thanks for all the other answers. I will try that!  
  > **username_0**: 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: https://verbb.io/blog/everything-you-need-to-know-about-modules#templates > 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  
  > **username_0**: I think I need to register the views, but I fail... I register the view like here (https://verbb.io/blog/everything-you-need-to-know-about-modules#templates) 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`  
  
  ---
  
  