Skip to content

Instantly share code, notes, and snippets.

@owldesign
Created September 27, 2018 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owldesign/864c0127e18918ad64402744080af4ec to your computer and use it in GitHub Desktop.
Save owldesign/864c0127e18918ad64402744080af4ec to your computer and use it in GitHub Desktop.
Craft CMS 3 Sending Mail through a plugin
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment