Функция почты PHP не правильно настроена из заголовка

У меня есть функция php mail, которая отправляет электронные письма с контентом из формы контакта. Но когда я отправляю это письмо на своем веб-сайте. вместо того, чтобы из заголовка был введен адрес электронной почты, это username@srv30.000webhost.com, и я не знаю почему. моя почтовая функция php ниже.

$ email – это адрес электронной почты, введенный пользователем.

EDIT: Subject: Комментарий от hello X-PHP-Script: kwp.host22.com/form_action.php для 78.147.187.64 Message-Id: <20131124204151.478EC28021@srv30.000webhost.com> Дата: Вс, 24 Ноя 2013 15:41 : 51 -0500 (EST) От: a5485011@srv30.000webhost.com Возврат: a5485011@srv30.000webhost.com X-OriginalArrivalTime: 24 ноября 2013 20: 41: 52.0292 (UTC) FILETIME = [9B09B640: 01CEE955]

полный скрипт php:

$name = ($_GET['Name']) ? $_GET['Name'] : $_POST['Name']; $email = ($_GET['Email']) ?$_GET['Email'] : $_POST['Email']; $phone = ($_GET['Phone']) ?$_GET['Phone'] : $_POST['Phone']; $comment = ($_GET['Comment']) ?$_GET['Comment'] : $_POST['Comment']; // flag to indicate which method it uses. If POST set it to 1 if ($_POST) $post=1; // Simple server side validation for POST data, of course, you should validate the email if (!$name) $errors[count($errors)] = 'Please enter your name.'; if (!$email) $errors[count($errors)] = 'Please enter your email.'; if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; // if the errors array is empty, send the mail if (!$errors) { // recipient $to = '<example@example.com>'; // sender $from = $name . ' <' . $email . '>'; // subject and the html message $subject = 'Comment from ' . $name; $message = ' <!DOCTYPE html> <html> <head></head> <body> <table> <tr><td>Name</td><td>' . $name . '</td></tr> <tr><td>Email</td><td>' . $email . '</td></tr> <tr><td>Phone</td><td>' . $phone . '</td></tr> <tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr> </table> </body> </html>'; // send the mail $result = @mail($to, $subject, $message, $from); // if POST was used, display the message straight away if ($_POST) { if ($result) echo 'Thank you! We have received your message.'; else echo 'Sorry, unexpected error. Please try again later'; // else if GET was used, return the boolean value so that // ajax script can react accordingly // 1 means success, 0 means failed } else { echo $result; } // if the errors array has values } else { // display the errors message for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>'; echo '<a href="contact.php">Back</a>'; exit;} function sendmail($to, $subject, $message, $from) { $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: ' . $email . "\r\n"; $headers .= 'Reply-To: ' .$email . "\r\n"; $headers .= 'X-Mailer: PHP/' . phpversion(); @mail($to, $subject, $message, $headers); if ($result) return 1; else return 0;} 

im совершенно новый для PHP, поэтому любая помощь будет оценена. Спасибо

Я считаю, что эта строка

 $result = @mail($to, $subject, $message, $from); 

Следует зафиксировать это

 $result = sendmail($to, $subject, $message, $from); 

И измените вашу функцию sendmail на это.

 function sendmail($to, $subject, $message, $from) { $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: ' . $from . "\r\n"; $headers .= 'Reply-To: ' .$from . "\r\n"; $headers .= 'X-Mailer: PHP/' . phpversion(); @mail($to, $subject, $message, $headers); if ($result) return 1; else return 0; } 

Здесь вы используете $ email, но, возможно, вам стоит использовать $ from. Ваша функция принимает $ to, $ subject, $ message, $ from.

Также вы должны немного изменить свой код

 function sendmail($to, $subject, $message, $from) { $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: ' . $from . "\r\n"; $headers .= 'Reply-To: ' .$from . "\r\n"; $headers .= 'X-Mailer: PHP/' . phpversion(); if(mail($to, $subject, $message, $headers)){ return 1; } return 0; } 

sendmail – это двоичный код приложения unix, используемый для отправки электронных писем. Написание собственной функции sendmail в PHP ничего не делает. Функция PHP mail использует sendmail в Linux по умолчанию, я полагаю, и в Windows вам нужно настроить SMTP. Но поскольку ваши электронные письма отправляются, это не проблема. Возможно, вам следует переименовать функцию, чтобы вы не смешивали эти вещи случайно.

Также здесь весь код переписан, может быть, вы можете чему-то научиться 🙂

 $post = false; // flag to indicate which method it uses. If POST set it to 1 if (isset($_POST['Name'])) { $post = true; } $name = (isset($_GET['Name'])) ? $_GET['Name'] : $_POST['Name']; $email = (isset($_GET['Email'])) ? $_GET['Email'] : $_POST['Email']; $phone = (isset($_GET['Phone'])) ? $_GET['Phone'] : $_POST['Phone']; $comment = (isset($_GET['Comment'])) ? $_GET['Comment'] : $_POST['Comment']; // Simple server side validation for POST data, of course, you should validate the email if (!$name) { $errors[] = 'Please enter your name.'; } if (!$email) { $errors[] = 'Please enter your email.'; } if (!$comment) { $errors[] = 'Please enter your comment.'; } // if the errors array is empty, send the mail if (count($errors) == 0) { // recipient $to = 'example@example.com'; // sender $from = $name . ' <' . $email . '>'; // subject and the html message $subject = 'Comment from ' . $name; $message = '<!DOCTYPE html> <html> <head></head> <body> <table> <tr><td>Name</td><td>' . $name . '</td></tr> <tr><td>Email</td><td>' . $email . '</td></tr> <tr><td>Phone</td><td>' . $phone . '</td></tr> <tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr> </table> </body> </html>'; $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; $headers .= 'From: ' . $from . "\r\n"; $headers .= 'Reply-To: ' . $from . "\r\n"; $headers .= 'X-Mailer: PHP/' . phpversion(); $mailSuccess = mail($to, $subject, $message, $headers); // if POST was used, display the message straight away if ($post) { if ($mailSuccess) { echo 'Thank you! We have received your message.'; } else { echo 'Sorry, unexpected error. Please try again later'; } // else if GET was used, return the boolean value so that // ajax script can react accordingly // 1 means success, 0 means failed } else { echo (int)$mailSuccess; } // if the errors array has values } else { // display the errors message foreach ($errors as $error) { echo $error . '<br/>'; } echo '<a href="contact.php">Back</a>'; }