diff --git a/src/Module.php b/src/Module.php index a2e5b70..487f37b 100644 --- a/src/Module.php +++ b/src/Module.php @@ -8,6 +8,7 @@ * @property string $defaultRoute * @property string $frontendLayout * @property string $backendLayout + * @property bool $sendHtmlMails */ class Module extends \yii\base\Module { @@ -19,6 +20,7 @@ class Module extends \yii\base\Module public $defaultRoute = 'backend'; public $frontendLayout = '//main'; public $backendLayout = '@backend/views/layouts/box'; + public $sendHtmlMails = false; public function beforeAction($action) { diff --git a/src/models/ContactLog.php b/src/models/ContactLog.php index 4b6f951..180d103 100644 --- a/src/models/ContactLog.php +++ b/src/models/ContactLog.php @@ -18,6 +18,8 @@ class ContactLog extends BaseContactLog { + public static $moduleId = 'contact'; + public function behaviors() { $behaviors = parent::behaviors(); @@ -64,6 +66,9 @@ public function sendMessage() $message->setTo($to); $message->setSubject($this->emailSubject); $message->setTextBody($this->dataValue2txt(Json::decode($this->json))); + if (Yii::$app->getModule(self::$moduleId)->sendHtmlMails) { + $message->setHtmlBody($this->dataValue2Html(Json::decode($this->json))); + } return $message->send(); } @@ -94,11 +99,41 @@ private function dataValue2txt($data, $level = 0) } else { $valueText = trim($value); } - $text .= $prefix . trim($key) . ': ' . $valueText . "\n"; + $text .= $prefix . trim($this->labelFromAttribute($key)) . ': ' . $valueText . "\n"; } return $text; } + + /** + * @param array $data + * @return void + */ + private function dataValue2Html(array $data = []): string + { + if (!is_array($data)) { + return ''; + } + $rows = ''; + foreach ($data as $attribute => $value) { + if (is_array($value)) { + $row = $this->dataValue2Html($value); + } else { + $row = '' . $this->labelFromAttribute($attribute) . '' . $value . ''; + } + $rows .= $row; + } + return '' . $rows . '
'; + } + + private static $_schemaCache = []; + private function labelFromAttribute(string $attribute) + { + if (empty(static::$_schemaCache)) { + static::$_schemaCache = Json::decode($this->contactTemplate->form_schema); + } + return static::$_schemaCache['properties'][$attribute]['title'] ?? $attribute; + } }