联系表格不发送电子邮件

时间:2014-12-17 20:48:36

标签: php forms email

看起来一切都应该正常运行。我哪里做错了? 这是我对这个资源的第一个问题所以请不要严格判断。

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>   

该函数返回true,但邮件未收到电子邮件。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您的邮件参数不正确您正在跳过邮件参数并在那里传递标题

进一步取代你正在传递身体的主题

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

你无法通过新线&#34; \ n&#34;第二个应该是主题的论点

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

Documetation

答案 1 :(得分:0)

邮件功能的第三个参数应该是消息。
mail


将您的代码修改为:

邮件($ to,$ subject,$ message,$ headers);