<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>

CC BY-SA 4.0
2018-05-15T16:07:23.733Z
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
Nate Beaty
CC BY-SA 4.0
2018-05-15T21:09:38.327Z
@natebeaty I suggest you to use a proper IDE. Most of them handle those issues nearly on their own.
Robin Schambach
CC BY-SA 4.0
2018-05-16T22:21:19.357Z
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.
Nate Beaty
CC BY-SA 4.0
2018-07-09T20:42:02.637Z
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.
Dan Zuzevich
CC BY-SA 4.0
2018-07-09T21:10:28.147Z
I put in a request for edit, but I actually capitalized Craft\Mail when its supposed to be lowercase in the use statement.
Dan Zuzevich
CC BY-SA 4.0
2019-05-31T09:54:55.033Z
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. :)
bzin
CC BY-SA 4.0
2019-05-31T09:58:52.207Z
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
Robin Schambach
CC BY-SA 4.0
2018-02-12T14:30:27.193Z
2019-02-23T18:19:32.947Z
2020-06-17T08:56:27.903Z
Robin Schambach
<p>I have created custom plugin in Craft 3 and want send email to receiver or site email when user submits a form.</p>

CC BY-SA 4.0
2019-01-08T07:03:45.637Z
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
Shivam Kumar
CC BY-SA 4.0
2019-01-08T07:21:07.360Z
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.
Lindsey D
<emails>
How to send email from Craft 3 custom plugin?
# 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 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.  

---

## 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
/**
 * @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**: @natebeaty 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 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. :)  
> **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  

---

