public function mail() {
$this->load->language('extension/module/form2304');
$this->load->model('setting/module');
$settings = $this->model_setting_module->getModule($this->request->post['code']);
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate($settings)) {
$recivers = explode(',', $settings['recivers']);
$this->load->model('account/custom_field');
$custom_fields = $this->getCustomFields($settings);
$text = '';
$attachments = [];
$json_data = []; // Массив для данных, которые попадут в JSON
foreach ($custom_fields as $custom_field) {
if (!empty($this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']])) {
$text .= $custom_field['name'] . ": ";
if ($custom_field['type'] == 'select') {
foreach ($custom_field['custom_field_value'] as $custom_field_value) {
if ($custom_field_value['custom_field_value_id'] == $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']]) {
$text .= $custom_field_value['name'] . "\n";
}
}
}
if ($custom_field['type'] == 'radio') {
foreach ($custom_field['custom_field_value'] as $custom_field_value) {
if ($custom_field_value['custom_field_value_id'] == $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']]) {
$text .= $custom_field_value['name'] . "\n";
}
}
}
$cb_text = '';
if ($custom_field['type'] == 'checkbox') {
foreach ($custom_field['custom_field_value'] as $custom_field_value) {
if (in_array($custom_field_value['custom_field_value_id'], $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']])) {
$cb_text .= ($cb_text == '' ? '' : '; ') . $custom_field_value['name'];
}
}
}
$text .= ($cb_text == '' ? '' : $cb_text . "\n");
if ($custom_field['type'] == 'text') {
$text .= $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']] . "\n";
}
if ($custom_field['type'] == 'textarea') {
$text .= $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']] . "\n";
}
if ($custom_field['type'] == 'file') {
$this->load->model('tool/upload');
$upload_info = $this->model_tool_upload->getUploadByCode($this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']]);
if (file_exists(DIR_UPLOAD . $upload_info['filename'])) {
// Формируем новое имя файла по шаблону: исходное_имя.md5_хеш
$file_hash = md5_file(DIR_UPLOAD . $upload_info['filename']);
$new_filename = $upload_info['name'] . '.' . $file_hash;
// Переименовываем файл
rename(DIR_UPLOAD . $upload_info['filename'], DIR_UPLOAD . $new_filename);
// Сохраняем путь к файлу для вложения в письмо
$attachments[] = DIR_UPLOAD . $new_filename;
// Добавляем информацию о файле в текст письма
$text .= $new_filename . "\n";
// Сохраняем данные о файле для JSON
$json_data['files'][] = [
'original_name' => $upload_info['name'],
'stored_name' => $new_filename,
'path' => DIR_UPLOAD . $new_filename,
'size' => filesize(DIR_UPLOAD . $new_filename),
'mime' => mime_content_type(DIR_UPLOAD . $new_filename)
];
}
}
if ($custom_field['type'] == 'date') {
$text .= $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']] . "\n";
}
if ($custom_field['type'] == 'time') {
$text .= $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']] . "\n";
}
if ($custom_field['type'] == 'datetime') {
$text .= $this->request->post['custom_field'][$this->request->post['code']][$custom_field['custom_field_id']] . "\n";
}
$text .= "\n";
}
}
$text .= $this->language->get('post_page') . ": " . htmlspecialchars_decode($this->request->post['post_page']);
// Добавляем общие данные в JSON
$json_data['post_page'] = $this->request->post['post_page'];
$json_data['timestamp'] = date('Y-m-d H:i:s');
// Сохраняем JSON-файл
$json_filename = 'form_submission_' . time() . '_' . md5(serialize($json_data)) . '.json';
file_put_contents(DIR_STORAGE . 'json/' . $json_filename, json_encode($json_data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
foreach ($recivers as $reciver) {
if ($reciver == '') {
continue;
}
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
$mail->smtp_username = $this->config->get('config_mail_smtp_username');
$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
$mail->smtp_port = $this->config->get('config_mail_smtp_port');
$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
$mail->setTo($reciver);
$mail->setFrom($this->config->get('config_email'));
$mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
$mail->setSubject(html_entity_decode($settings['name'], ENT_QUOTES, 'UTF-8'));
$mail->setText($text);
foreach ($attachments as $attach) {
$mail->addAttachment($attach);
}
$mail->send();
}
$json['success']['message'] = $settings['message'];
if ($settings['redirect']) {
$json['success']['redirect'] = $settings['redirect'];
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
} else {
$json['error'] = $this->error;
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
} // закрывающая скобка метода mail()