2017-08-27 97 views
2

我不明白爲什麼我的PHP聯繫表發送空郵件。 請幫我弄清楚問題所在。PHP聯繫表發送空郵件

HTML表單代碼:

<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php"> 
    <div class="form-group"> 
     <input type="text" name="name" class="form-control" required="required" placeholder="Имя"> 
    </div> 
    <div class="form-group"> 
     <input type="phone" name="phone" class="form-control" required="required" placeholder="Номер телефона"> 
    </div> 
    <div class="form-group"> 
     <input type="email" name="email" class="form-control" required="required" placeholder="Email"> 
    </div> 
    <div class="form-group"> 
     <textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="Ваши пожелания"></textarea> 
    </div>       
    <div class="form-group"> 
    <input type="submit" name="submit" class="btn btn-submit" value="Отправить"> 
    </div> 
</form> 

PHP文件:

<?php 
$name  = @trim(stripslashes($_POST['name'])); 
$from  = @trim(stripslashes($_POST['email'])); 
$phone  = @trim(stripslashes($_POST['phone'])); 
$subject = @trim(stripslashes($_POST['subject'])); 
$message = @trim(stripslashes($_POST['message'])); 
$to   = '[email protected]'; 

$headers = array(); 
$headers[] = "MIME-Version: 1.0"; 
$headers[] .= "Content-type: text/plain; charset=iso-8859-1"; 
$headers[] .= "From: {$name} <{$from}>"; 
$headers[] .= "Reply-To: <{$from}>"; 
$headers[] .= "Subject: {$subject}"; 
$headers[] .= "X-Mailer: PHP/".phpversion(); 

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

die; 

我刪除後 '@' 我的錯誤日誌說:

[太陽8月27日21:41: [2017] [警告] [客戶端89.169.84.160] mod_fcgid: stderr:PHP注意:未定義索引: /var/www/u0337516/data/www/helloschool.ru/sendemail.php第2行, 引薦:http://helloschool.ru/

JS代碼(main.js):

var form = $('#main-contact-form'); 
    form.submit(function(event){ 
     event.preventDefault(); 
     var form_status = $('<div class="form_status"></div>'); 
     $.ajax({ 
      url: $(this).attr('action'), 
      beforeSend: function(){ 
       form.prepend(form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Отправка заявки...</p>').fadeIn()); 
      } 
     }).done(function(data){ 
      form_status.html('<p class="text-success">Спасибо, что оставили заявку! Мы свяжемся с вами как можно скорее.</p>').delay(3000).fadeOut(); 
     }); 
    }); 

預先感謝和遺憾的新手問題。

+1

檢查後提交表單。 –

+2

使用PHP的郵件功能永遠不會取得巨大的成功。節省很多頭痛和浪費時間,並開始使用SMTP。通過使用像Swiftmailer這樣的預先構建的解決方案來簡化它。 –

+0

爲什麼'@',你得到未定義的通知? – chris85

回答

0

1)您在php代碼中有錯誤。參數headers必須是字符串。

$headers = array(); 
$headers[] = "MIME-Version: 1.0"; 
$headers[] .= "Content-type: text/plain; charset=iso-8859-1"; 
$headers[] .= "From: {$name} <{$from}>"; 
$headers[] .= "Reply-To: <{$from}>"; 
$headers[] .= "Subject: {$subject}"; 
$headers[] .= "X-Mailer: PHP/".phpversion(); 

必須

$headers = ''; 
$headers .= "MIME-Version: 1.0"."\r\n"; 
$headers .= "Content-type: text/plain; charset=iso-8859-1"."\r\n"; 
$headers .= "From: {$name} <{$from}>"."\r\n"; 
$headers .= "Reply-To: <{$from}>"."\r\n"; 
$headers .= "Subject: {$subject}"."\r\n"; 
$headers .= "X-Mailer: PHP/".phpversion(); 

2)你message是俄語,但代碼頁ISO-8859-1僅支持拉丁字母,你需要西里爾。嘗試使用HTML的代碼頁(我認爲它是Windows-1251或UTF-8)。 3)您的javascript中有錯誤。

$.ajax({ 
     url: form.attr('action')}); 

必須

$.ajax({ 
     url: form.attr('action'), 
     data: form.serialize(), 
     method: "POST"}); 
+0

謝謝,您是對的,但在我做出這些更改後仍然無效。它可能是別的嗎?謝謝 –

+0

@StanislavDobryakov我認爲這是'codepage'問題。正如我所看到的,你使用俄語作爲'message',但'codepage' iso-8859-1只支持英語。嘗試選擇另一個代碼頁(Windows-1251,UTF-8)。嘗試發送電子郵件英文信息,而不是俄文 –

+0

謝謝,我試過了,沒有工作。順便說一下,我的錯誤日誌:[Sun Aug 27 21:41:14 2017] [warn] [client 89.169.84.160] mod_fcgid:stderr:PHP注意:Undefined index:name in/var/www/u0337516/data/www /helloschool.ru/sendemail.php第2行,referer:http://helloschool.ru/ –