12

I have created custom plugin in Craft 3 and want send email to receiver or site email when user submits a form.

2
  • 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 Jan 8, 2019 at 7:03
  • 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. Feel free to link back to this thread for reference.
    – Lindsey D
    Jan 8, 2019 at 7:21

1 Answer 1

22

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);
}
7
  • 3
    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 docs.craftcms.com/api/v3/craft-mail-message.html
    – Nate Beaty
    May 15, 2018 at 16:07
  • 1
    @natebeaty I suggest you to use a proper IDE. Most of them handle those issues nearly on their own. May 15, 2018 at 21:09
  • 13
    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
    May 16, 2018 at 22:21
  • 1
    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. Jul 9, 2018 at 20:42
  • I put in a request for edit, but I actually capitalized Craft\Mail when its supposed to be lowercase in the use statement. Jul 9, 2018 at 21:10

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.