<?php
if (isset($_POST['email_address'])) { // Проверка, указан ли адрес для отправки
$sender = trim($_POST['email_address']);
if (!empty($sender)) {
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $sender)) {
$error_message .= "The email address is invalid.<br />";
}
} else {
$error_message .= "Email address is a required field.<br/>";
}
} else {
$error_message .= "Form error, Email address is required.<br/>";
error_log("Email field missing in contact.php!",0);
}
// If no errors send email.
if (!isset($error_message)) {
// Loop through form fields and create mailto.
$email = "You have a new enquiry\r\n\r\n";
foreach ($_POST as $key => $value) {
$field_name = ucwords(str_replace("_", " ", $key));
$field_value = ucwords($value);
$email .= "$field_name = $field_value\r\n";
}
$to = 'mail@mail.com'; //Адрес куда отправляем
$subject = 'Тема письма'; // Тема письма
$message = $email;
$headers = "From: $sender" . "\r\n" .
"Reply-To: $sender" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header('Location: /thank-you-for-your-enquiry/'); // Перенаправление после отправки
} else {
header('Location: /Contact-error/'); // Перенаправление в случае ошибки
}
?>
Отправка почты с сайта на PHP mail()
|
PHP