The script does not work to send letters. Help me figure out what could be the problem. When you press a button, nothing happens

<!-- Form --> <form method="post" action="contact.php" name="contactform" id="contactform"> <fieldset> <div> <label for="name" accesskey="N">Как к Вам обращаться: </label> <input name="name" type="text" id="name" /> </div> <div> <label for="email" accesskey="E">Email: <span>*</span></label> <input name="email" type="email" id="email" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" /> </div> <div> <label for="comments" accesskey="M">Сообщение: <span>*</span></label> <textarea name="comments" cols="40" rows="3" id="comments" spellcheck="true"></textarea> </div> </fieldset> <input type="submit" class="submit" id="submit" value="Отправить сообщение" /> </form> 

Script to send letters

  <?php // Проверка на заполненность if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) { echo "Заполните отмеченные поля"; return false; } $name = $_POST['name']; $email_address = $_POST['email']; $message = $_POST['comments']; // Создаем почту и отправляем сообщение $to = 'msam15@mail.ru'; // Адрес получателя $email_subject = "Посылка с контактной формы"; // Тема письма $email_body = "Вы получили новое письмо с веб-сайта, отправленное через контактную форму.\n\n"."Детали сообщения:\n\n Имя: $name\n\n Email: $email_address\n\n Сообщение:\n $message"; $headers = "Ответить на сообщение по адресу, указанному в контактной форме: $email_address"; $send = mail($to,$email_subject,$email_body,$headers); return true; if ($send == 'true') { echo ' <div class="notification error closeable" style="display: block; float: right;"> <p><span>Ошибка!</span> Ваше сообщение не отправлено</p> </div> <div class="clearfix"></div>'; } else { echo ' <div class="notification success closeable" style="display: block; float: right;"> <p><span>Спасибо!</span> Ваша сообщение успешно отправлено</p> </div> <div class="clearfix"></div>'; } ?> 
  • The button is working, but the letter is not sent, the mail function does not work - m_vav

1 answer 1

First, you didn’t read the manual for the mail () function badly. Your $headers is a string with text, and you need to pass the string additional_headers to the function. See at least an example in the manual. (see example at the end)

Secondly, using return in the global scope will stop the script from executing. Documentation . So you will never see your notice.

Thirdly, sending a letter is a big and complicated question. If you use the mail () function, then you need to take into account that php does not send emails, but sends it to your mail server. Most likely it is POSTFIX . Need to look at the settings and logs.

And in the fourth letter could be sent, but not delivered. Again, look in the logs and in the spam in the mailbox. It could be blacklisted or spammed. Do not forget about DKIM and SPF

It is better to send mail via SMTP server. For example Yandex or Google. For php there is a library Swiftmailer

Ps. Here is an example of working code:

 $charset = 'utf-8'; // Кодировка письма $to = ""; // Получатель $subject = ""; // Тема письма $text = ""; // Контент письма $from = ""; // Отправитель $fromName = ""; // Имя отправителя // Вот что такое заголовки $headers = "MIME-Version: 1.0\n"; $headers .= "From: =?$charset?B?".base64_encode($fromName)."?= <$from>\n"; $headers .= "Content-type: text/html; charset=$charset\n"; $headers .= "Content-Transfer-Encoding: base64\n"; return mail("=?$charset?B?".base64_encode($to)."?= <$to>", "=?$charset?B?".base64_encode($subject)."?=", chunk_split(base64_encode($text)), $headers, "-f$from"); 
  • Manual about the mail() function, then it can be read just well, I could see. - nick