mail.php:
<?php
$method = $_SERVER['REQUEST_METHOD'];
// Initialize variables with empty values
$project_name = "";
$admin_email = "";
$form_subject = "";
$message = "";
if ($method === 'POST') {
// Process POST request
$project_name = isset($_POST["project_name"]) ? trim($_POST["project_name"]) : "";
$admin_email = isset($_POST["admin_email"]) ? trim($_POST["admin_email"]) : "";
$form_subject = isset($_POST["form_subject"]) ? trim($_POST["form_subject"]) : "";
// Prepare message content
$message = "<table style='width: 100%;'>";
$c = true; // For alternating row colors
foreach ($_POST as $key => $value) {
if ($value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject") {
$message .= "
" . (($c = !$c) ? '<tr>' : '<tr style="background-color: #f8f8f8;">') . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
</tr>
";
}
}
$message .= "</table>";
} elseif ($method === 'GET') {
// Process GET request
$project_name = isset($_GET["project_name"]) ? trim($_GET["project_name"]) : "";
$admin_email = isset($_GET["admin_email"]) ? trim($_GET["admin_email"]) : "";
$form_subject = isset($_GET["form_subject"]) ? trim($_GET["form_subject"]) : "";
// Prepare message content
$message = "<table style='width: 100%;'>";
$c = true; // For alternating row colors
foreach ($_GET as $key => $value) {
if ($value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject") {
$message .= "
" . (($c = !$c) ? '<tr>' : '<tr style="background-color: #f8f8f8;">') . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
</tr>
";
}
}
$message .= "</table>";
}
// Function to adopt text for email headers
function adopt($text) {
return '=?UTF-8?B?' . base64_encode($text) . '?=';
}
// Validate and adopt project name and admin email for headers
$project_name_header = $project_name ? adopt($project_name) : "No Name";
$admin_email_header = $admin_email ? $admin_email : "no-reply@example.com";
// Email headers
$headers = "MIME-Version: 1.0" . PHP_EOL .
"Content-Type: text/html; charset=utf-8" . PHP_EOL .
'From: ' . $project_name_header . ' <' . $admin_email_header . '>' . PHP_EOL .
'Reply-To: ' . $admin_email_header . '' . PHP_EOL;
// Send email
if (mail($admin_email, adopt($form_subject), $message, $headers)) {
echo 'Письмо успешно отправлено.';
} else {
echo 'Ошибка отправки письма.';
}
?>
js:
$(document).ready(function() {
$("form").submit(function(event) {
event.preventDefault();
var th = $(this);
$.ajax({
type: "POST",
url: "/mail.php",
data: th.serialize()
}).done(function(response) {
alert("Спасибо! Мы с Вами свяжемся");
console.log("Response: ", response); // Debugging output
setTimeout(function() {
th.trigger("reset");
}, 1000);
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Ошибка при отправке сообщения");
console.log("Error: ", textStatus, errorThrown); // Debugging output
});
});
});
index.html:
<form>
<!-- Hidden Required Fields -->
<input type="hidden" name="project_name" value="ImproStore">
<input type="hidden" name="admin_email" value="samarov_dubskoe@mail.ru">
<input type="hidden" name="form_subject" value="Form Subject">
<!-- END Hidden Required Fields -->
<input class="okno" type="text" name="Name" placeholder="Как Вас зовут?" required><br>
<input class="okno" type="text" name="E-mail" placeholder="Ваша почта" required><br>
<input class="okno" type="text" name="Phone" placeholder="Ваш номер телефона"><br>
<button class="okno-btn">Отправить</button>
</form>
Проблема в том, что я получаю не 3 строчки: имя, номер, почта, а вот такой кусок кода:
<table style='width: 100%;'></table>
<tr style="background-color: #f8f8f8;">
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>Name</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>123213</td>
</tr>
<tr>
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>E-mail</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>123123</td>
</tr>
<tr style="background-color: #f8f8f8;">
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>Phone</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>123123</td>
</tr>
Тут конечно есть и имя и номер и почта, но не очень удобно их выковыривать отсюда.