Answers.Body: <h3>Edit: After the migration to Craft 3.1.x you should rather use this Code</h3>
  <pre class="lang-PHP prettyprint-override"><code>/**
   * @param string                            $html
   * @param string                            $subject
   * @param array|string|\craft\elements\User $mail
   *
   * @return bool
   */
  public function sendMail(string $html, string $subject, $mail): bool
  {
      return Craft::$app
          -&gt;getMailer()
          -&gt;compose()
          -&gt;setTo($mail)
          -&gt;setSubject($subject)
          -&gt;setHtmlBody($html)
          -&gt;send();
  }
  </code></pre>
  <h3>Edit: this is my old answer, valid for Craft 3.0.x</h3>
  <p>This is my code to send emails in Craft 3. Let me know if you have any questions.</p>
  <pre><code>use craft\mail\Message;
  
  /**
   * @param $html
   * @param $subject
   * @param null $mail
   * @param array $attachments
   * @return bool
   */
  private function sendMail($html, $subject, $mail = null, array $attachments = array()): bool
  {
      $settings = Craft::$app-&gt;systemSettings-&gt;getSettings('email');
      $message = new Message();
  
      $message-&gt;setFrom([$settings['fromEmail'] =&gt; $settings['fromName']]);
      $message-&gt;setTo($mail);
      $message-&gt;setSubject($subject);
      $message-&gt;setHtmlBody($html);
      if (!empty($attachments) &amp;&amp; \is_array($attachments)) {
  
          foreach ($attachments as $fileId) {
              if ($file = Craft::$app-&gt;assets-&gt;getAssetById((int)$fileId)) {
                  $message-&gt;attach($this-&gt;getFolderPath() . '/' . $file-&gt;filename, array(
                      'fileName' =&gt; $file-&gt;title . '.' . $file-&gt;getExtension()
                  ));
              }
          }
      }
  
      return Craft::$app-&gt;mailer-&gt;send($message);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-15T16:07:23.733Z
Answers.Text: To avoid the 5 minutes of googling around I just did, you need to add `use craft\mail\Message;` at the top of your file for the `$message = new Message();` call. See https://docs.craftcms.com/api/v3/craft-mail-message.html
Answers.UserDisplayName: Nate Beaty
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-15T21:09:38.327Z
Answers.Text: @natebeaty I suggest you to use a proper IDE. Most of them handle those issues nearly on their own.
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-16T22:21:19.357Z
Answers.Text: I know you mean well, but your comment comes off as condescending. Even if someone is using a "proper IDE" it's helpful for them to know they need `use craft\mail\Message;` above their class declaration to make this code example work.
Answers.UserDisplayName: Nate Beaty
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-09T20:42:02.637Z
Answers.Text: Pretty tough to figure out how to find out what portion of the class documentation to go to when trying to send an email. Couldnt find it there, so here I am.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-09T21:10:28.147Z
Answers.Text: I put in a request for edit, but I actually capitalized Craft\Mail when its supposed to be lowercase in the use statement.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-31T09:54:55.033Z
Answers.Text: First of all, thanks a lot for the detailed example Robin. 
  
  Although I got a `deprecated` warning on my IDE and it seems recommended to update the `getSettings` from systemSettings. These values should come from the project config. :)
Answers.UserDisplayName: bzin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-31T09:58:52.207Z
Answers.Text: That's why I said the it's valid for Craft 3.0.X without project config and the first answer is for Craft 3.1 with deprecated system settings
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-02-12T14:30:27.193Z
Answers.LastActivityDate: 2019-02-23T18:19:32.947Z
Answers.LastEditDate: 2020-06-17T08:56:27.903Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have created custom plugin in Craft 3 and want send email to receiver or site email when user submits a form.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-08T07:03:45.637Z
Comments.Text: I am new in craft please guide me where should i keep this code.
  Actually my task is -
  I have a replacement form that contain some fields. I want to send a mail to the admin with data and attachment Please help me
Comments.UserDisplayName: Shivam Kumar
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-08T07:21:07.360Z
Comments.Text: Hi Shivam, welcome to Craft SE! Since this is a new question (not an answer to the original question), I'd recommend [starting a new thread](https://craftcms.stackexchange.com/questions/ask). Feel free to link back to this thread for reference.
Comments.UserDisplayName: Lindsey D
Tags: <emails>
Title: How to send email from Craft 3 custom plugin?
ThreadText: # How to send email from Craft 3 custom plugin?
  
  (Asked by: username_0 on 2018-02-12)
  
  I have created custom plugin in Craft 3 and want send email to receiver or site email when user submits a form.
  
  **Tags:** emails
  
  > **Comments:**
  > **username_1**: I am new in craft please guide me where should i keep this code. Actually my task is - I have a replacement form that contain some fields. I want to send a mail to the admin with data and attachment Please help me  
  > **username_2**: Hi username_1, welcome to Craft SE! Since this is a new question (not an answer to the original question), I'd recommend [starting a new thread](https://craftcms.stackexchange.com/questions/ask). Feel free to link back to this thread for reference.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2018-02-12. Score: 22 | Accepted answer
  
  ### Edit: After the migration to Craft 3.1.x you should rather use this Code
  
  ```PHP
  /**
   * @param string                            $html
   * @param string                            $subject
   * @param array|string|\craft\elements\User $mail
   *
   * @return bool
   */
  public function sendMail(string $html, string $subject, $mail): bool
  {
      return Craft::$app
          ->getMailer()
          ->compose()
          ->setTo($mail)
          ->setSubject($subject)
          ->setHtmlBody($html)
          ->send();
  }
  
  ```
  
  ### Edit: this is my old answer, valid for Craft 3.0.x
  
  This is my code to send emails in Craft 3. Let me know if you have any questions.
  
  ```
  use craft\mail\Message;
  
  /**
   * @param $html
   * @param $subject
   * @param null $mail
   * @param array $attachments
   * @return bool
   */
  private function sendMail($html, $subject, $mail = null, array $attachments = array()): bool
  {
      $settings = Craft::$app->systemSettings->getSettings('email');
      $message = new Message();
  
      $message->setFrom([$settings['fromEmail'] => $settings['fromName']]);
      $message->setTo($mail);
      $message->setSubject($subject);
      $message->setHtmlBody($html);
      if (!empty($attachments) && \is_array($attachments)) {
  
          foreach ($attachments as $fileId) {
              if ($file = Craft::$app->assets->getAssetById((int)$fileId)) {
                  $message->attach($this->getFolderPath() . '/' . $file->filename, array(
                      'fileName' => $file->title . '.' . $file->getExtension()
                  ));
              }
          }
      }
  
      return Craft::$app->mailer->send($message);
  }
  
  ```
  
  > **Comments:**
  > **username_4**: To avoid the 5 minutes of googling around I just did, you need to add `use craft\mail\Message;` at the top of your file for the `$message = new Message();` call. See https://docs.craftcms.com/api/v3/craft-mail-message.html  
  > **username_3**: @username_4 I suggest you to use a proper IDE. Most of them handle those issues nearly on their own.  
  > **username_4**: I know you mean well, but your comment comes off as condescending. Even if someone is using a "proper IDE" it's helpful for them to know they need `use craft\mail\Message;` above their class declaration to make this code example work.  
  > **username_5**: Pretty tough to figure out how to find out what portion of the class documentation to go to when trying to send an email. Couldnt find it there, so here I am.  
  > **username_5**: I put in a request for edit, but I actually capitalized Craft\Mail when its supposed to be lowercase in the use statement.  
  > **username_6**: First of all, thanks a lot for the detailed example username_3.   Although I got a `deprecated` warning on my IDE and it seems recommended to update the `getSettings` from systemSettings. These values should come from the project config. :)  
  > **username_3**: That's why I said the it's valid for Craft 3.0.X without project config and the first answer is for Craft 3.1 with deprecated system settings  
  
  ---
  
  