0

Looks like everything should work properly. Where did I go wrong? This is my first question on this resource so please do not judge strictly.

PHP:

if(trim($_POST['p1']) == '') {    
$hasError = true;
} else {
$name = trim ($_POST['p1']);
}


if(trim($_POST['p3']) == '') {
$hasError = true;
} else {
if (function_exists ('stripslashes')) {
$comments = stripslashes (trim($_POST ['p3']));
} else {
$comments = trim($_POST['p3']);
}
}    


if(trim($_POST['p2']) == '')  {
$hasError = true;
} else if (!preg_match("/^([0-9_\.-]+)$/",
trim($_POST['p2']))) {
$hasError = true;} 
else {
$tel = trim($_POST['p2']);
}
//----------------------------------------

if(trim($_POST['p4']) == '')  {
$hasError = true;
} else if (filter_var(trim($_POST['p4']), FILTER_VALIDATE_EMAIL)) {
$email = trim($_POST['p4']);} 
else {
$hasError = true;
}

//----------------------------------------
if(trim($_POST['p3']) == '') {
$hasError = true;
} else {
if (function_exists ('stripslashes')) {
$comments = stripslashes (trim($_POST ['p3']));
} else {
$comments = trim($_POST['p3']);
}
}

if(!isset($hasError))
$emailTo = 'donowan2@gmail.com';
$body = "Name: $name \n\nTelephone: $tel\n Email: $email\n 
\n\nComments:\n $comments";
$Headers = 'From: My Site <'.$emailTo.'>' .
 "\r\n" . 'Reply-To: ' . $email;
 mail($emailTo,  $body, $Headers);
 $emailSent = true;



if (isset($hasError)) {
echo "<script type=\"text/javascript\">";
echo "window.alert (\"Пожалуйста, заполните все поля и введите коректные данные.\")";
echo "</script>";
echo "<script type=\"text/javascript\">";
echo "window.location = \"/\"";
echo "</script>";
}

if($emailSent == true) {
echo "<script type=\"text/javascript\">";
echo "window.alert(\"Письмо успешно отправлено\")";
echo "</script>";

echo "<p><strong>Письмо успешно отправлено</srtong></p>";
echo "<p>Спасибо Вам ";
echo "за использование контактной формы. Ваше письмо успешно отправлено";
echo "и мы вскоре свяжемся с Вами </p>";
}
echo "<a href = \"contacts.html\"> Вернутся на страницу контактов </a>";

echo "<script type=\"text/javascript\">";
echo "window.location = \"/\"";
echo "</script>";

?>

HTML:

 <div style="float: right; width: 42%;"><form novalidate="novalidate" id="contact-form"                       class="zakaz-forma" method="post" action="/sendmail.php" name="">
    <div class="close"></div>
    <div class="contact-main">
                    <div class="contact-field">
                        <div class="contact-left">
                            <p>Efternamn:</p>
                            <p><input type="text" placeholder="Efternamn:" aria-invalid="false" aria-required="true" class="forma2-name" size="40" value="" name="p1"></p>
                            <p>Tel/mobil:</p>
                            <p><input type="tel" placeholder="Tel/mobil:" aria-invalid="false" aria-required="true" class="forma2-mail" size="40" value="" name="p2"> </p>
                            <p>E-post:</p>                          
                            <p><input type="email" placeholder="E-post:" aria-invalid="false" aria-required="true" class="forma2-mail" size="40" value="" name="p4"> </p>   
                        </div>
                    </div>
                    <p>
                    Frågor/Önskemål:<br>
                     <textarea aria-invalid="false" class="forma2-message" rows="10" cols="40" name="p3"></textarea> </p>
                    <a class="button-1" href="#" onclick="document.getElementById('contact-form').submit();">SKICKA</a>
                </div>
                <div class="wpcf7-response-output wpcf7-display-none"></div>
        </form>
</div>   

The function returns true, but the message does not come to an email. Thank you in advance.

rjdown
  • 9,162
  • 3
  • 32
  • 45
Sergii Onish
  • 140
  • 1
  • 8

2 Answers2

0

Your arguments to mail are not correct you are skipping message argument and passing headers there

futher more in place of subject you are passing body

Subject: Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters

You cannot pass new line "\n" to second argument that is supposed to be subject

  bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Documetation

A.B
  • 20,110
  • 3
  • 37
  • 71
0

Third argument of mail function should be message.
mail


Modify your code to something like:

mail($to, $subject, $message, $headers);

user3252599
  • 811
  • 1
  • 5
  • 6