Created
September 27, 2018 15:55
-
-
Save owldesign/864c0127e18918ad64402744080af4ec to your computer and use it in GitHub Desktop.
Craft CMS 3 Sending Mail through a plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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