W
W
WieFix2013-12-15 17:52:10
PHP
WieFix, 2013-12-15 17:52:10

Why is the email not being sent? (php)

I welcome everyone! Not strong in PHP. You need to make sure that the script sends a letter to email.
There is an index.html file and in it the submit form looks like this:

<form action="mail.php" method="post" id="main_form">
<input name="title" type="text" placeholder="ИМЯ" id="main_name">
<input name="phone" type="text" placeholder="ТЕЛЕФОН" id="main_telephone">
<input name="mess" type="text" placeholder="E-MAIL" id="main_email">
<input type="submit" value="ОТПРАВИТЬ" class="ex_hover">
</form>

And there is a separate mail.php file
<?php 
// если была нажата кнопка "Отправить" 
if($_POST['submit']) {
$title = substr(htmlspecialchars(trim($_POST['title'])), 0, 1000); 
$mess = substr(htmlspecialchars(trim($_POST['mess'])), 0, 1000000); 
$phone = substr(htmlspecialchars(trim($_POST['phone'])), 0, 1000000); 
// $to - кому отправляем 
$to = '[email protected]'; 
// $from - от кого 
$from='[email protected]'; 
// функция, которая отправляет наше письмо
mail($to, $title, $mess, 'From:'.$from); 
echo 'Спасибо! Ваше письмо отправлено.'; 
} 

?>

I need that after a person left a request, it went to my mail

Answer the question

In order to leave comments, you need to log in

7 answer(s)
A
Alexey Firsov, 2013-12-16
@lesha_firs

Are you testing locally by any chance? well, opneServer or Denver? if so, look for letters in the !send or tmp folders!

A
Alexander Zelenin, 2013-12-15
@zelenin

if($_POST['submit']) {
I don't see an input named submit in the form

N
Nikolai Alexandrov, 2013-12-15
@dos

So that the mail(); worked, you should at least have an SMTP service installed on the server, and a reverse zone configured. If this is difficult for you, then try this option:
1. Download: swiftmailer.org/download
2. Write a script that will send mail through your GMAIL account

<?php
// если была нажата кнопка "Отправить" 
if($_POST['submit']) {
$title = substr(htmlspecialchars(trim($_POST['title'])), 0, 1000); 
$mess = substr(htmlspecialchars(trim($_POST['mess'])), 0, 1000000); 
$phone = substr(htmlspecialchars(trim($_POST['phone'])), 0, 1000000); 
// $to - кому отправляем 
$to = '[email protected]'; 
// $from - от кого 
$from='[email protected]'; 
// функция, которая отправляет наше письмо

// подключаем swift_required.php который вы скачала и п.1
require_once '/path/to/swift-mailer/lib/swift_required.php';

// создаем письмо
$message = Swift_Message::newInstance()
  ->setSubject($title)
  ->setFrom(array($from))
  ->setTo(array($to))
  ->setBody($mess);

// настраиваем подключение к gmail
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  ->setUsername('[email protected]')
  ->setPassword('password');

// отправляем
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

N
Nazar Mokrinsky, 2013-12-15
@nazarpc

An even simpler email can be sent via PHPMailer.
Yes, it is better to use authorization so that the letter is more likely not to fall into spam.

V
Valera Udav, 2013-12-15
@TARAKANhoy

Try this code for mail.php

<?php
//Принимаем значения формы, обрабатываем и заносим в переменные. Эту часть кода в самое начало файла, до DOCTYPE
if (!empty($_POST['title'])) $name = substr(htmlspecialchars(trim($_POST['title'])), 0, 1000);
if (!empty($_POST['phone'])) $phone = substr(htmlspecialchars(trim($_POST['phone'])), 0, 20);
if (!empty($_POST['mess'])) $text = substr(htmlspecialchars(trim($_POST['mess'])), 0, 1000000);
?>

<?php
//Формируем письмо. Эту часть кода вставить в любое место между body
if(isset($name,$phone,$text)) {
  $subject = "Форма обратной связи"; //тема сообщения
  $message = "Имя: ".$name."<br>Телефон: ".$phone."<br>Текст: ".$text."<br>"; //содержание сообщение
  $emailto = "[email protected]"; //e-mail кому
  $emailfrom = "[email protected]"; //e-mail от кого
  $chek = mail($emailto, $subject, $message, "Content-type:text/html; Charset=utf-8\r\nFrom:".$emailfrom."\r\n"); //отправляем сообщение
  if($chek) echo "Ваше письмо успешно отправлено!";
  else echo "Ваше письмо не отправлено!";
}
else {
  echo "Вы заполнили не все поля!";
}
?>

ps The problem with your code not working was that you did not pass the submit value and the if($_POST['submit']) check did not pass. My version of sending a letter is better, but it can be modified for special cases, but in 90% of the case it will work as it should. I myself use this one.

I
InDiGo2010, 2013-12-15
@InDiGo2010

maybe this is a better option?

$headers = "From: [email protected] <[email protected]>\r\n"; 
$headers .= "Content-type: text/html; charset=utf-8";


$title = substr(htmlspecialchars(trim($_POST['title'])), 0, 1000); 
$mess = substr(htmlspecialchars(trim($_POST['mess'])), 0, 1000000); 
$phone = substr(htmlspecialchars(trim($_POST['phone'])), 0, 1000000);

if(empty($title))
die("Отсутствует заголовок");

if(empty($mess))
die("Отсутствует сообщение");

if(empty($phone))
die("Отсутствует телефон");


$msg = $mess."<br />".$phone;

$to = '[email protected]';

if(mail($to, $title, $msg, $headers)) {
die("Успешно");
} else {
die("Сообщение не отправлено");
}

despite the fact that this option supports html tags in emails.

K
KLFWoot, 2017-05-16
@KLFWoot

The problem is in the submit code
. The plugin submits on behalf of the user filling out the form.
The problem is solved, described here
Sends both by standard means and with the smtp plugin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question