There is a standard form :

<form> <input type="text" name="name" id="name"> <input type="text" name="email" id="email"> </form> 

ajax request :

 var str = form.serialize(); $.ajax({ url: 'contacts.php', type: 'post', data: str }).done(function(msg) { if (msg === "OK") { console.log('сообщение отправлено'); $(".res").html(res); } else { $(".res").html(msg); } }).always(function() { ... }); }); 

And there is contacts.php itself:

 <?php define("CONTACT_FORM", 'myEmail@gmail.com'); $subject = 'Заявка'; $name = stripslashes($_POST['name']); $phone = stripslashes($_POST['phone']); $email = stripslashes($_POST['email']); $msg = stripslashes($_POST['msg']); $error = ''; $message = ' <html> <head> <title>Заявка</title> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> </head> <body> <p>Имя : '.$name.'</p> <p>Email : '.$email.'</p> <p>Phone : '.$phone.'</p> <p>Message : '.$msg.'</p> </body> </html>'; $mail = mail(CONTACT_FORM, $subject, $message, "MIME-Version: 1.0\r\n" ."From: ".$name." <".$email.">\r\n" ."Reply-To: ".$email."\r\n" ."Content-type: text/html; charset=UTF-8\r\n" ."X-Mailer: PHP/" . phpversion()); if($mail){ echo "OK"; } ?> 

where 'myEmail@gmail.com' is the address where letters should come.

No errors are written that everything is perfectly sent (js works "OK" from php comes in any case).

If myEmail@gmail.com entered in the form field, then letters arrive, and if Vasya@mail.ru (or any other mailing address) is entered in the form field - letters simply do not come.

Question: why when filling in the form and sending, the letter comes to the mail only if the email address in the form ( <input type="text" name="email" id="email" > ) matches define("CONTACT_FORM", 'myEmail@gmail.com'); ? How can I fix .php?

  • show the form code and how you form the value of str . And what do the logs say? - Alex
  • added form to question, str . No errors there is no writes that everything is perfectly sent. Letters from myEmail@gmail.com to myEmail@gmail.com come, and from Vasya@mail.ru to myEmail@gmail.com simply do not come. - HamSter
  • Some kind of hell ( - HamSter
  • one
    and why they should come from Vasya@mail.ru usually letters from the site are sent either from the server php. either from a mail address but from 1 address such as request@site.ru, in order to send letters from any email address, it seems like you need to connect to the mail server that serves this address - Broouzer King
  • one
    Replace ."From: ".$name." <".$email.">\r\n" with ."From: ".$name." <".CONTACT_FORM.">\r\n" and you will happiness. - Visman

2 answers 2

Why Google (and perhaps not he) misses letters from third-party servers with the sender equal to the recipient, I do not know.

Solution from my comment:

Replace line of code

 ."From: ".$name." <".$email.">\r\n" 

on

 ."From: ".$name." <".CONTACT_FORM.">\r\n" 

so that the sender is always equal to the receiver

PS The answer to such letters is generated by the Answer button in the mail client. At the same time, the data from the Reply-To: header is put in the recipient's address.

  • Thanks, it helped!) - HamSter

I usually use this design to send a message

 $field_email = 'request@site.ru';// откуда будут слаться письма $mail_to = 'куда отправлять (в вашем случае myEmail@gmail.com)'; $subject = 'тема письма'; $body_message = ' <html> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <body> <p> Имя: '.$name.'<br> Email: '.$email.'<br> Номер телефона: '.$phone.'<br> Текст: '.$summary.' </p> </body> </html>'; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=UTF-8\r\n"; $headers .= 'From: '.$field_email."\r\n"; $headers .= 'Reply-To: '.$field_email."\r\n"; $mail_status=mail($mail_to, $subject, $body_message, $headers); 
  • still the same problem ( - HamSter