2010-08-29 204 views
0

mailer.phpPHP發送電子郵件

<?php 
if(isset($_POST['submit'])) { 

$to = "[email protected]"; 
$subject = "Contact via website"; 
$name_field = $_POST['name']; 
$email_field = $_POST['email']; 
$message = $_POST['message']; 

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; 

echo "1"; 
echo "Data has been submitted to $to!"; 
mail($to, $subject, $body); 

} else { 
echo "0"; 
} 
?> 

jQuery代碼

$('.submit').click(function(){ 
      $('span.msg').css({'visibility': 'visible'}).text('Sending...'); 
      $.post("mailer.php", $(".contactPage").serialize(), 
       function(data){ 
        $('span.msg').text((data == "1") ? 'Your Message has been received. Thank you' : "Could not send your message at this time").show(); 
       }); 

      return false;  
     }); 

我總是得到

Could not send your message at this time 

我在PHP幾乎沒有知識,什麼我做錯了嗎?

回答

5

你的php腳本從不打印只是1它打印01Data has been submitted to...因此(data == "1")永遠不會是真的。


順便說一句:您的腳本可以用最少的mail()返回值來決定是否它標誌着成功或失敗。

+0

該怎麼做? – coure2011 2010-08-29 20:03:47

+0

我建議您打印{成功:true}而不是1或0。 在Jquery中使用一個JSON庫或簡單地使用(雖然不推薦) function(data){response = eval(data); if(response.success){alert(「Msg sent」); }} – 2010-08-29 20:26:55

+0

任何代碼片段? – coure2011 2010-08-29 22:32:25

0

首先,檢查服務器的郵件設置。如果你的腳本駐留在服務器上,你不管理上,最簡單的方法就是做這一情況,內容如下一個簡單的PHP文件:

<?php 
    mail("[email protected]","test","works"); <br> 
?> 

運行,並檢查你的電子郵件。

+0

是的,它工作... – coure2011 2010-08-29 23:05:29