I
I
Ivan2015-10-13 10:59:06
PHP
Ivan, 2015-10-13 10:59:06

Why does the script for sending an email from the site not work?

I needed a script for a form for sending letters from the site, I found an article on Habré with a working code, I decided to test it on the hosting (test.php normally sent a letter from the hosting), launched the form, but the letter does not come, what could be the problem?
HTML

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <title>Небольшая тестовая страница</title>
    <link href="css/bootstrap.min.css" rel="stylesheet"> 
  <link rel="stylesheet" href="css/styles.css" type="text/css">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300" type="text/css">
  
  <script type="text/javascript">
$(document).ready(function(){
    $("#form").submit(function() { //устанавливаем событие отправки для формы с id=form
            var form_data = $(this).serialize(); //собераем все данные из формы
            $.ajax({
            type: "POST", //Метод отправки
            url: "test.php", //путь до php фаила отправителя
            data: form_data,
            success: function() {
                   //код в этом блоке выполняется при успешной отправке сообщения
                   alert("Ваше сообщение отпрвлено!");
            });
    });
});    
</script>

  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>
<body>
    ХАО!
  <form id="form">
    <input type="text" name="name" required="required"/>
    <input type="text" name="phone" required="required"/>
    <input type="submit" value="Заказать звонок"/>
</form>


</body>
</html>

PHP
<?
if((isset($_POST['name'])&&$_POST['name']!="")&&(isset($_POST['phone'])&&$_POST['phone']!="")){ //Проверка отправилось ли наше поля name и не пустые ли они
        $to = '[email protected]'; //Почта получателя, через запятую можно указать сколько угодно адресов
        $subject = 'Обратный звонок'; //Загаловок сообщения
        $message = '
                <html>
                    <head>
                        <title>'.$subject.'</title>
                    </head>
                    <body>
                        <p>Имя: '.$_POST['name'].'</p>
                        <p>Телефон: '.$_POST['phone'].'</p>                        
                    </body>
                </html>'; //Текст нащего сообщения можно использовать HTML теги
        $headers  = "Content-type: text/html; charset=utf-8 \r\n"; //Кодировка письма
        $headers .= "From: Отправитель <[email protected]>\r\n"; //Наименование и почта отправителя
        $mail($to, $subject, $message, $headers); //Отправка письма с помощью функции mail
}
?>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
facir, 2015-10-13
@facir

just try to create one file for example send.php and write there

<?
mail('[email protected]', 'Тема', 'Сообщение');
?>

run it and look at the result, I think you will immediately find an error.
if everything is sent, then the variables are incorrect somewhere, if it doesn’t go, then hotting does not support, which is hardly
just checked, fully working
$to = "[email protected]";
        $subject = "Обратный звонок";
        $message = "<html><head><title>'.$subject.'</title></head>";
        $message .= "<body><p>Имя: ".$_POST['name']."</p><p>Телефон: ".$_POST['phone']."</p></body></html>";
        $headers  = "Content-type: text/html; charset=utf-8 \r\n";
        $headers .= "From: Отправитель <[email protected]>\r\n";
        mail($to, $subject, $message, $headers);

L
Light777, 2015-10-13
@Light777

This may be due to the hosting, some hosting do not send. try using phpmailer, via smtp
Test it on your local, if the script works then the letter should go to the mailoutput folder in XAMPP

I
Ivan, 2015-10-13
@W2nn3ss

So is the code correct?
I somehow sin on him, since the usual test.php with the code

<?
   mail("[email protected]", "Имя", "Телефон");
?>

normally sent a letter from the hosting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question