2014-11-04 132 views
0

我有我的網站使用PHP聯繫表格,但它不能正常工作。當我填寫所有領域的正確提交。它顯示錯誤消息「無效的數據」。PHP的郵件功能沒有工作

<?php 

$action=$_REQUEST['action']; 
if ($action=="")  
    { 
    ?> 
    <form action="" method="POST" enctype="multipart/form-data"> 
    <input type="hidden" name="action" value="submit"> 
    Your name:<br> 
    <input name="name" type="text" value="" size="30" required/><br> 
    Your email:<br> 
    <input name="email" type="email" value="" size="30" required /><br> 
    Your message:<br> 
    <textarea name="message" rows="7" cols="30" required></textarea><br> <br> 
    <input type="submit" value="Send email" class="topbarbtn"/> 
    </form> 

<?php 
$subject=$_REQUEST['name']; 
$email=$_REQUEST['email']; 
$body=$_REQUEST['message']; 
}else{ 
$to = "[email protected]"; 
$subject = $subject; 
$from = $email; 
$message = $body; 


if (($from=="")||($subject=="")||($message=="")) 
     { 
     echo '<script type="text/javascript">alert("Invalid Details");</script>' ; 
     } else{ 

$headers = "From: " . $from . "\r\n"; 
$body .= $message; 
mail($to, $subject, $body, $headers); 
} 
?> 
+0

可以粘貼輸出?你能發送來自shell的郵件嗎? – michaelbn 2014-11-04 08:03:28

回答

0

在你的邏輯注意到一個缺陷 - 您嘗試從爲$電子郵件設置$,而你沒有得到在else語句變量$電子郵件

添加此行:

$from = $_REQUEST['email']; 

如果郵件仍然沒有發送,請注意,如果使用\ r \ n,某些郵件服務器(如qmail)會拒絕郵件。 因此,您應該嘗試僅將\ n或\ n \ n用作標題中的換行符。

0

喜歡這個工作:

<?php 

$action=$_REQUEST['action']; 


if ($action=="") {?> 

    <form action="" method="POST" enctype="multipart/form-data"> 
     <input type="hidden" name="action" value="submit"> 
     Your name:<br> 
     <input name="name" type="text" value="" size="30" required/><br> 
     Your email:<br> 
     <input name="email" type="email" value="" size="30" required /><br> 
     Your message:<br> 
     <textarea name="message" rows="7" cols="30" required></textarea><br> <br> 
     <input type="submit" value="Send email" class="topbarbtn"/> 
    </form> 

<?php 

} 
else { 
    $to = "...mail address..."; 
    $subject = $_REQUEST['name']; 
    $from = $_REQUEST['email']; 
    $message = $_REQUEST['message']; 


    if (($from=="")||($subject=="")||($message=="")) { 
     echo '<script type="text/javascript">alert("Invalid Details");</script>' ; 
    } 
    else{ 
     $headers = "From: " . $from . "\r\n"; 
     $body .= $message; 
     mail($to, $subject, $body, $headers); 
    } 
} 
?>