12

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

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • 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 Jan 08 '19 at 07: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 08 '19 at 07:21

1 Answers1

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

}

Robin Schambach
  • 19,713
  • 1
  • 19
  • 44
  • 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 https://docs.craftcms.com/api/v3/craft-mail-message.html – Nate Beaty May 15 '18 at 16:07
  • 1
    @natebeaty I suggest you to use a proper IDE. Most of them handle those issues nearly on their own. – Robin Schambach May 15 '18 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 '18 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. – Dan Zuzevich Jul 09 '18 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. – Dan Zuzevich Jul 09 '18 at 21:10
  • 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 May 31 '19 at 09:54
  • 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 May 31 '19 at 09:58