diff --git a/php/cl_contactform_output.php b/php/cl_contactform_output.php index 89491e4..5560765 100644 --- a/php/cl_contactform_output.php +++ b/php/cl_contactform_output.php @@ -1,4 +1,8 @@ '', 'from' => ['name' => '', 'email' => ''], 'message' => '', 'subject' => '']; private array $unraveled = []; private array $form = ['form' => '', 'answer' => '', 'colorError' => '']; - public function __construct(private bool $captchaInstalled) + public function __construct(private bool $captchaInstalled, private cSession $cSession) { } @@ -35,18 +40,22 @@ class w3form $this->unravel($this->suppress('sent')); $this->generateEmailMessage(); - $oMailer = new PHPMailer(); - $oMailer->CharSet = "UTF-8"; - $oMailer->AddAddress($this->email['adresses']); - $oMailer->From = $this->email['from']['email']; - $oMailer->FromName = $this->email['from']['name']; - $oMailer->Subject = $this->email['subject']; - $oMailer->Body = $this->email['message']; + $phpMailer = new PHPMailer(true); + $phpMailer->isMail(); + $phpMailer->isHTML(false); + $phpMailer->CharSet = "UTF-8"; + $phpMailer->AddAddress($this->email['adresses']); + $phpMailer->setFrom($this->email['from']['email'], $this->email['from']['name']); + $phpMailer->Subject = $this->email['subject']; + $phpMailer->Body = $this->email['message']; - if ($oMailer->send()) { - return true; + try { + if ($phpMailer->send()) { + return true; + } + } catch (Exception $e) { + echo $e->errorMessage(); } - return false; } @@ -63,7 +72,7 @@ class w3form private function add2Message($key, $value): void { - if (strlen($key) > 25 or strlen($value) > 54) { + if (strlen($key) > 25 || strlen($value) > 54) { $this->email['message'] .= "$key\n$value\n"; } else { $this->email['message'] .= $key; @@ -74,18 +83,16 @@ class w3form private function generateEmailMessage(): void { - if ($this->unraveled) - foreach ($this->unraveled as $key => $value) { - $this->add2Message($key, $value); - } + foreach ($this->unraveled as $key => $value) { + $this->add2Message($key, $value); + } } - private function suppress(): array + private function suppress(...$suppress): array { $fields = []; - $suppress = func_get_args(); foreach ($_POST as $key => $value) { - if (array_search($key, $suppress) === false) + if (!in_array($key, $suppress)) $fields[$key] = $value; } return $fields; @@ -130,8 +137,9 @@ class w3form { $fields = explode('###', $form); $field = []; + $fieldsCount = count($fields); - for ($i = 1; $i < count($fields); $i = $i + 2) { + for ($i = 1; $i < $fieldsCount; $i += 2) { $attribute = explode(';', trim($fields[$i])); foreach ($attribute as $attribute) { $nameValue = explode(':', trim($attribute)); @@ -148,7 +156,8 @@ class w3form public function formOutput($sent = false): void { - echo '